EBS volume integration with ECS fargate

Hi Team,

I want to attach EBS volume in my ECS fargate task, through the AWS console I’m getting option to “Configure at deployment” however not able to find this option in task definition resource of terraform registry. AWS recently announced a new feature for ECS at the start of this year, 2024, that ECS can now leverage EBS volumes as a storage option for tasks. So can you please confirm that this option is available in terraform or not?

Thanks

A search in the terraform-provider-aws GitHub repoistory an open enhancement request for it - see Amazon ECS and AWS Fargate now integrate with Amazon EBS · Issue #35279 · hashicorp/terraform-provider-aws · GitHub.

Thanks for your response. Will this implemented in coming release?

No idea, but you can vote on the GitHub issue to hopefully get it prioritized.

1 Like

Hi Team,

This feature is launched in recent release. However when I’m trying to use this, it is not configuring for me, do we any example for using this or I’m sharing some terraform code snippet, kindly review and let me know if I’m doing anything wrong here. Any help is really appreciate.

task_volumes = [
{
name = “grafana-db”
configure_at_launch = “true”
}
]

container_mount_points = [
{
container_path : “/var/lib/grafana”
source_volume : “grafana-db”
}
]

Here is the Terraform configuration from an acceptance test case that was added for this feature - see if it helps.

data "aws_caller_identity" "current" {}

data "aws_partition" "current" {}

resource "aws_ecs_cluster" "test" {
  name = "test-ecs-cluster"
}

resource "aws_ecs_task_definition" "test" {
  family = "test-ecs-task-def"
  container_definitions = <<TASK_DEFINITION
[
  {
    "cpu": 128,
    "essential": true,
    "image": "mongo:latest",
    "memory": 128,
    "name": "mongodb",
    "mountPoints": [
      {"sourceVolume": "vol1", "containerPath": "/vol1"}
    ]
  }
]
TASK_DEFINITION
  volume {
    name                = "vol1"
    configure_at_launch = true
  }
}

resource "aws_iam_role" "ecs_service" {
  name = "test-iam-role"
  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Principal": {"AWS": "*"},
            "Effect": "Allow",
            "Sid": ""
        }
    ]
}
EOF
}

resource "aws_iam_role_policy" "ecs_service" {
  name = "test-iam-policy"
  role = aws_iam_role.ecs_service.name
  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "elasticloadbalancing:*",
        "ec2:*",
        "ecs:*"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
EOF
}

resource "aws_ecs_service" "test" {
  name            = "test-ecs-service"
  cluster         = aws_ecs_cluster.test.id
  task_definition = aws_ecs_task_definition.test.arn
  desired_count   = 1
  volume_configuration {
    name = "vol1"
    managed_ebs_volume {
      role_arn   = aws_iam_role.ecs_service.arn
      size_in_gb = "8"
    }
  }
  depends_on = [aws_iam_role_policy.ecs_service]
}