[英]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? 如何从Nodejs / Javascript AWS Lambda函数中识别区域?
The AWS_DEFAULT_REGION
environment variable gives a ReferenceError (See here , which is for Java, not Node/Javascript.) AWS_DEFAULT_REGION
环境变量提供了一个ReferenceError(请参阅此处 ,这是针对Java的,而不是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. 我意识到我可以从上下文对象中获取“invokedFunctionArn”并为该区域解析它,但似乎应该有更直接的方法。
Use the environment variable: 使用环境变量:
process.env.AWS_REGION
Source: http://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html#lambda-environment-variables 资料来源: 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.
一个丑陋的黑客会在包含OS(linux)中执行某些操作并希望显示信息。
uname -a
perhaps. uname -a
也许吧。
I'm just curious. 我只是好奇。 What do you need it for?
你需要什么? Some kind of debugging info?
某种调试信息?
There just doesn't seem to be a more direct way to identify the region in a running lambda function, other than using context.invokedFunctionArn property. 除了使用context.invokedFunctionArn属性之外,似乎没有更直接的方法来识别正在运行的lambda函数中的区域。
Here's what I ended up doing: 这是我最终做的事情:
exports.handler = (event,context, callback) => {
var arnList = (context.invokedFunctionArn).split(":");
var lambdaRegion = arnList[3] //region is the fourth element in a lambda function arn
//more code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.