我正在尝试创建一个将部署 Lambda 函数的 CloudFormation 模板,并且我需要将安全选项作为可选参数。
我能够使用此处的问题部分完成此任务:
How to make a whole object in CloudFormation templates optional?
有趣的是,该方法可以很好地使 VpcConfig 属性在 AWS GUI 控制台中成为可选属性,但无法使其在 CLI 中成为可选属性。不幸的是,我需要它在 CLI 中工作,因为我将使用 CodeBuild 来调用和部署此模板的资源。
相关参数如下:
"SecurityGroupIds" : {
"Type" : "CommaDelimitedList",
"Description" : "A list of one or more security groups IDs in the VPC that includes the resources to which your Lambda function requires access."
},
"SubnetIds" : {
"Type" : "CommaDelimitedList",
"Description" : "A list of one or more subnet IDs in the VPC that includes the resources to which your Lambda function requires access."
}
及条件:
"HasVPC": {"Fn::And": [{"Fn::Not": [{"Fn::Equals": [{"Fn::Join": ["", {"Ref": "SubnetIds"}]}, ""]}]}, {"Fn::Not": [{"Fn::Equals": [{"Fn::Join": ["", {"Ref": "SecurityGroupIds"}]}, ""]}]}]}
下面是在模板的资源部分定义的 Lambda 资源中使用该条件的位置:
"VpcConfig": {
"Fn::If": [
"HasVPC",
{
"SecurityGroupIds" : {"Ref": "SecurityGroupIds"},
"SubnetIds" : {"Ref": "SubnetIds"}
},
{ "Ref":"AWS::NoValue" }
]
},
当我在 CLI 中发出命令部署此堆栈时,出现以下错误:
An error occurred (ValidationError) when calling the CreateChangeSet operation: Parameters: [SecurityGroupIds, SubnetIds] must have values
这是我从模板所在的同一目录发出的 AWS CLI 命令。注意:ARN 值均经过大量修改,不再是我帐户中的真实值,但我将它们保留为正确的格式,以便您可以看到命令的真实格式:
aws cloudformation deploy --template-file lambda-template.json --stack-name "CLI-lambda-stack" --parameter-overrides S3BucketName="myBucket" S3FileLocation="lambda_function.zip" S3ObjectVersion="ZuB0iueEghOyh5q00.DiykLNudujdsc5" DeadLetterArn="arn:aws:sns:us-west-2:577898337216:CloudFormationTests" EnvironmentVariable="testing" KmsKeyArn="arn:aws:kms:us-west-2:504398934246:key/b24e7b72-a94d-6a3e-b848-165115c86212" HandlerFunctionName="lambda_function.lambda_handler" MemorySize="128" Role="arn:aws:iam::102893937243:role/serverless-test-default-us-east-1-lambdaRole" FuncName="myCLILambda"
最佳答案
您没有提供 SecurityGroupIds
和 SubnetIds
默认值,并且您没有在 --parameter-overrides
上提供它们。因此,如果没有提供值,CloudFormation 不知道如何处理它们。
添加 Default
语句应该可以解决问题:
{
"Parameters" : {
"SecurityGroupIds" : {
"Type" : "CommaDelimitedList",
"Description" : "A list of one or more security groups IDs in the VPC that includes the resources to which your Lambda function requires access.",
"Default" : ""
},
"SubnetIds" : {
"Type" : "CommaDelimitedList",
"Description" : "A list of one or more subnet IDs in the VPC that includes the resources to which your Lambda function requires access.",
"Default" : ""
}
}
关于amazon-web-services - 使用 AWS CLI 启动 CloudFormation 模板时的可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52978480/