This is doable in TF, but kind of painful. Absent creating your own SSM parameter in the account and referencing that in your TF, you have to query your AWS org for the name as noted by others.
- You'll likely need a provider alias that is configured to point at your org account, because your TF target is probably not the same account.
provider "aws" {
alias = "controltower"
region = "us-east-1"
profile = "mycorp-controltower"
}
Don't forget the un-aliased/default provider pointed at your target account. Otherwise terraform will assume the "controltower" provider here for everything.
- Grab the org information. The only way I can find to do this is to get all org accounts first.
data.aws_caller_identity.current.account_id
data "aws_organizations_organization" "org" {
provider = aws.controltower
}
data "aws_organizations_organizational_unit_descendant_accounts" "accounts" {
parent_id = data.aws_organizations_organization.org.roots[0].id
provider = aws.controltower
}
- Now you have a list of accounts that aren't keyed - it's just an array. Effectively, you have to search for your target account and extract the name:
[for acct in data.aws_organizations_organizational_unit_descendant_accounts.accounts.accounts: acct.name if acct.id == data.aws_caller_identity.current.account_id][0]
An SSM parameter would be easier than this, but on the downside its value would have to be managed properly ie if for some reason you changed the account's name. Calling the org data like this gets you an answer that won't drift.