Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get AWS Account ID from Boto

I have an AWS_ACCESS_KEY_ID and an AWS_SECRET_KEY. These are active credentials, so they belong to an active user, who belongs to an AWS Account. How, using Boto3, do I find the ID of this AWS Account?

like image 241
Zags Avatar asked Apr 19 '16 05:04

Zags


People also ask

How do I find my AWS account ID?

To find your AWS account ID when signed in as the root userIn the navigation bar on the upper right, choose your account name or number and then choose My Security Credentials. Expand the Account identifiers section. The account number appears next to the label AWS account ID.

How do I get my AWS account ID from command line?

Finding Your Account ID using the AWS CLI Use the following command to view your user ID, account ID, and your user ARN: aws sts get-caller-identity.

How do I find my AWS Boto3 username?

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.


1 Answers

The AccountID can be grabbed from the get-caller-identity sts function. This returns an "Account" field:

client = boto3.client("sts", aws_access_key_id=access_key, aws_secret_access_key=secret_key) account_id = client.get_caller_identity()["Account"] 
like image 78
louahola Avatar answered Oct 27 '22 00:10

louahola