代码之家  ›  专栏  ›  技术社区  ›  hammer89

Terraform:“解码JSON时出错:JSON:无法将字符串解组到Go struct field ContainerDefinition”aws\u ecs\u task\u definition resource

  •  0
  • hammer89  · 技术社区  · 2 年前

    我从terraform文档中复制了这个资源块,并更新了值。但是,当我触发管道时,我的错误率低于,无法解决它。

    我的版本: source=“hashicorp/aws” 版本=“~>3.0”

    我的资源:

    resource "aws_ecs_task_definition" "aws-ecs-task" {
      family                   = "${var.app_name}-task"
      network_mode             = "awsvpc"
      requires_compatibilities = ["FARGATE"]
      cpu                      = 256
      memory                   = 512
      execution_role_arn       = aws_iam_role.ecsTaskExecutionRole.arn
      task_role_arn            = aws_iam_role.ecsTaskExecutionRole.arn
      container_definitions = jsonencode([
        {
          name        = "${var.app_name}-${var.app_environment}-container"
          image       = "${var.repository_url}:latest"
          essential   = true
          environment = "${var.app_environment}"
          essential = true
          portMappings = [
            {
              containerPort = 8080
              hostPort      = 8080
            }
          ]
        }
      ])
      tags = {
        Name        = "${var.app_name}-ecs-td"
        Environment = var.app_environment
      }
    }
    

    我的错误输出:

    Error: ECS Task Definition container_definitions is invalid: Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition.Environment of type []*ecs.KeyValuePair
      on application/cluster/cluster.tf line 84, in resource "aws_ecs_task_definition" "aws-ecs-task":
      84:   container_definitions = jsonencode([
      85:     {
      86:       name        = "${var.app_name}-${var.app_environment}-container"
      87:       image       = "${var.repository_url}:latest"
      88:       essential   = true
      89:       environment = "${var.app_environment}"
      90:       essential = true
      91:       portMappings = [
      92:         {
      93:           containerPort = 8080
      94:           hostPort      = 8080
      95:         }
      96:       ]
      97:     }
      98:   ])
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   luk2302    2 年前

    根据 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition 这个 environment 需要采用以下形式:

    [
      {"name": "VARNAME", "value": "VARVAL"}
    ]
    

    在你的情况下,这会转化为

    environment = [{"name": "environment", "value": "${var.app_environment}"}]