How to get AWS Account Id in Lambda
Last Updated :
23 Jul, 2025
AWS Lambda is a FaaS (Function as a Service) provided by Amazon Web Services. It is a compute service which can be used to run code in response to an event without provisioning or managing servers making it an optimal choice for creating event-driven serverless applications. AWS Lambda provides high-availability compute infrastructure and performs all the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, and logging. You only pay for the compute time that you consume and there is no charge when your code is not running.
Why fetch account ID inside Lambda Function
- Multi-Account Management – When using multiple accounts in AWS for different purposes like development, testing, billing management, etc, fetching the AWS Account ID can help Lambda functions in managing resources and applying different configurations or policies according to the account context.
- Logging and Monitoring - Including the AWS Account ID in logs helps with tracking and auditing activities and facilitates troubleshooting by identifying which account generated specific logs.
- Billing and Cost Allocation - For organizations using consolidated billing or cost tracking, knowing the AWS Account ID can help in attributing costs and usage to the appropriate account. This can aid in financial management and cost optimization efforts.
- Custom Alerts and Notifications – Fetching account ID in lambda functions can help in sending custom alerts, notifications, or emails to relevant clients, stakeholders, and teams giving them account context about issues or changes specific to their account.
Methods to retrieve account ID inside Lambda Function
Using AWS SDK
The most straightforward and common method to retrieve account ID is to use the SDK provided by AWS. AWS SDK (Software Development Kit) is a set of toolkits provided by AWS to interact with their resources or services programmatically.
AWS SDKs can be used to get account IDs using different languages supported by lambda like Python, JavaScript, etc.
Here is an example of fetching AWS account id using the “boto3” library in Python:
Output:
Using AWS Lambda Environment Variables
If you want to access account id frequently and don’t want to make repeated calls to AWS services then you can setup the account id as lambda environment variables and use them in the code directly.
To set the environment variable:
- Go to the AWS Lambda console.
- Select your function.
- Under the "Configuration" tab, click on "Environment variables."
- Add a new environment variable with the key ACCOUNT_ID and the value set to your AWS Account ID.
Here is the python code to access your account id configured in environment variables under lambda function.
Output:
Using IAM Role ARN
If you have access to the ARN of the Lambda function’s execution role, you can extract the account ID directly from it. This approach is less common but can be used when other methods are not feasible.
General format of ARN: arn:partition:service:region:account-id:resource
Components of an ARN
- arn: This is the literal prefix indicating that the string is an ARN.
- partition: The partition in which the resource is located. Common partitions include:
- aws for standard AWS regions
- aws-us-gov for AWS GovCloud (US)
- aws-cn for AWS China regions
- service: The AWS service namespace that owns the resource. Examples include s3, ec2, lambda, etc.
- region: The region in which the resource resides. Some ARNs do not include a region, such as those for global services (e.g., IAM). Region codes follow a format like us-east-1, eu-west-1, etc.
- account-id: The AWS account ID of the resource owner. This is a 12-digit number.
- resource: The specific resource identifier within the service. The format of this part varies by service and can include resource type, ID, and other parameters.
Here’s a code snippet to parse the ARN in Python:
Output:
Code output after executing lambda function to fetch account id by parsing Lambda ARNConclusion
Retrieving the AWS Account ID within a Lambda function is essential for effective resource management, cost tracking, and security in the AWS environment. You can use methods like the AWS SDK, environment variables, or ARN parsing to integrate the account ID into your Lambda functions, enhancing visibility and control across serverless applications. These practices are crucial for optimizing AWS Lambda use and ensuring effective cloud management.
Similar Reads
How To Implement MFA For AWS Account MFA stands for Multi-Factor Authentication. In AWS, it acts as a second layer of security to protect AWS accounts. Even if someone knows your password, they cannot access your account because they do not have your physical device. This is what it aims to achieve and it is a highly recommended securi
9 min read
How to Deploy Django Application in AWS Lambda? Pre-requisite: AWS , Python Django is a Python web framework that makes it easy to build web applications quickly and securely. It has a large and helpful community that provides support and contributes to its development. AWS Lambda is a serverless computing platform that runs your code in Docker c
7 min read
How to Configure AWS Lambda? AWS Lambda is a responsive cloud service that examines the steps followed within any application and responds to them by compiling the codes that have been defined by the users, also known as functions. The service automatically computes and manages the resources across multiple availability zones a
4 min read
How To Create Cron Job In AWS Lambda? A Cron job works like scheduling a task in any system. It is mainly used for backups, system maintenance, etc. Cron's job works on both local systems as well as cloud services. To run the crown job in AWS, we have to use AWS Lambda. In AWS Lambda, we set up the functions and schedule a time to run t
7 min read
How to Duplicate an AWS Lambda Function AWS Lambda is a serverless computing technology made by Amazon Web Services, that allows one to run a piece of code without any need of maintaining a server. This makes it an ideal choice for various applications, from simple task automation to complex backend services. However, there may be a need
4 min read
How to Get Session Token in AWS? A session token is a popular concept that is used in AWS for giving access to some user or person for a limited amount of time, in this the user gets to access the AWS resources but only for a limited amount of time only.The purpose of the session token is to have more security in the AWS system so
6 min read