How do I identify the region from within a Nodejs/Javascript AWS Lambda function?
The AWS_DEFAULT_REGION
environment variable gives a ReferenceError (See here, which is for Java, not Node/Javascript.)
I realize I can get the "invokedFunctionArn" from the context object and parse it for the region, but it seems like there should be a more direct way.
To get the AWS Region where your Lambda Function is running we need to access the Environment Variable AWS_REGION . To access the environment variable AWS_REGION using Node. js Lambda Function we can use process. env.
AWS Lambda Extensions are now generally available in all commercial regions where AWS Lambda is available. For more information, see the AWS Regional Services List. You can deploy extensions with functions packaged as both ZIP archives or as container images.
AWS Lambda functions using the Node. js 14 runtime now support code packaged as ECMAScript modules, allowing Lambda customers to consume a wider range of JavaScript packages in their Lambda functions.
Sign in to the AWS Management Console . Choose a service to go to that service's console. In the navigation bar, choose the name of the currently displayed Region. Then choose the Region to which you want to switch.
Use the environment variable:
process.env.AWS_REGION
Source: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables
You can take a look at the context that is passed in. There maybe more goodies to discover there.
exports.handler = function(event, context) {
console.log('Function name:', context.functionName);
context.succeed();
};
I don't know anything that simply tells you the region. An ugly hack would be exec-ing something in the containing OS (linux) and hoping for the info to show up. uname -a
perhaps.
I'm just curious. What do you need it for? Some kind of debugging info?
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