【问题标题】:Optional parameters when using AWS CLI to launch CloudFormation template使用 AWS CLI 启动 CloudFormation 模板时的可选参数
【发布时间】:2018-10-28 01:44:57
【问题描述】:

我正在尝试创建一个将部署 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 中发出部署此堆栈的命令时,我收到以下错误:

调用 CreateChangeSet 时发生错误 (ValidationError) 操作:参数:[SecurityGroupIds, SubnetIds] 必须有值

这是我从模板所在的同一目录发出的 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"

【问题讨论】:

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


    【解决方案1】:

    您没有提供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" : ""
        }
    }
    

    【讨论】:

    • 当我说“参数列表”时,我指的是 CLI 命令上的 --parameter-overrides 参数。我编辑了我的答案(在接受您的编辑后)以使其更清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 2023-03-28
    • 2018-08-31
    • 1970-01-01
    • 2020-04-19
    相关资源
    最近更新 更多