question

Luca Martinucci avatar image
Luca Martinucci asked

How to retrieve the AWS region where the Lambda function is running?

An Alexa skill I have developed calls a nodejs Lambda function that runs the same, identical code in different regions.

In order to customize its behavior according to the region, I have added an environment variable that "tells" the function which is its region.

I would like to retrieve this piece of information programmatically at runtime, in order to avoid using the environment variable and make the function totally region-independent.

Is it possible?

Thanks in advance.

lambda
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Andy Whitworth avatar image
Andy Whitworth answered

The region is already available in AWS supplied environment variables, you don't need to add you own.

https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

To access in node:

const region = process.env.AWS_REGION ||  process.env.AWS_DEFAULT_REGION

Not a python dev so don't have that code but check the AWS_REGION var first, it present then use that. If not present then use AWS_DEFAULT_REGION.


1 comment
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Luca Martinucci avatar image Luca Martinucci commented ·
I have tried the AWS supplied environment variables: they work fine.

Thanks a lot.

0 Likes 0 ·