Retrieving your AWS account ID is a common requirement when managing AWS resources programmatically using Boto3, the AWS SDK for Python. This article provides a straightforward guide to obtaining your AWS account ID using Boto3.

import boto3
session = boto3.Session()
sts_client = session.client('sts')
account_id = sts_client.get_caller_identity()['Account']

Prerequisites

Setting Up a Boto3 Session

Before we can retrieve our AWS account ID, we need to set up a Boto3 session. This session will allow us to connect to AWS and access our resources:

import boto3
session = boto3.Session()

This will create a default session using the credentials and configuration set up on your local machine.

Retrieving AWS Account ID

Now, we can use Boto3 to retrieve our AWS account ID. We will use the get_caller_identity() method from the sts (Security Token Service) client. This method will return information about the AWS account associated with the credentials used to create the session:

import boto3
session = boto3.Session()
sts_client = session.client('sts')
account_id = sts_client.get_caller_identity()['Account']

The get_caller_identity() method will return a dictionary with information about the account, including the account ID. We can access the account ID using the key Account in the dictionary.

Using Different Credentials

If you have multiple AWS accounts or need to use different credentials, you can specify them when creating the session. For example, if you have a profile set up in your AWS credentials file, you can use the following code to create a session with those credentials:

import boto3
session = boto3.Session(profile_name='my_profile')
sts_client = session.client('sts')
account_id = sts_client.get_caller_identity()['Account']

You can also specify the access key and secret key directly in the session creation code:

import boto3
session = boto3.Session(
	aws_access_key_id='my_access_key',
	aws_secret_access_key='my_secret_key'
)
sts_client = session.client('sts')
account_id = sts_client.get_caller_identity()['Account']

The code above is especially useful when using boto3 to assume IAM roles.

FAQ

Where do I get my AWS account ID?

To locate your AWS account ID, sign into the AWS Management Console. Once signed in, click on your name or account name in the top navigation bar. The 12-digit account number that appears is your AWS account ID. Alternatively, you can use aws sts get-caller-identity AWS CLI command.

How to get account ID in Lambda Python?

You can use context object to get Lambda ARN, e.g. my_arn = context.invoked_function_arn. Parse obtained ARN to get the account ID: aws_account = my_arn.split(":")[4]. Alternatively, use the get_caller_identity method of the boto3 sts client.

How does boto3 know which AWS account to use?

Boto3 identifies the AWS account to use based on the provided credentials. These credentials, typically stored in environment variables like AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, can also be specified in a configuration file or within the code. These credentials are associated with an active user within an AWS account.

What is the format of AWS account ID?

The AWS account ID is a unique identifier associated with an AWS account. It is a 12-digit string of numbers without hyphens. For example, it could look like this: 123456789012. It’s important to note that this ID is unique to each account and is used to manage resources and permissions within AWS.

Conclusion

This concise script is all you need to fetch your AWS account ID using Boto3. It’s a simple yet essential task for managing AWS resources and automation scripts in Python. Remember, handling AWS credentials securely is crucial in any AWS-related development.