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.
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.
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.
Reserved environment variablesAWS_REGION – The AWS Region where the Lambda function is executed.
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.
All Lambda containers has environment variables set $AWS_REGION
From Java Code in Lambda.You can access it as below
System.getenv("AWS_REGION")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With