You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform部署ECS任务定义时mount_points与port_mappings丢失求助

问题描述

使用Terraform部署包含两个共享EBS卷的aws_ecs_task_definition时,部署流程可完成并更新任务/容器定义,但在AWS控制台查看容器定义时,mountPoints和portMappings始终为空数组。执行terraform plan时,Terraform也未识别到已配置的挂载点和端口映射。

相关Terraform代码

locals {
  volume_name = "ebs_test"
}

resource "aws_ebs_volume" "ebs_test" {
  availability_zone = "zone"
  size = 51

  tags = {
    Name = local.volume_name
  }
}

data "aws_ecr_image" "container_1_image" {
  repository_name = "Container1"
  image_tag = "latest"
}

data "aws_ecr_image" "container_2_image" {
  repository_name = "Container2"
  image_tag = "latest"
}

resource "aws_ecs_task_definition" "test_task" {
  family = "container_family"
  requires_compatibilities = ["EC2"]
  execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
  task_role_arn = aws_iam_role.execution_role.arn
  placement_constraints {
    type = "memberOf"
    expression = "ec2InstanceId == ${module.instance.instance_id}"
  }

  volume {
    name = local.volume_name
    docker_volume_configuration {
      scope = "shared"
      autoprovision = true
      driver = "rexray/ebs"
    }
  }

  container_definitions = jsonencode([
    {
      name = "Container1"
      memory = 256
      essential = true
      image = "${var.account_id}.dkr.ecr.${var.region}.amazonaws.com/${data.aws_ecr_image.container_1_image.repository_name}:latest@${data.aws_ecr_image.container_1_image.image_digest}"
      mount_points = [
        {
          container_path = "/mnt/${local.volume_name}"
          source_volume = local.volume_name
        }
      ]
      port_mappings = [
        {
          host_port = 80
          container_port = 80
        }
      ]
    },
    {
      name = "Container2"
      memory = 256
      image = "${var.account_id}.dkr.ecr.${var.region}.amazonaws.com/${data.aws_ecr_image.container_2_image.repository_name}:latest@${data.aws_ecr_image.container_2_image.image_digest}"
      mount_points = [
        {
          source_volume = local.volume_name
          container_path = "/mnt/${local.volume_name}"
        }
      ]
      port_mappings = [
        {
          host_port = 80
          container_port = 80
        }
      ]
    }
  ])
}

异常现象

  • AWS返回的任务JSON中显示:
    "portMappings": [],
    "mountPoints": [],
    
  • terraform plan输出中未识别到挂载点和端口映射配置:
    ~ container_definitions    = jsonencode(
          ~ [
              ~ {
                  - cpu              = 0
                  ~ memory           = 256 -> 512
                  - mountPoints      = []
                    name             = "Container1"
                  - portMappings     = []
                  - systemControls   = []
                  - volumesFrom      = []
                    # (4 unchanged attributes hidden)
                },
              ~ {
                  - cpu              = 0
                  - mountPoints      = []
                    name             = "Container2"
                  - portMappings     = []
                  - systemControls   = []
                  - volumesFrom      = []
                    # (5 unchanged attributes hidden)
                },
            ] # forces replacement
        )
    

解决方案

问题根源是ECS容器定义的JSON字段要求使用驼峰式命名,而代码中使用了下划线命名(mount_points、port_mappings、container_path、source_volume、host_port、container_port),这些不符合AWS API规范的字段会被直接忽略,导致配置不生效。

需要将所有下划线命名的字段替换为对应的驼峰式字段:

  • mount_points → mountPoints
  • port_mappings → portMappings
  • container_path → containerPath
  • source_volume → sourceVolume
  • host_port → hostPort
  • container_port → containerPort

修正后的容器定义代码

container_definitions = jsonencode([
  {
    name = "Container1"
    memory = 256
    essential = true
    image = "${var.account_id}.dkr.ecr.${var.region}.amazonaws.com/${data.aws_ecr_image.container_1_image.repository_name}:latest@${data.aws_ecr_image.container_1_image.image_digest}"
    mountPoints = [
      {
        containerPath = "/mnt/${local.volume_name}"
        sourceVolume = local.volume_name
      }
    ]
    portMappings = [
      {
        hostPort = 80
        containerPort = 80
      }
    ]
  },
  {
    name = "Container2"
    memory = 256
    image = "${var.account_id}.dkr.ecr.${var.region}.amazonaws.com/${data.aws_ecr_image.container_2_image.repository_name}:latest@${data.aws_ecr_image.container_2_image.image_digest}"
    mountPoints = [
      {
        sourceVolume = local.volume_name
        containerPath = "/mnt/${local.volume_name}"
      }
    ]
    portMappings = [
      {
        hostPort = 80
        containerPort = 80
      }
    ]
  }
])

修正后重新执行terraform apply,挂载点和端口映射配置会正常同步到AWS,terraform plan也会正确识别这些字段。


内容的提问来源于stack exchange,提问作者Cogito Ergo Sum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 12:13:12