amazon-web-services - 使用 AWS CLI 启动 CloudFormation 模板时的可选参数

标签 amazon-web-services aws-lambda aws-cloudformation

我正在尝试创建一个将部署 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"

最佳答案

您没有提供 SecurityGroupIdsSubnetIds 默认值,并且您没有在 --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/

相关文章:

amazon-web-services - 如何将用于写入Timestream数据的AWS lambda函数转换为写入多测量记录

amazon-web-services - 调用 AWS lambda 函数,等待完成

amazon-web-services - 如何在 lambda cloudformation 模板中添加 vpc 配置部分

node.js - 在 ElasticBeanstalk 上运行 Grunt

amazon-web-services - AWS Lambda 似乎在完成之前就退出了

amazon-web-services - 使用亚马逊弹性mapreduce服务时如何在hadoop中包含第三方库

amazon-web-services - 使用cloudformation将弹性IP添加到ec2实例

python - 减少用于在 CloudFront 上部署的 python AWS Lambda 大小

amazon-web-services - 更改 CodeStar 项目 CloudFormation 模板中 Lambda 的 IAM 角色?

aws-cloudformation - 在 AppSync 中链接多个突变