简体   繁体   English

AWS Lambda环境变量

[英]AWS Lambda Environment Variable

When I add an environment variable to a aws-lambda through the aws-lambda console, I am able to reference these variables using: 当通过aws-lambda控制台将环境变量添加到aws-lambda时,我可以使用以下命令引用这些变量:

import os
# ...
print("environment variable: " + os.environ['variable'])

How would I use an environment variable in aws-lambda within a cloudformation template? 如何在cloudformation模板的aws-lambda中使用环境变量? I don't want to declare the environment variable in the aws-lambda console. 我不想在aws-lambda控制台中声明环境变量。

Thanks 谢谢

According AWS documentation , AWS::Lambda::Function resource has Environment property that you may use for specifying environment variables. 根据AWS文档AWS::Lambda::Function资源具有Environment属性 ,您可以使用该属性来指定环境变量。 So your resource in a cloud formation file would look like: 因此,您在云形成文件中的资源将如下所示:

{
  "Type" : "AWS::Lambda::Function",
  "Properties" : {
    "FunctionName" : "Your function name",
    "Environment" : {
        "Variables": {
           "variable1": "value1",
           "variable2": "value2"
        }
    },
    ...
  }
}

If you are using YAML template for declaring the cloud formation configuration, then you can define your environment variable under Parameters like: 如果您使用YAML模板声明云形成配置,则可以在“参数”下定义环境变量,例如:

Parameters:
  Domain: {Type: String, Default: 'test', AllowedValues: ['test', 'gamma', 'prod']}
  ...

And then use it where you have declared the Lambda Resources like below 然后在声明了Lambda资源的地方使用它,如下所示

Resources:
  LambdaFunction:
    Properties:
      Environment:
        Variables:
          DOMAIN: {Ref: Domain}
          .......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM