无法访问 Lambda 函数中的环境变量。

huangapple go评论78阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2020年8月13日 01:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63381920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定