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

Terraform local-exec执行AWS CLI命令报错,求命令修正方法

解决Terraform local-exec命令的换行解析错误

原代码执行时触发的错误:

Error: Missing newline after argument
on main.tf line 9, in resource "null_resource" "enable-usage-plan":
9: command = "aws apigateway update-usage-plan --usage-plan-id sx8lzu --patch-operations op="add",path="/apiStages",value="7tdnnlppg1:poc""
An argument definition must end with a newline.

该命令在AWS CLI中可正常运行,问题源于HCL解析器对单行转义字符串的处理歧义,可通过以下两种方式修改:

方案一:使用Heredoc多行字符串格式

无需转义引号,命令结构更清晰,同时满足Terraform对参数换行的要求:

resource "null_resource" "enable-usage-plan" {
  triggers = {
    usage_plan_id = "cd8lab"
  }

  provisioner "local-exec" {
    command = <<EOF
aws apigateway update-usage-plan --usage-plan-id cd8lab --patch-operations op="add",path="/apiStages",value="7tdnnlppg1:poc"
EOF
  }
}

方案二:使用单引号包裹命令

用单引号包裹整个命令字符串,避免内部双引号的转义需求,让HCL解析器正确识别完整参数:

resource "null_resource" "enable-usage-plan" {
  triggers = {
    usage_plan_id = "cd8lab"
  }

  provisioner "local-exec" {
    command = 'aws apigateway update-usage-plan --usage-plan-id cd8lab --patch-operations op="add",path="/apiStages",value="7tdnnlppg1:poc"'
  }
}

说明

两种方案均保留原AWS CLI命令的逻辑,可解决HCL解析歧义问题,任选其一即可正常执行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 23:25:33