从.env文件访问Lambda环境变量

时间:2020-05-31 04:47:23

标签: c# .net-core aws-lambda aws-api-gateway aws-amplify

我正在运行.net core 3.1 AWS Lambda函数,但无法使用以下命令从c#中的AWS Lambda控制台访问环境变量:

var envVariable = Environment.GetEnvironmentVariable("myVariableName");

环境变量存储在lambda函数根目录下的.env文件中。是否需要将.env文件复制到build文件夹中才能在lambda函数中进行访问?如何从.env文件访问AWS Lambda环境变量?

应用程序结构

Application Structure

cloudformation-template.json 是指.env文件。

enter image description here

更新:2020年5月31日:

我删除了.env文件,并将变量直接添加到cloudformation-template.json中作为单个变量。当我尝试使用amplify publish命令将其推送到aws时,我得到

Resource is not in the state stackUpdateComplete
An error occured during the push operation: Resource is not in the state stackUpdateComplete error

1 个答案:

答案 0 :(得分:0)

我可以通过将环境变量直接添加到cloudformation-template.json并运行amplify publish命令来推送到aws来解决此问题。

"Environment": {
                    "Variables": {                      
                        "VUE_APP_STRIPE_PUBLISHABLE_KEY": "<someRandomString>",
                        "VUE_APP_STRIPE_SECRET_KEY": "<someRandomString>"
                    }
                },