地形:"Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition" aws_ecs_task_definition 资源

Terraform : "Error decoding JSON: json: cannot unmarshal string into Go struct field ContainerDefinition" aws_ecs_task_definition resource

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

我的版本: 源=“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:   ])

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

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

在你的情况下,这将转化为类似

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