Java 如何确定 AWS Lambda 函数中的当前区域?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36428783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can one determine the current region within an AWS Lambda function?
提问by Richard Crane
Regions.getCurrentRegion()
returns null from within an AWS Lambda function. It seems that Regions.getCurrentRegion()
is not supported from within an AWS Lambda function. Is there an alternative way to determine which region the lambda function is running in?
Regions.getCurrentRegion()
从 AWS Lambda 函数中返回 null。似乎Regions.getCurrentRegion()
在 AWS Lambda 函数中不受支持。有没有其他方法可以确定 lambda 函数在哪个区域运行?
NOTE: AWS Lambda function is written in Java.
注意:AWS Lambda 函数是用 Java 编写的。
回答by garnaat
The context
object that is passed to your Lambda function has an attribute called invokedFunctionArn
. The ARN is of the format:
context
传递给您的 Lambda 函数的对象具有一个名为 的属性invokedFunctionArn
。ARN 的格式为:
arn:aws:<service>:<region>:<account_id>:<resource>
So you could split this string on the :
character and find the region associated with the Lambda function.
因此,您可以在:
字符上拆分此字符串并找到与 Lambda 函数关联的区域。
Note: In java you would call the getInvokedFunctionArn()
getter of the context object.
注意:在 Java 中,您将调用getInvokedFunctionArn()
上下文对象的getter。
回答by sihil
You can read the AWS_REGION
environment variable and use the Regions.fromName
function to parse that into a useable region.
您可以读取AWS_REGION
环境变量并使用该Regions.fromName
函数将其解析为可用区域。
Regions.fromName(System.getenv("AWS_REGION"))
The advantage of this over the ARN parsing approach is that you do not need a Context object which means you can use it outside of your handler function.
与 ARN 解析方法相比,此方法的优势在于您不需要 Context 对象,这意味着您可以在处理程序函数之外使用它。
回答by Gokul
All Lambda containers has environment variables set $AWS_REGION
所有 Lambda 容器都设置了环境变量 $AWS_REGION
From Java Code in Lambda.You can access it as below
从 Lambda 中的 Java 代码。您可以按如下方式访问它
System.getenv("AWS_REGION")
回答by vaquar khan
1) You can use environment variable and access it as
1)您可以使用环境变量并将其作为
System.getenv("AWS_REGION")
Following is a list of environment variables that are part of the AWS Lambda execution environment and made available to Lambda functions. The table below indicates which ones are reserved by AWS Lambda and can't be changed, as well as which ones you can set when creating your Lambda function. For more information on using environment variables with your Lambda function
以下是作为 AWS Lambda 执行环境一部分并可供 Lambda 函数使用的环境变量列表。下表显示了哪些是 AWS Lambda 保留且无法更改的,以及您在创建 Lambda 函数时可以设置的。有关在 Lambda 函数中使用环境变量的更多信息
https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
https://docs.aws.amazon.com/lambda/latest/dg/lambda-environment-variables.html
2) You can read the AWS_DEFAULT_REGION environment variable
2) 您可以读取 AWS_DEFAULT_REGION 环境变量
Regions.fromName(System.getenv("AWS_DEFAULT_REGION"))