Open In App

How to get AWS Account Id in Lambda

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Python code for fetching account id using boto3 library

Output:

Code output after executing lambda function to fetch account id using sdk

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.

Python code to access account id  configured  in AWS Lambda environment variables

Output:

Code output after executing lambda function to fetch account id from environment variables


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:

Python code to parse Lambda ARN to get account id

Output:

account_id_sdk_output
Code output after executing lambda function to fetch account id by parsing Lambda ARN

Conclusion

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.


    Article Tags :

    Similar Reads