简体   繁体   English

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

[英]How to pass parameter as a file in AWS CloudFormation deploy?

I was trying to update the existing CloudFormation stack with the below command.我试图使用以下命令更新现有的 CloudFormation 堆栈。

aws cloudformation deploy

there is no option to pass parameter file with deploy option.没有通过部署选项传递参数文件的选项。 we tried to pass parameter file with --parameter-overrides but it's giving the below error.我们尝试使用 --parameter-overrides 传递参数文件,但它给出了以下错误。

value passed to --parameter-overrides must be of format Key=Value传递给 --parameter-overrides 的值必须采用 Key=Value 格式

the command we try to execute is我们尝试执行的命令是

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

is there any way to pass the parameters in file with aws cloudformation deploy有什么方法可以使用aws cloudformation deploy传递文件中的参数

Passing a parameters stored as JSON in a local file looks like:在本地文件中传递存储为 JSON 的参数如下所示:

aws cloudformation deploy \
    --stack-name ${CI_PROJECT_NAME}-${STAGE} \
    --template-file deployment.yml \
    --s3-bucket ${CI_BUCKET} \
    --role-arn ${DEPLOYMENT_ROLE} \
    --no-fail-on-empty-changeset \
    --parameter-overrides file://./env/sandbox/config-sandbox.json \

and the config-sandbox.json like this.和这样的config-sandbox.json。

{
  "Parameters": {
   
    "EnvironmentStage": "sandbox",
   
  }
}

More details: https://aws.amazon.com/fr/blogs/devops/passing-parameters-to-cloudformation-stacks-with-the-aws-cli-and-powershell/更多详细信息: https://aws.amazon.com/fr/blogs/devops/passing-parameters-to-cloudformation-stacks-with-the-aws-cli-and-powershell/

this might be too late already, but for the sake of future similar issue I found this answer on ( https://github.com/aws/serverless-application-model/issues/111 )这可能已经太晚了,但是为了将来出现类似问题,我在( https://github.com/aws/serverless-application-model/issues/111 )上找到了这个答案

The command should look like:该命令应如下所示:

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

Now this is not going to be a json file, since "parameter-overrieds" expects a Key=Value pairs!现在这不会是 json 文件,因为“参数覆盖”需要一个 Key=Value 对!

workaround for this issue is pass parameters with jq command.此问题的解决方法是使用jq命令传递参数。

yum install jq

Below is the syntax for the same.以下是相同的语法。

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

You can actually pass a file path to Cloudformation deploy --parameter-overrides.您实际上可以将文件路径传递给 Cloudformation deploy --parameter-overrides。 The below syntax worked for me:以下语法对我有用:

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

where file://path/to_parameter_file.json represents the path to the parameter you want to pass.其中 file://path/to_parameter_file.json 表示要传递的参数的路径。

I had the same issue with the files我对文件有同样的问题

Initially I had used最初我用过

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

This did not work I got the error that the elements should be of Class 'Str' and not an ordered.Dict这不起作用我得到的错误是元素应该是 Class 'Str' 而不是ordered.Dict

2nd iteration I changed it to as mentioned in the earlier responses that did not work either第二次迭代我将其更改为之前的响应中提到的也不起作用

finally I have it as最后我把它当作

[
  "EnvironmentStage=sandbox"
]

and it works well它运作良好

This worked for me in buildspec file:这在 buildspec 文件中对我有用:
Structure of parameters.json:参数结构。json:

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

and then:接着:

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

You can do like this based on aws doc:您可以根据 aws doc 执行以下操作:

https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/index.html 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
aws cloudformation create-stack  --stack-name startmyinstance  
    --template-body file://home/ec2-user/templates/startmyinstance.json
    --parameters  ParameterKey=KeyPairName,ParameterValue=MyKey ParameterKey=InstanceType,ParameterValue=t1.micro
'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM