Richard Crane
Richard Crane

Reputation: 1220

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

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.

Upvotes: 73

Views: 76641

Answers (9)

Evandro Pomatti
Evandro Pomatti

Reputation: 15124

From @eis comment using Region.of()

Region.of(System.getenv("AWS_REGION"));

Upvotes: 0

If anyone looking to get the region in Node JS. This will be work

process.env.AWS_REGION

Upvotes: 10

Yuri
Yuri

Reputation: 4478

As its javadoc states Regions.getCurrentRegion() is relevant in the AWS EC2 context only. In other contexts it returns null as it does in the AWS Lambda context.

AWS Lambda by default defines the AWS_REGION environmental variable holding the region name. The value can be read by System.getenv("AWS_REGION").

Upvotes: 2

Brandon Evans
Brandon Evans

Reputation: 26

Although using the AWS_REGION environment variable will work in most cases, I've found that with a Lambda@Edge, this variable will resolve to the region from which the content was served (i.e. the closest region to the client). Using the invokedFunctionArn value from the context object will not work either for the same reason. Here is the context I received when invoking a Lambda in us-east-1 from a location closest to us-east-2:

{
  "callbackWaitsForEmptyEventLoop": true,
  "functionVersion": "6",
  "functionName": "us-east-1.<FUNCTION_NAME>",
  "memoryLimitInMB": "128",
  "logGroupName": "/aws/lambda/us-east-1.<FUNCTION NAME>",
  "logStreamName": "2020/09/04/[6]<LOG STREAM NAME",
  "invokedFunctionArn": "arn:aws:lambda:us-east-2:<ACCOUNT ID>:function:us-east-1.<FUNCTION NAME>:6",
  "awsRequestId": "0fa5f5c3-90ea-41d5-b3c3-1714ccdf1b17"
}

So, the solution that I've found works consistently between Lambda@Edge and other Lambdas is to retrieve the region from the functionName value from the context object. Here's how I am doing this with Node.js:

functionName.split('.')[0];

Upvotes: 1

James Shapiro
James Shapiro

Reputation: 6226

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}!')
    }

Upvotes: 21

sihil
sihil

Reputation: 2721

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.

Upvotes: 85

vaquar khan
vaquar khan

Reputation: 11449

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"))

Upvotes: 5

Gokul
Gokul

Reputation: 493

All Lambda containers has environment variables set $AWS_REGION

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

System.getenv("AWS_REGION")

Upvotes: 23

garnaat
garnaat

Reputation: 45876

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.

Upvotes: 14

Related Questions