You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用aws cloudformation update-stack结合--cli-input-json时如何指定TemplateBody文件

解决CloudFormation UpdateStack用--cli-input-json时TemplateBody引用文件的问题

AWS CLI的--cli-input-json参数要求传入完整的JSON结构,其中TemplateBody字段需要直接填入模板的文本内容,并不支持像单独使用--template-body file://template.yml那样的文件引用语法。不过有两种实用的解决方式:

方式1:通过Shell命令将文件内容嵌入JSON

可以先读取模板文件的内容,再通过工具(比如jq)将内容插入到JSON结构中,示例如下:

  1. 读取模板文件内容到变量:
TEMPLATE_CONTENT=$(cat template.yml)
  1. 使用jq生成包含模板内容的JSON并执行命令:
aws cloudformation update-stack --cli-input-json "$(jq -n --arg body "$TEMPLATE_CONTENT" '{
  "StackName": "你的栈名称",
  "TemplateBody": $body,
  "Capabilities": ["CAPABILITY_IAM"],
  "Parameters": [
    {"ParameterKey": "InstanceType", "ParameterValue": "t2.micro"}
  ]
}')"

方式2:混合使用--template-body和--cli-input-json

如果只是想把其他参数(比如栈名、参数、权限)放在JSON里,模板文件单独指定,可以直接混合使用两个参数,CLI会自动合并参数(注意不要在JSON里再写TemplateBody字段):

aws cloudformation update-stack \
  --template-body file://template.yml \
  --cli-input-json '{
    "StackName": "你的栈名称",
    "Capabilities": ["CAPABILITY_IAM"],
    "Parameters": [
      {"ParameterKey": "InstanceType", "ParameterValue": "t2.micro"}
    ]
  }'

这种方式更简洁,也避免了处理大模板内容嵌入的问题。

内容的提问来源于stack exchange,提问作者voldomazta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 17:12:16