通过 Terraform 运行“aws stepfunctions update-state-machine”时出现 Json 解析错误

问题描述

我正在关注 this question 中的答案,我尝试启用 X 射线并且它有效,我使用的代码

resource "null_resource" "enable_step_function_logging" {
  triggers = {
state_machine_arn = aws_sfn_state_machine.sfn_state_machine.arn
  }
provisioner "local-exec" {
  command = "aws stepfunctions update-state-machine --state-machine-arn ${self.triggers.state_machine_arn} --tracing-configuration enabled=true"
  }
}

现在我想启用 cloudwatch 日志记录 '--logging-configuration=xxx' 部分,但我不断收到错误消息。这是我尝试过的:

resource "null_resource" "enable_step_function_logging" {
  triggers = {
    state_machine_arn = aws_sfn_state_machine.sfn_state_machine.arn
    logs_params       = <<ParaMS
      {
        "level":"ALL","includeExecutionData":true,"destinations":[
            {
                "cloudWatchLogsLogGroup":{
                    "logGroupArn":"${aws_cloudwatch_log_group.sfn_cloudwatch_log_group.arn}:*"
                    }
                }
            ]
            }
    ParaMS
  }
  provisioner "local-exec" {
    command     = "aws stepfunctions update-state-machine --state-machine-arn ${self.triggers.state_machine_arn}  --tracing-configuration enabled=true --logging-configuration='${self.triggers.logs_params}'"
  }
}

然后当我在 terraform 中申请时,它给了我错误

Error: Error running command 'aws stepfunctions update-state-machine --state-machine-arn arn:aws:states:us-east-1:xxxxxxxxx:stateMachine:xxxxxxxxstate-machine  --tracing-configuration enabled=true --logging-configuration='      {
        "level":"ALL","destinations":[
            {
                "cloudWatchLogsLogGroup":{
                    "logGroupArn":"arn:aws:logs:us-east-1:xxx:log-group:/aws/vendedlogs/states/xxxxxxx-Logs:*"
                    }
                }
            ]
            }
'': exit status 252. Output:
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text,you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

UnkNown options: {

抱怨 aws 命令格式无效,我在网上找不到任何示例,有人可以帮忙吗?

解决方法

从未在 Windows 上使用 terraform 我有点不清楚,但我怀疑 local-exec 正在使用 cmd 而不是 bash 来运行 aws-cli。事物的转义和解释方式可能存在差异?尝试告诉 terraform 使用 bash:

  provisioner "local-exec" {
    command     = "aws stepfunctions update-state-machine --state-machine-arn ${self.triggers.state_machine_arn}  --tracing-configuration enabled=true --logging-configuration='${self.triggers.logs_params}'"
    interpreter = ["bash","-c"]
  }