Get AWS account number if you have access keys

If you are regular AWS user you probably have access to multiple accounts. It's a common case that you maintain a big list in your .aws/credentials file. Let's say that you have reinstalled/changed your computer like I did recently, but instead of keeping account aliases in your file you tried to be smart and gave "more friendly" names to your credentials, or you didn't know account alias or simply account didn't have alias at all... That's not an issue if you don't need console access to any of those. So how can you retrieve actual account number for those accounts when you need it?

Obviously you can ask account owner(s), or filter your emails by subject "Welcome to Amazon Web Services" -> then try to login to each account, or if you are account owner you can login to root account.

Assuming that you have your access keys smarter way is to use AWS CLI and STS to get what you need. Here it is:

aws sts get-caller-identity --profile your_profile_name

If you do not add --profile param then command will use your default profile as usual. Response will contain your account number, access_key_id and your IAM user arn:

  "UserId": "ABCDEFGHJKL",
  "Account": "1234567890",
  "Arn": "arniam::***********:user/your_user_name"

More information can be found here:

If you want to get account alias, assuming that one has been assigned, you can do it using AWS CLI and IAM:

aws iam list-account-aliases --profile your_profile_name

Strange thing is that response will return array of aliases but your account can have only one which means that returned array will have 0 or 1 element.

  "AccountAliases": []

More information: