How can one determine the current region within an AWS Lambda function?
JavaAmazon Web-ServicesAws LambdaJava Problem Overview
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.
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
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 - Java
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 - Java
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 - Java
-
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
-
You can read the AWS_DEFAULT_REGION environment variable
Regions.fromName(System.getenv("AWS_DEFAULT_REGION"))
Solution 6 - Java
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];