The AWS account ID is a 12-digit number, such as 98765432109, that you use to construct Amazon Resource Names (ARNs). The AWS account ID distinguishes your resources from resources in other AWS accounts. 

Sometimes we may have to get the aws account id. The following code snippet will help you to get the account id of the account corresponding to the given credentials. This code need access to AWS STS service.

import boto3
client = boto3.client("sts", aws_access_key_id="XXXXXXXX", aws_secret_access_key="XXXXXXXX")
account_id = client.get_caller_identity()["Account"]
print account_id

view raw
get_account_id.py
hosted with ❤ by GitHub