I'm trying to create a ECS cluster with a service but I'm not able to setup the autoscaling, so no instances are started in the cluster:
service my_service was unable to place a task because no container instance met all of its requirements. Reason: No Container Instances were found in your cluster. For more information, see the Troubleshooting section.
This is my Terraform config (only relevant config):
Cluster and service
resource "aws_ecs_cluster" "my_cluster" {
name = "my_cluster"
}
resource "aws_ecs_service" "my_service" {
name = "my_service"
cluster = "${aws_ecs_cluster.my_cluster.id}"
task_definition = "${aws_ecs_task_definition.my_tf.arn}"
desired_count = 1
iam_role = "${aws_iam_role.ecs-service-role.id}"
}
Autoscaling
resource "aws_launch_configuration" "launch_config" {
name_prefix = "my_lc"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
user_data = "${data.template_file.user_data.rendered}"
security_groups = ["${aws_security_group.my_sg.id}"]
iam_instance_profile = "${aws_iam_instance_profile.ecs-instance-profile.id}"
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "autoscaling_group" {
name = "my_autoscaling_group"
max_size = 2
min_size = 1
launch_configuration = "${aws_launch_configuration.launch_config.name}"
vpc_zone_identifier = ["${aws_subnet.public.id}"]
}
data "template_file" "user_data" {
template = "${file("${path.module}/templates/user-data.sh")}"
vars {
cluster_name = "${aws_ecs_cluster.my_cluster.name}"
}
}
templates/user-data.sh
#!/bin/bash
# ECS config
{
echo "ECS_CLUSTER=${cluster_name}"
} >> /etc/ecs/ecs.config
start ecs
AMI
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
As far as I know, cluster and Autoscaling Group are linked via user_data
in Launch Configuration, but it seems it's not working.
What am I missing?