How to Get AWS Account Id using Python Boto3

How to Get AWS Account Id using Python Boto3

How to Get AWS Account Id using Python Boto3

Dear Reader, I hope you are doing well. If you are looking for how to find your AWS account Id using python boto3. This post is for you. In this quick tutorial, I will help you get your AWS account Id using AWS SDK for Python, also known as Boto3.

Boto3: Boto3 is the Python SDK for AWS. It lets you create and manage(update/delete) resources at AWS using python script.

Related: 5 Ways to Create and Manage Resource at AWS

Don’t want to miss any posts from us? join us on our Facebook group, and follow us on Facebook, Twitter, LinkedIn, and Instagram. You can also subscribe to our newsletter below to not miss any updates from us.

Prerequisite

How to Get AWS Account Id using Python Boto3

AWS provides the Security Token Service or STS that usually lets you request temporary credentials for your IAM users or federated users.

AWS STS also exposes an API called GetCallerIdentity that gives you the details about the IAM user or role whose credential is used to call this API. Boto3 provides you with the same functionality using get_caller_identity() method of the Client that represents STS.

In this section, I’ll show you how to get your AWS account Id using Boto3. As you expect, we will use get_caller_identity() method.

Let’s start

Create an STS Client & Get AWS Account Id using Boto3

Create a new folder or python project and create a new file get-account-id.py

Let’s start adding code to it.

1. Import boto3 and connect to STS service

Import boto3 and create an STS client.

import boto3
sts = boto3.client('sts')

2. Call the client.get_caller_identity() method

Now that we have a client, we can call the method get_caller_identity() which is available to the STS client.

response = sts.get_caller_identity()

The get_caller_identity() method returns the response which is a dictionary.

Try printing the response and you will see something like below-

{
    'UserId': 'AIDAZZFUGMQ4PHIPEWRED',
    'Account': '123456789012',
    'Arn': 'arn:aws:iam::123456789012:user/dummy@gmail.com',
    'ResponseMetadata': {
        'RequestId': 'bf0019a9-9749-4f83-a1f4-7c70d869d551',
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'x-amzn-requestid': 'bf0019a9-9749-4f83-a1f4-7c70d869d551',
            'content-type': 'text/xml',
            'content-length': '420',
            'date': 'Mon, 19 Dec 2022 08:26:07 GMT',
        },
        'RetryAttempts': 0,
    },
}

3. Pick Account Id for your purpose

In the above response as you notice, the Account key has your account Id. You can pick the account Id from the python dictionary by using the key name Account as shown below-

account-id = response["Account"]

Final Code to Get AWS Account Id using Python Boto3

So this is our short and sweet example code in boto3 to get your AWS account Id.

import boto3
sts = boto3.client('sts')

response = sts.get_caller_identity()

print(response["Account"])

Once you run this python file, you should be able to see the accountId as you can see in the screenshot.

How to Get AWS Account Id using Python Boto3 1

Conclusion

In this tutorial, we learnt how to get AWS account Id using Python Boto3. First, we learnt about AWS Security Token Service or STS which provides the method get GetCallerIdentity that returns the details about the user/role that was used to call this API.

Later we used AWS SDK for Python or Boto3 to connect with STS and call this API to retrieve the account ID.

This you find this post useful? Please let me know in the comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on Facebook , TwitterLinkedIn and Instagram
  • Share this post with your friends

Leave a Reply

Your email address will not be published. Required fields are marked *