Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 728
Richard Crane Avatar asked Apr 05 '16 14:04

Richard Crane


People also ask

How do you find the region of a Lambda function?

To get the AWS Region where your Lambda Function is running you will need to import the os module. Then from the os module, you need to get the value of AWS_REGION from the environ mapping variable. This will return the AWS Region where the Lambda Function is running.

How do I find my current AWS region?

aws configure get region will get you the current region at that point in your script. If you are using a profile, then type aws configure get region --profile $PROFILE_NAME . This will return the region of the configuration, not the region that your aws cli invocation was performed from.

Does AWS Lambda have region?

Reserved environment variablesAWS_REGION – The AWS Region where the Lambda function is executed.


2 Answers

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.

like image 185
sihil Avatar answered Oct 09 '22 00:10

sihil


All Lambda containers has environment variables set $AWS_REGION

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

System.getenv("AWS_REGION") 
like image 31
Gokul Avatar answered Oct 08 '22 22:10

Gokul