From 89a45a747ea5c0cdcc7882cfd0fa5640665c77b5 Mon Sep 17 00:00:00 2001 From: xpk Date: Wed, 17 Aug 2022 15:20:12 +0800 Subject: [PATCH] NEW: account-list module --- modules/util/account-list/README.md | 16 ++++++++++++++++ modules/util/account-list/main.tf | 2 ++ modules/util/account-list/outputs.tf | 3 +++ modules/util/account-list/provider.tf | 10 ++++++++++ 4 files changed, 31 insertions(+) create mode 100644 modules/util/account-list/README.md create mode 100644 modules/util/account-list/main.tf create mode 100644 modules/util/account-list/outputs.tf create mode 100644 modules/util/account-list/provider.tf diff --git a/modules/util/account-list/README.md b/modules/util/account-list/README.md new file mode 100644 index 0000000..7748081 --- /dev/null +++ b/modules/util/account-list/README.md @@ -0,0 +1,16 @@ +# acocunt-list module +This module returns a list of accounts by querying the aws_organizations_organiation datasource. It returns an accounts map like this + +``` +{ + account-name-1 = "111111111111" + account-name-2 = "111111111111" +} +``` + +In the root module, query the account id like this +```terraform +output result { + value = lookup(module.account-list.accounts, "account-name-1") +} +``` \ No newline at end of file diff --git a/modules/util/account-list/main.tf b/modules/util/account-list/main.tf new file mode 100644 index 0000000..8ca0bca --- /dev/null +++ b/modules/util/account-list/main.tf @@ -0,0 +1,2 @@ +data "aws_organizations_organization" "org" {} + diff --git a/modules/util/account-list/outputs.tf b/modules/util/account-list/outputs.tf new file mode 100644 index 0000000..33d1c70 --- /dev/null +++ b/modules/util/account-list/outputs.tf @@ -0,0 +1,3 @@ +output accounts { + value = zipmap(data.aws_organizations_organization.org.accounts[*].name, data.aws_organizations_organization.org.accounts[*].id) +} \ No newline at end of file diff --git a/modules/util/account-list/provider.tf b/modules/util/account-list/provider.tf new file mode 100644 index 0000000..da0e9eb --- /dev/null +++ b/modules/util/account-list/provider.tf @@ -0,0 +1,10 @@ +terraform { + required_version = "~> 1.2.5" + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.22" + } + } +} +