4
votes

I'm trying to setup an ECS cluster using Terraform and I can't figure out how to make Terraform autoprovision an EBS volume on the ECS instance that hosts a service that requires that volume.

In my config I have:

resource "aws_ecs_task_definition" "name" {
  family = "name"
  container_definitions = "${file("task-definitions/name.json")}"

  volume {
    name = "name-storage"
    host_path = "/ecs/name-storage"
  }
}

resource "aws_ecs_service" "name" {
  name = "name"
  cluster = "${aws_ecs_cluster.main.id}"
  task_definition = "${aws_ecs_task_definition.name.arn}"
  desired_count = 1
  iam_role = "${aws_iam_role.ecs_service_role.arn}"
  depends_on = ["aws_iam_role_policy.ecs_service_role_policy"]

  load_balancer {
    elb_name = "${aws_elb.name.id}"
    container_name = "name"
    container_port = 8000
  }
}

and in the task-definitions/name.json I have:

"mountPoints": [
  {
    "sourceVolume": "name-storage",
    "containerPath": "/var/lib/cassandra"
  }
]

How do I actually get the volume on the instance that hosts this service?

1

1 Answers

2
votes

Mounting a volume inside a Docker container is something else than provisioning EBS storage. You copied configuration of the ECS service/task setup but not the setup of your ECS instance (which should be a normal EC2 instance). If you use the standard ECS optimized Amazon Machine Image some standard EBS volumes are created at setup automatically.

The question remains wether you want to mount a volume inside your Docker container while using ECS. In a lot of cases this is not needed and/or there are other preferable solutions. ECS is build with the premise that any container can run on any instance in your cluster, if you need specific data from outside of your container, this premise might not hold.