Skip to content

Commit 22a5164

Browse files
committed
Output OU names and IDs
1 parent b2dba02 commit 22a5164

File tree

8 files changed

+82
-1
lines changed

8 files changed

+82
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ module "organizational-units" {
3131
| aws\_region | AWS region | string | `us-east-1` | no |
3232
| ou\_list | List of organizational unit to manage. These will be top level under root | string | - | yes |
3333

34+
## Outputs
35+
36+
| Name | Description |
37+
|------|-------------|
38+
| organizational\_units | Organizational units |
39+
3440
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
3541
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM GRAPH HOOK -->
3642

examples/basic/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ terraform apply
2323
| aws\_region | AWS region | string | `us-east-1` | no |
2424
| ou\_list | List of organizational unit to manage. These will be top level under root | string | - | yes |
2525

26+
## Outputs
27+
28+
| Name | Description |
29+
|------|-------------|
30+
| organizational\_units | Organizational units |
31+
2632
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/basic/outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# External has results but no idea about plan/apply
2+
# Null understands plan/apply but has no output
3+
4+
output "organizational_units" {
5+
description = "Organizational units"
6+
value = "${module.example.organizational_units}"
7+
}

main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "null_resource" "organizational_units" {
1919
command = "echo '${jsonencode(map("aws_profile", var.aws_profile, "ou_list", var.ou_list))}' | bash -c ${path.module}/scripts/ou.sh"
2020
}
2121
}
22+
23+
data "external" "organizational_units" {
24+
program = ["bash", "${path.module}/scripts/get_ous.sh"]
25+
26+
query = {
27+
aws_profile = "${var.aws_profile}"
28+
}
29+
}

outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
# External has results but no idea about plan/apply
2+
# Null understands plan/apply but has no output
13

4+
output "organizational_units" {
5+
description = "Organizational units"
6+
value = "${data.external.organizational_units.result}"
7+
}

resource-plan-graph.png

34.1 KB
Loading

scripts/get_ous.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/local/bin/bash
2+
# #!/bin/bash
3+
#
4+
# This script is expected to be from Terraform via external provider
5+
6+
# Get the current OU names and ids
7+
#
8+
# Parameters:
9+
# aws_profile AWS profile
10+
11+
set -e
12+
eval "$(jq -r '@sh "aws_profile=\(.aws_profile)"')"
13+
14+
root_id=$(aws organizations list-roots --profile ${aws_profile} | jq -r .Roots[0].Id)
15+
16+
ou_exists_list=$(aws organizations list-organizational-units-for-parent --parent-id ${root_id} --profile ${aws_profile} | jq -r '.OrganizationalUnits[] | [.Name, .Id] | join(":")')
17+
declare -A ou_lookup
18+
for ou in ${ou_exists_list}; do
19+
ou_lookup["${ou%%:*}"]="${ou##*:}"
20+
done
21+
22+
# can only return strings
23+
#echo "{\
24+
# \"ou_names\": $(echo -n "${!ou_lookup[@]}" | jq -cRs 'split(" ")'),\
25+
# \"ou_ids\": $(echo -n "${ou_lookup[@]}" | jq -cRs 'split(" ")') \
26+
#}"
27+
28+
json="{"
29+
for ou in "${!ou_lookup[@]}"; do
30+
json="${json} \"${ou}\":\"${ou_lookup[${ou}]}\","
31+
done
32+
json="${json%%,}"
33+
json="${json} }"
34+
echo "${json}"

scripts/ou.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ for ou in ${ou_list}; do
3838
fi
3939
done
4040

41+
ou_exists_list=$(aws organizations list-organizational-units-for-parent --parent-id ${root_id} --profile ${aws_profile} | jq -r '.OrganizationalUnits[] | [.Name, .Id] | join(":")')
42+
declare -A ou_lookup
43+
for ou in ${ou_exists_list}; do
44+
ou_lookup["${ou%%:*}"]="${ou##*:}"
45+
done
46+
4147
# OUs: core, environments
4248
# What data should be outputted?
43-
jq -n --arg aws_profile "$aws_profile" '{"aws_profile":$aws_profile}'
49+
# all OU names and IDs
50+
# format?
51+
# Other things will need to be able lookup OU ID
52+
# 2 ordered lists ${!ou_lookup[@]} ${ou_lookup[@]}
53+
54+
echo "{\
55+
\"ou_names\":$(echo -n "${!ou_lookup[@]}" | jq -cRs 'split(" ")'),\
56+
\"ou_ids\":$(echo -n "${ou_lookup[@]}" | jq -cRs 'split(" ")') \
57+
}"

0 commit comments

Comments
 (0)