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

Richard Crane picture Richard Crane · Apr 5, 2016 · Viewed 29.9k times · Source

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.

Answer

sihil picture sihil · May 17, 2016

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.