gpt4 book ai didi

amazon-web-services - 如何获取 AWS Lambda 函数中阶段的名称?

转载 作者:行者123 更新时间:2023-12-02 02:55:47 25 4
gpt4 key购买 nike

我有一个 lambda 函数(没有 APIGateWay。我想在测试和生产阶段使用相同的 lambda 函数,为此我想在运行时获取阶段名称。我的代码是用 java 编写的,我知道我们可以找到使用以下内容的默认 AWS 区域:

Regions regions = Regions.fromName(System.getenv("AWS_DEFAULT_REGION"));

所以这是我的问题:

1- 如何获取我的 lambda 函数运行阶段的名称?

2- 这个默认区域是什么意思?它会始终与运行我的 lambda 函数的区域相同吗?

非常感谢任何线索。

最佳答案

How to get the name of the stage in which my lambda function is running?

对于 Lambda 代理集成,您可以从输入流中的 requestContext 获取阶段名称,其中包含由 API 网关序列化为 JSON 字符串的 API 请求。输入数据还可以包括请求的阶段变量 stageVariables(如果您使用/需要的话)。例如:

JSONParser parser = new JSONParser();

public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
...
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String stage = null;

JSONObject event = (JSONObject)parser.parse(reader);
if (event.get("requestContext") != null) {
JSONObject requestContext = (JSONObject)parser.parse((String)event.get("requestContext"));
if (requestContext.get("stage") != null) {
stage = (String)requestContext.get("stage");
}
}
...
}

参见 full example .

What does this default region means? will it be always same as the region in which my lambda function is running?

根据 docs并且根据SDK .

SDK 不引用它。它仅使用 AWS_REGION。参见 SDKGlobalConfiguration.java .

关于amazon-web-services - 如何获取 AWS Lambda 函数中阶段的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49496694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com