简体   繁体   中英

How to get the AWS Account Name using Terraform?

I'm trying to get the AWS Account Name to be able to use it later in my Terraform Code. I only have Account access so I not am able to use resources that need Organization privileges.

I thought this would work:

data "aws_iam_account_alias" "current" {}

output "account_id" {
  value = data.aws_iam_account_alias.current.account_alias
}

But it returns an empty list as the Account has no Aliases (turns out Account Name is different from Account Alias ).

Is there any similar way to get the Account Name using Terraform? (having full account permission but no organization permission)

you can use:

data "aws_caller_identity" "current" {}

output "account_id" {
  value = data.aws_caller_identity.current.account_id
}

output "caller_arn" {
  value = data.aws_caller_identity.current.arn
}

output "caller_user" {
  value = data.aws_caller_identity.current.user_id
}

Source: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity

You may use the aws_caller_identity data source to get the ID or ARN from the current account. It is analogous to the output of aws sts get-caller-identity . If you really need the Friendly Name of the account and not simply the ID, you can try to get it via the aws_organizations_organization data source , which exports all available accounts, with their ARN, ID, Name, and a few other attributes. Because you mentioned that you don't have organizations access, this might not be a viable solution.

AWS is horrible in naming, and AWS IAM account seems to be something different from AWS organization.

I believe what you are looking for is an AWS organization name, as I was looking for a name as well, but only got empty aliases, although I could be wrong.

You can get it here in Terraform.

Or from the CLI with:

aws aws organizations describe-account --account-id XXXXX

Edit: Link to the SO question which answered my question: The differences between IAM and AWS Organization

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