Terraform拒绝JSON template_file

时间:2019-05-08 18:59:57

标签: terraform

Terraform在计划期间拒绝以下ECS任务定义。 JSON验证并且使用内联container_definitions可以正常工作。

我已经Google搜索并阅读了一些评论,其中指出TF的JSON对象存在问题,主要与嵌套有关。我可以通过将JSON直接放在任务定义的资源块中的container_definition中来解决此问题,但我希望将其粘贴在模板文件中。

Error: Error running plan: 1 error(s) occurred:

* module.sonarqube.aws_ecs_task_definition.task: ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Memory of type int64

template_file中引用的JSON文档:

   {
        "name": "sonarqube",
        "image": "sonarqube:7.5-community",
        "memory": "2048",
        "logConfiguration": {
            "logDriver": "awslogs",
            "options": {
                "awslogs-group": "${log-group}",
                "awslogs-region": "${region}",
                "awslogs-stream-prefix": "ecs"
            }
        },
        "portMappings": {
            "hostPort": "9000",
            "protocol": "tcp",
            "containerPort": "9000"
        },
        "environment": [
            {
                "name": "sonar.jdbc.password",
                "value": "${password}"
            },
            {
                "name": "sonar.jdbc.url",
                "value": "${url}/${extra_url}"
            },
            {
                "name": "sonar.jdbc.username",
                "value": "${username}"
            }
        ]
    }

相关的TF块:

data "template_file" "task-def" {
  template = "${file("${path.module}/task-def.json")}"

  vars = {
    log-group = "/ecs/${var.cluster_name}-${var.name}"
    region    = "${var.region}"
    url       = "jdbc:postgresql://${var.rds_url}${var.extra_url}"
    username  = "${var.username}"
    password  = "${var.password}"
  }
}
resource "aws_ecs_task_definition" "task" {
  family       = "${var.name}"
  network_mode = "bridge"
  cpu          = "1024"
  memory       = "2048"

  execution_role_arn = "${var.ecs-exec-role}"

  container_definitions = "${data.template_file.task-def.rendered}"
}

```

1 个答案:

答案 0 :(得分:0)

Terraform希望Json采用有点直接的格式。解决此问题后,它将起作用:

  1. 内存大小和端口号应为整数,而不是字符串
  2. Terraform希望使用“有障碍物的数组”,而不是JSON“对象”
  3. 变量$ extra_url未导入template_file.task-def

task-def.json 的固定版本,已在 terraform v0.11.13 provider.aws v2.9.0 上进行了测试:

[
  {
    "name": "sonarqube"
  },
  {
    "image": "sonarqube:7.5-community"
  },
  {
    "memory": 2048
  },
  {
    "logConfiguration": {
      "logDriver": "awslogs",
      "options": {
        "awslogs-group": "tyu",
        "awslogs-region": "tyu",
        "awslogs-stream-prefix": "ecs"
      }
    }
  },
  {
    "portMappings": [
      {
        "hostPort": 9000
      },
      {
        "protocol": "tcp"
      },
      {
        "containerPort": 9000
      }
    ]
  },
  {
    "environment": [
      {
        "name": "sonar.jdbc.password",
        "value": "${password}"
      },
      {
        "name": "sonar.jdbc.url",
        "value": "${url}/${extra_url}"
      },
      {
        "name": "sonar.jdbc.username",
        "value": "${username}"
      }
    ]
  }
]

template_file.task-def 的固定版本:

data "template_file" "task-def" {
  template = "${file("${path.module}/task-def.json")}"

  vars = {
    log-group = "/ecs/${var.cluster_name}-${var.name}"
    region    = "${var.region}"
    url       = "jdbc:postgresql://${var.rds_url}${var.extra_url}"
    username  = "${var.username}"
    password  = "${var.password}"
    extra_url  = "${var.extra_url}"
  }
}