简体   繁体   English

使用 for_each 将变量传递给模块时,jsondecode 失败

[英]jsondecode fails when using for_each to pass variables to module

I'm trying to use for_each with a terraform module creating datadog synthetic tests.我正在尝试将 for_each 与创建数据狗综合测试的 terraform 模块一起使用。 The object names in an s3 bucket are listed and passed as the set for the for_each.列出 s3 存储桶中的对象名称并作为 for_each 的集合传递。 The module reads the content of each file using the each.value passed in by the calling module as the key.模块使用调用模块传入的 each.value 作为键读取每个文件的内容。 I hardcoded the s3 object key value in the module during testing and it was working.我在测试期间对模块中的 s3 对象键值进行了硬编码,它正在工作。 When I attempt to call the module from main.tf, passing in the key name dynamically from the set it fails with the below error.当我尝试从 main.tf 调用模块时,从集合中动态传递键名它失败并出现以下错误。

│ Error: Error in function call
│
│   on modules\Synthetics\trial2.tf line 7, in locals:
│    7:   servicedef  = jsondecode(data.aws_s3_object.tdjson.body)
│     ├────────────────
│     │ data.aws_s3_object.tdjson.body is ""
│
│ Call to function "jsondecode" failed: EOF.

main.tf主文件

data "aws_s3_objects" "serviceList" {
  bucket = "bucketname"
}
module "API_test" {
  for_each = toset(data.aws_s3_objects.serviceList.keys)
  source  = "./modules/Synthetics"
  
  S3key   = each.value
  }

module模块

data "aws_s3_object" "tdjson" {
  bucket = "bucketname"
  key    = var.S3key
}

locals {
  servicedef  = jsondecode(data.aws_s3_object.tdjson.body)
  Keys        = [for k,v in local.servicedef.Endpoints: k]

}

Any clues as to what's wrong here?关于这里有什么问题的任何线索?

Thanks谢谢

Check out the note on the aws_s3_object data source :查看有关aws_s3_object 数据源的注释:

The content of an object ( body field) is available only for objects which have a human-readable Content-Type ( text/* and application/json ).对象的内容( body字段)仅适用于具有人类可读Content-Typetext/*application/json )的对象。 This is to prevent printing unsafe characters and potentially downloading large amount of data which would be thrown away in favour of metadata.这是为了防止打印不安全的字符并可能下载大量数据,这些数据将被丢弃以支持元数据。

Since it's successfully getting the data source (not throwing an error), but the body is empty, this is very likely to be your issue.由于它成功获取了数据源(没有抛出错误),但正文为空,这很可能是您的问题。 Make sure that your S3 object has the Content-Type metadata set to application/json .确保您的 S3 对象将Content-Type元数据设置为application/json Here's a Stack Overflow question/answer on how to do that via the CLI; 这是有关如何通过 CLI 执行此操作的 Stack Overflow 问题/答案 you can also do it via the AWS console, API, or Terraform (if you created the object via Terraform).您也可以通过 AWS 控制台、API 或 Terraform(如果您通过 Terraform 创建对象)来执行此操作。

EDIT: I found the other issue.编辑:我发现了另一个问题。 Check out the syntax for using for_each with toset :查看将for_eachtoset一起使用的语法:

resource "aws_iam_user" "the-accounts" {
  for_each = toset( ["Todd", "James", "Alice", "Dottie"] )
  name     = each.key
}

The important bit is that you should be using each.key instead of each.value .重要的是您应该使用each.key而不是each.value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM