How can one determine the current region within an AWS Lambda function?

43,238

Solution 1

You can read the AWS_REGION environment variable and use the Regions.fromName function to parse that into a useable region.

Regions.fromName(System.getenv("AWS_REGION"))

The advantage of this over the ARN parsing approach is that you do not need a Context object which means you can use it outside of your handler function.

Source: AWS's Lambda environment variables docs.

Solution 2

All Lambda containers has environment variables set $AWS_REGION

From Java Code in Lambda.You can access it as below

System.getenv("AWS_REGION")

Solution 3

The context object that is passed to your Lambda function has an attribute called invokedFunctionArn. The ARN is of the format:

arn:aws:<service>:<region>:<account_id>:<resource>

So you could split this string on the : character and find the region associated with the Lambda function.

Note: In java you would call the getInvokedFunctionArn() getter of the context object.

Solution 4

For anyone looking to do this in Python:

import os
import json

def lambda_handler(event, context):
    my_region = os.environ['AWS_REGION']
    print(my_region)
    return {
        'statusCode': 200,
        'body': json.dumps(f'Hello from {my_region}!')
    }

Solution 5

1) You can use environment variable and access it as

System.getenv("AWS_REGION")

Following is a list of environment variables that are part of the AWS Lambda execution environment and made available to Lambda functions. The table below indicates which ones are reserved by AWS Lambda and can't be changed, as well as which ones you can set when creating your Lambda function. For more information on using environment variables with your Lambda function

https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html

2) You can read the AWS_DEFAULT_REGION environment variable

Regions.fromName(System.getenv("AWS_DEFAULT_REGION"))
Share:
43,238
Richard Crane
Author by

Richard Crane

Entrepreneur, Architect, and Developer Rich likes to work with the latest products and technologies such as Azure, Amazon Web Services, .NET, Silverlight, HTML5, SQL Server, Hadoop, Elastic Map Reduce, Windows Phone 8, and iOS (iPad/iPhone). He has deep expertise in a wide range of technologies including web applications, databases, high performance computing, distributed systems and user interface technologies. Current interests include cloud computing, high performance computing, large scale data processing, financial technical analysis, and scalable database architectures.

Updated on September 04, 2020

Comments

  • Richard Crane
    Richard Crane over 3 years

    Regions.getCurrentRegion() returns null from within an AWS Lambda function. It seems that Regions.getCurrentRegion() is not supported from within an AWS Lambda function. Is there an alternative way to determine which region the lambda function is running in?

    NOTE: AWS Lambda function is written in Java.