Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the AWS Region in a Javascript/Nodejs Lambda Function:

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.

like image 697
ktippetts Avatar asked Oct 06 '16 16:10

ktippetts


People also ask

How do I get AWS Region in Lambda node?

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.

Does AWS Lambda have region?

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.

Does AWS Lambda support JavaScript?

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.

How do I specify AWS region?

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.


Video Answer


2 Answers

Use the environment variable:

process.env.AWS_REGION

Source: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables

like image 115
Andrew Clark Avatar answered Sep 23 '22 13:09

Andrew Clark


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?

like image 33
Bela Vizy Avatar answered Sep 24 '22 13:09

Bela Vizy