skip to Main Content

I am trying to deploy an aws_ecs_task_definition with two containers that share an EBS volume. Terraform will successfully run the deployment and update the task/container definitions but port_mappings and mount_points are never present in the container definitions when view in AWS.

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
        }
      ]
    }
  ])
}

This is what is always present in the task JSON pulled from AWS:

"portMappings": [],
"mountPoints": [],

Terraform apparently isn’t seeing the port_mappings or mount_points properties being set when checking 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
        )

I have been trying to use this GitHub repo is a reference: https://github.com/markgllin/ecs_with_ebs

What am I missing?

2

Answers


  1. Chosen as BEST ANSWER

    Apparently it was simply the format for the strings. They needed to be camel-case and not under bar style. mount_points -> mountPoints etc.


  2. You are using the names mount_points and port_mappings in your Terraform code, but Terraform is looking for the names mountPoints and portMappings (as you can see in the Terraform plan output).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search