【问题标题】:How to pass parameter as a file in AWS CloudFormation deploy?如何在 AWS CloudFormation 部署中将参数作为文件传递?
【发布时间】:2020-10-12 18:00:44
【问题描述】:

我正在尝试使用以下命令更新现有的 CloudFormation 堆栈。

aws cloudformation deploy

没有通过 deploy 选项传递参数 file 的选项。我们尝试使用 --parameter-overrides 传递参数文件,但它给出了以下错误。

传递给 --parameter-overrides 的值必须采用 Key=Value 格式

我们尝试执行的命令是

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides ip.json --no-execute-changeset

有没有办法通过 aws cloudformation deploy

传递文件中的参数

【问题讨论】:

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

在本地文件中传递以 JSON 格式存储的参数如下所示:

aws cloudformation deploy \
    --stack-name demo \
    --template-file test.yml --parameter-overrides file://test.json  \

和这样的 test.json。

{
    "Parameters": {
      "BucketName": "myawesometestdemo"
    }
}

test.yml

---
AWSTemplateFormatVersion: '2010-09-09'
Description: Simple S3 Bucket test
Parameters:
  BucketName:
    Type: String
    Description: The name of the S3 Bucket to create

Metadata:

  AWS::CloudFormation::Interface:
    ParameterLabels:
      BucketName:
        default: S3 Bucket Name

Resources:

  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName

aws cli 版本:

aws --version
aws-cli/2.2.35 Python/3. XXX

【讨论】:

  • 起初这对我不起作用。事实证明,我仍然安装了 AWS CLI v1。更新到 v2 后,它工作了。
  • 即使使用 aws-cli/2.0.30 ['{'] value passed to --parameter-overrides must be of format Key=Value 也无法正常工作
【解决方案2】:

这可能已经太晚了,但为了将来出现类似问题,我在 (https://github.com/aws/serverless-application-model/issues/111) 上找到了这个答案

命令应该是这样的:

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides $(cat params.properties) --no-execute-changeset

现在这将不是一个 json 文件,因为“参数覆盖”需要一个 Key=Value 对!

【讨论】:

  • 这里的行尾会发生什么?你不需要像$(cat params.properties | tr "\n" " ") 这样的东西来用空格替换行尾吗?
  • @Xabs,你需要,谢谢!!但是,我确实已经在 props 文件中手动添加了一个新行以避免这种情况:)
  • 巨大的 thabks 兄弟 ;)
【解决方案3】:

解决此问题的方法是使用 jq 命令传递参数。

yum install jq

下面是相同的语法。

aws cloudformation deploy --template-file sg.yml --stack-name Common-SG --parameter-overrides $(jq -r '.[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ip.json) --no-execute-changeset

【讨论】:

    【解决方案4】:

    您实际上可以将文件路径传递给 Cloudformation deploy --parameter-overrides。以下语法对我有用:

    aws cloudformation deploy \
      --template-file template.yml \
      --stack-name my-stack \
      --parameter-overrides file://path/to_parameter_file.json
    

    其中 file://path/to_parameter_file.json 表示要传递的参数的路径。

    【讨论】:

      【解决方案5】:

      我对文件有同样的问题

      一开始我用过

      [
        {
          "ParameterKey": "EnvironmentStage",
          "ParameterValue": "sandbox"
        }
      ]
      

      这不起作用,我得到了元素应该属于“Str”类而不是ordered.Dict的错误

      第二次迭代我将其更改为之前的响应中提到的也不起作用

      终于有了

      [
        "EnvironmentStage=sandbox"
      ]
      

      效果很好

      【讨论】:

        【解决方案6】:

        这在 buildspec 文件中对我有用:
        parameters.json的结构:

        [
          {
            "ParameterKey": "Key1",
            "ParameterValue": "Value1"
          }
        ]
        

        然后:

        post_build:
                commands:
                    - echo "Start build..."
                    - aws cloudformation deploy --template-file ./template.yaml --parameter-overrides $(jq -r '.[] | [.ParameterKey, .ParameterValue] | "\(.[0])=\(.[1])"' ./parameters/parameters.json) --stack-name ${stackName} --capabilities CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND
        

        【讨论】:

          【解决方案7】:

          您可以根据 aws doc 这样做:

          https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html

          aws cloudformation deploy --template-file /path_to_template/template.json --stack-name my-new-stack --parameter-overrides Key1=Value1 Key2=Value2
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2022-01-22
            • 2019-09-05
            • 2018-11-15
            • 2021-05-09
            • 1970-01-01
            • 2019-10-07
            • 2019-02-05
            • 2016-09-08
            相关资源
            最近更新 更多