簡體   English   中英

如何在 AWS CloudFormation 部署中將參數作為文件傳遞?

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

我試圖使用以下命令更新現有的 CloudFormation 堆棧。

aws cloudformation deploy

沒有通過部署選項傳遞參數文件的選項。 我們嘗試使用 --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傳遞文件中的參數

在本地文件中傳遞存儲為 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 \

和這樣的config-sandbox.json。

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

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

這可能已經太晚了,但是為了將來出現類似問題,我在( 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 對!

此問題的解決方法是使用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

您實際上可以將文件路徑傳遞給 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 表示要傳遞的參數的路徑。

我對文件有同樣的問題

最初我用過

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

這不起作用我得到的錯誤是元素應該是 Class 'Str' 而不是ordered.Dict

第二次迭代我將其更改為之前的響應中提到的也不起作用

最后我把它當作

[
  "EnvironmentStage=sandbox"
]

它運作良好

這在 buildspec 文件中對我有用:
參數結構。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

您可以根據 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
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