How to properly use terraform to manage ecs

1,526 views
Skip to first unread message

Viktor Ershov

unread,
Oct 21, 2015, 7:38:13 AM10/21/15
to Terraform
Hey guys

I have a very simple config 

resource "aws_instance" "ecs_scraper_worker" {
    count
= 2
    key_name = "scraper"
    ami = "ami-d0b9acb8"
    instance_type = "c4.large"
    iam_instance_profile = "ecsInstanceRole"
}

resource
"aws_ecs_task_definition" "scraper" {
    family
= "scraper"
    container_definitions = "${file("etc/scraper-ecs-task-definition.json")}"
}

resource
"aws_ecs_service" "scraper" {
    depends_on
= ["aws_instance.ecs_scraper_worker"]
    name
= "scraper"
    cluster = "arn:aws:ecs:us-east-1:027466452922:cluster/default"
    task_definition = "${aws_ecs_task_definition.scraper.arn}"
    desired_count = 2
}

some things are hardcoded since they already configured and I don't need terraform to manage them.

All I need - create some instances, run and from time to time update task on these instances.

But I have 2 questions:

1. After instances are created it takes some time for them to join the cluster, and when service is created and tries to find appropriate instances in specified cluster, there are not instances in cluster yet. I tried to add depends_on into service config, but it didn't help since it needs more time to wait until instances are added to cluster. How can I deal with this?

2. From time to time I need to update the docker image which is run on my instances. Without terraform I would stop running task, create new task revision and run new revision on my instances. But I didn't find how to create new revision with terraform, the only way I found is to destroy everything and then re-create the whole environment which results in new task revision. Is it possible to achieve the same result without destroying the whole environment?

Thanks!

Paul Hinze

unread,
Oct 29, 2015, 3:19:57 PM10/29/15
to terrafo...@googlegroups.com
1. After instances are created it takes some time for them to join the cluster, and when service is created and tries to find appropriate instances in specified cluster, there are not instances in cluster yet. I tried to add depends_on into service config, but it didn't help since it needs more time to wait until instances are added to cluster. How can I deal with this?

Are the instances not ready to participate in the cluster until after cloud-init has completed? If so, you could add a "remote-exec" provisioner to wait until cloud-init completes.

  provisioner "remote-exec" {
    inline = ["while sudo pkill -0 cloud-init 2>/dev/null; do sleep 2; done"]
    connection {
      user = "..."
      host = "${self.public_ip}"
    }
  }

2. From time to time I need to update the docker image which is run on my instances. Without terraform I would stop running task, create new task revision and run new revision on my instances. But I didn't find how to create new revision with terraform, the only way I found is to destroy everything and then re-create the whole environment which results in new task revision. Is it possible to achieve the same result without destroying the whole environment?

I'm not familiar with the ECS model offhand, but if there is a way you can think of to model this in declarative terraform config, we'd welcome a GitHub issue with a proposed config format and this use case description.

Paul


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/196b590b-174b-4108-8e0b-b21fb0d0e623%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages