英文:
Unable to Access Environment Variables in Lambda Function
问题
我想从我的Lambda函数内部访问环境变量,但是我一直在解决这个问题。
我使用Serverless来管理创建和部署我的Lambda函数。在我的Serverless文件中的一个函数定义中,我定义了2个环境变量:
functions:
update-item:
handler: ...
environment:
FUNCTION_NAME: updateItemById
TOPIC_NAME: ${self:custom.topicName}
events: ...
在我的Lambda函数中,我像这样访问环境变量TOPIC_NAME
:
Optional<String> topicName = Optional.of(System.getenv("TOPIC_NAME"));
这段代码在每次Lambda函数执行时都会抛出异常,因为TOPIC_NAME
环境变量不存在。然而,我可以看到环境变量存在于以下位置:
- Serverless.yml文件
- Serverless生成的CloudFormation文件
- 在AWS控制台中显示的CloudFormation文件
- 在AWS控制台中显示的Lambda函数
在Lambda函数内部,我使用一个小循环来打印出所有的环境变量:
Map<String, String> envVars = System.getenv();
for (String s : envVars.keySet()) {
System.out.println(s + " - " + envVars.get(s));
}
FUNCTION_NAME
环境变量会显示出来,但TOPIC_NAME
变量不会。我尝试过重命名、添加和删除环境变量,但这个打印循环的输出没有变化。我多次重新部署了我的代码,有时有代码更改,有时没有配置文件更改,我手动通过控制台添加了环境变量。我验证了Lambda函数的最新版本正在运行。但是,我所做的一切都没有起作用。
根据这种行为,似乎我的Lambda函数运行的容器从未被刷新,因此始终包含旧的环境变量。然而,这与我所阅读的一切以及我对Lambda容器的预期工作方式相矛盾。
英文:
I want to access environment variables from within my lambda function, however I've been having issues with accomplishing that.
I use serverless to manage the creation and deployment of my lambda functions. Within one of the function definitions in my serverless file I have defined 2 environment variables:
functions:
update-item:
handler: ...
environment:
FUNCTION_NAME: updateItemById
TOPIC_NAME: ${self:custom.topicName}
events: ...
Within my lambda function I access the environment variable TOPIC_NAME
like so:
Optional<String> topicName = Optional.of(System.getenv("TOPIC_NAME"));
This code throws an exception every time the lambda function executes because the TOPIC_NAME
environment variable doesn't exist. However, I can see that the environment variable exists in:
- The serverless.yml file
- The CloudFormation files that serverless generates
- The CloudFormation files as shown in the AWS console
- The lambda function as shown in the AWS console
Within the lambda function I use a small loop to print out all of the environment variables:
Map<String, String> envVars = System.getenv();
for (String s : envVars.keySet()) {
System.out.println(s + " - " + envVars.get(s));
}
The FUNCTION_NAME
environment variable is displayed, however the TOPIC_NAME
variable is not. I've tried renaming, adding, and removing environment variables and there is no change to the output of this print loop. I've re-deployed my code numerous times, with and without code changes, with config file changes, I've manually added the environment variable through the console. I've verified that the latest version of the lambda function is running. Nothing I've done has worked.
Based on the behavior it seems like the container that my lambda function is running in is never being refreshed and thus always contains the old environment variables. However, this runs contrary to everything I've read and how I expect lambda containers to work.
答案1
得分: 3
...
provider: aws
custom:
topicName: <some-topic-name>
英文:
${self:custom.topicName}
You need to make sure you have the following top-level reference configured in your serverless.yml
:
...
provider: aws
custom:
topicName: <some-topic-name>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论