Terraform报Invalid multi-line string错误,如何在resource后使用heredoc?
原因说明
你收到的报错核心是代码中存在被拆分到多行的带引号字符串,和你当前贴出的aws_cloudwatch_event_target资源定义结构无关,也不需要在resource关键字后使用heredoc语法。你贴出的示例代码本身没有语法问题,报错大概率出自你实际运行的代码中某段字符串的写法不符合HCL规范。
修复步骤
- 优先排查当前
aws_cloudwatch_event_target资源的所有字符串字段,确认是否存在引号未闭合就换行的错误写法,如下是典型的错误示例:
# 错误写法:引号内字符串被拆分到两行 target_id = "my-target -id"
- 如果业务场景确实需要使用多行字符串,采用heredoc语法即可,参考写法如下(以target的input参数为例):
resource "aws_cloudwatch_event_target" "my-functions-order-machine-completion-target" { target_id = "some-target-id" rule = aws_cloudwatch_event_rule.my-functions-order-machine-completion-rule.name arn = module.lambda["myLambda"].function_arn # 多行字符串用heredoc写法 input = <<EOT { "order_type": "online", "notify_scene": "completion" } EOT }
- 若当前资源排查无问题,继续检查关联的
aws_cloudwatch_event_rule.my-functions-order-machine-completion-rule资源的所有字符串字段(如name、description、schedule_expression等)是否存在跨行未闭合的引号。 - 修复完成后执行
terraform validate命令即可校验配置语法是否正确。
内容的提问来源于stack exchange,提问作者Harry
相关产品推荐
相关产品推荐

