CloudFormation-Lambda代码中的访问参数 [英] CloudFormation - Access Parameter from Lambda Code
问题描述
我有一个 CloudFormation
模板,看起来像这样:
I have a CloudFormation
template that looks like this:
{
"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"
}
}
}
}
我需要将 myParamToLambdaFunction
的值传递给Lambda
I need to pass the value of myParamToLambdaFunction
to the Lambda function.
有没有办法?
推荐答案
从> 18开始2016年11月,AWS Lambda现在支持环境变量,CloudFormation通过< a href = http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html#cfn-lambda-function-environment rel = nofollow noreferrer> <$ c AWS :: Lambda :: Function
资源。
As of 18 Nov 2016, AWS Lambda now supports environment variables, which CloudFormation supports through the Environment
property on the AWS::Lambda::Function
resource.
添加资源中的环境
属性,如下所示:
Add an Environment
property to your resource like this:
{
"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函数中引用环境变量orm(例如,Python中的 os.environ ['myParam']
)。
Then reference the environment variable from your deployed Lambda function according to your runtime platform (e.g., os.environ['myParam']
in Python).
这篇关于CloudFormation-Lambda代码中的访问参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!