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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |