3

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)

  • Have you set the [account alias](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-alias.html) already? – baduker May 09 '22 at 16:33
  • 1
    no, as what I'm trying the get is the account_name and not the account_alias. If I use the CLI with: ``` aws iam list-account-aliases ``` I get and empty list. ``` { "AccountAliases": [] } ``` – nicolas spencer May 09 '22 at 19:27
  • What is Account name? Account number? – Marcin May 10 '22 at 00:01

3 Answers3

3

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

MutantMahesh
  • 1,480
  • 15
  • 20
0

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.

logyball
  • 158
  • 1
  • 6
-1

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

vladimirror
  • 729
  • 12
  • 8