如何在AWS CloudFormation部署中将参数作为文件传递?

15

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

aws cloudformation deploy

在使用deploy选项时,没有传递file参数的选项。我们尝试使用--parameter-overrides参数传递file参数,但是出现以下错误:

--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 是否有办法在文件中传递参数?


看起来这里有类似的问题和解决方法:https://github.com/aws/aws-cli/issues/3274#issuecomment-498812550 - Pat Myron
1
@PatMyron 谢谢 - mahendra rathod
7个回答

14

将存储为JSON格式的参数传递到本地文件中看起来像:

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

而且test.json是这样的。

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

测试.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之后,它就可以工作了。 - Daniel Persson
8
即使使用aws-cli/2.0.30,这个方法也无效。--parameter-overrides参数中传递的['{']值必须符合Key=Value的格式。 - PRF

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

2
您实际上可以将文件路径传递给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 代表你想传递的参数文件路径。


这个也适用于yaml吗?:) - undefined

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文件,因为“参数重载”需要键=值对!

这里的行尾会发生什么?难道你不需要像 $(cat params.properties | tr "\n" " ") 这样的命令来将行尾替换为空格吗? - Xabs
@Xabs,是的,那是必要的,谢谢!不过,我已经手动向props文件中添加了一个新行来避免这种情况 :) - Dean
非常感谢你,兄弟 ;) - Joabe Lucena

1

我在文件方面遇到了同样的问题

最初我使用了

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

这个没用,出现了一个错误,说元素应该是 'Str' 类型而不是有序字典

在第二次尝试中,我按照之前的回复所提到的进行了更改,但也没有成功

最终我将它改为以下内容:

[
  "EnvironmentStage=sandbox"
]

它运行良好


感谢@DataGuru,展示了正确的格式。我还测试了几种参数json文件的格式。在所有三种不同的支持格式中,对我来说,这是最简单的一种:["key1=vlaue1", "key2=value2", ...] - undefined

0

这个在我的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

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接