As of 2016 年 11 月 18 日, AWS Lambda 现在支持环境变量,CloudFormation 通过以下方式支持Environment财产在AWS::Lambda::Function资源。
Add an Environment
像这样的资源属性:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "This template will deploy stuff",
"Parameters":{
"myParamToLambdaFunction" : {
"Description" : "Please enter the the value",
"Type" : "String",
"ConstraintDescription" : "must have some value."
}
},
"Resources": {
"SecGrpValidatorFromCFTemplate": {
"Type": "AWS::Lambda::Function",
"Properties": {
"FunctionName": "mylambdafunctionname",
"Handler": "myfile.lambda_handler",
"Role": {
"Fn::GetAtt": ["somerole", "Arn"]
},
"Timeout": "30",
"Runtime": "python2.7",
"Code": {
"S3Bucket":"mybucket",
"S3Key":"mylambdafunction.zip"
},
"Environment": {
"Variables": {
"myParam": {
"Ref": "myParamToLambdaFunction"
}
}
}
}
}
}
然后根据您的运行时平台从部署的 Lambda 函数引用环境变量(例如,os.environ['myParam']
在Python中)。