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];