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
- Python
- Boto3 installed (
pip install boto3
). - AWS credentials configured (usually via the AWS CLI or aws-vault).
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?
aws sts get-caller-identity
AWS CLI command.How to get account ID in Lambda Python?
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?
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?
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.