Search code examples
amazon-web-servicesterraformamazon-ecs

No instances in ECS cluster


I’m having a little weekend play with Terraform and AWS but have hit a brick wall with ECS… I think my config is right, but for some reason the EC2 instance that my task definition spawns is not binding to my cluster. My definition resource looks like this:

resource "aws_launch_configuration" "myapp" {
  name                        = "myapp-launchcfg"
    image_id                    = "ami-0acc9f8be17a41897"
    instance_type               = "t2.micro"
    associate_public_ip_address = true
    iam_instance_profile        = "${aws_iam_instance_profile.myapp.arn}"
    key_name                    = "${aws_key_pair.myapp.key_name}"

    user_data = "#!/bin/bash\necho ECS_CLUSTER=${aws_ecs_cluster.myapp.name} >> /etc/ecs/ecs.config"
}

Everything is created properly, no errors, but there are no instances running in the ECS cluster… yet there is an EC2 instance running. I’m not sure if the user_data block is right, or being executed as when I grab a screenshot of the terminal from the EC2 instance it’s sat there at a login prompt. Any ideas?


Solution

  • Thanks for the comments so far, I managed to resolve this in the end so I'll put the solution here for anyone else to stumble across.

    The main issue was that I was creating a VPC and a Subnet, but when I looked further AWS was also creating a default subnet, and because I'd made a custom one and bound to that the default wasn't getting used. Upon further inspection of the binding between the EC2 instance and subnet, the binding was for the default subnet, so in the end I had to change my initial subnet resource within Terraform from resource "aws_subnet" {} to resource "aws_default_subnet" {} to provide the settings I wanted to this default instance.

    Second, and finally, I had to modify my routing table to route all traffic from 0.0.0.0/0 to my Internet Gateway.

    Up and running now, still have a lot to learn but there you go!