简体   繁体   中英

Get AWS account ID by name

I know there are multiple ways to get AWS account name by its ID, but is the opposite possible? Is there a way to programmatically ( API , CLI , terraform etc.) get AWS account ID by its name?

Update : Forgot to mention that these accounts exist under organization structure in a specific OU, maybe this could help.

While this is not ideal, I realized that aws organizations list-accounts-for-parent command is the best compromise. It would give me all accounts within given OU, which I can filter by account name.

Given that my solution will ultimately be implemented in terraform I came out with something like this

data "external" "accounts" {
  program = ["aws", "organizations", "list-accounts-for-parent", "--parent-id", local.ou, "--query", "Accounts[?Name==`${local.account_name}`] | [0]"]
}

locals {
   ou           = "ou-12345678"
   account_name = "my-cool-account"
   account_id   = lookup(data.external.tools_accounts.result, "Id", null)
}

it would execute AWS CLI command, return back a map of key/values if account info is found, and lookup function would retrieve the account ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM