Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you get the AWS account name from boto?

I have an AWS key and secret key and would like to call boto to get the account name.

I can get account ID, but the AWS account name is a mystery.

like image 446
blacksheep9000 Avatar asked Sep 03 '14 14:09

blacksheep9000


2 Answers

To get the AWS account alias in boto3:

alias = boto3.client('iam').list_account_aliases()['AccountAliases'][0]
  • While the API response allows for multiple account aliases, AWS docs on aliases say there can be only one per account.
  • The account alias is not the same as the account name, but it's alphanumeric and more usable than account number. The alias must be created under IAM settings for account (or using AWS CLI) - not all accounts will have an alias.
  • Getting the alias doesn't require as much privilege to access as getting the account name via the boto3 organizations service.

To get account ID (account number):

id = boto3.client('sts').get_caller_identity().get('Account')
like image 70
RichVel Avatar answered Sep 17 '22 14:09

RichVel


Its late but might be helpful for future. If you are using organization service then using below code you can fetch Account Name.

org = boto3.client('organizations')
account_name = org.describe_account(AccountId='account-id').get('Account')
print(account_name ['Name'])

For more info

like image 45
saranjeet singh Avatar answered Sep 20 '22 14:09

saranjeet singh