0

Hello I am using aws ecs and terraform to deploy a few containers. I am trying to create an ecs cluster with ec2 instances and in each instance I want to run two nginx containers. I have a load balancer setup in my public subnets and I create an auto scaling group in my private subnets which I feed into my cluster. The EC2 instances are being created, the service is registered and the task is defied buy when I go to my console in ecs there is no task is running and when I go to the application load balancer dns I get a 503 not found error. I have listed the code related to ecs and the diagram of what I am trying to do. Diagram

resource "aws_launch_configuration" "ecs_launch_config" {
    image_id             = var.ami_id
    security_groups      = [aws_security_group.ecs_tasks.id]
    user_data            = var.user_data
    instance_type        = var.instance_type
    key_name = var.ssh_key_name
}

resource "aws_autoscaling_group" "asg" {
    name                      = "asg"
    vpc_zone_identifier       = aws_subnet.pt_sn[*].id
    launch_configuration      = aws_launch_configuration.ecs_launch_config.name
    desired_capacity          = 3
    min_size                  = 1
    max_size                  = 6
    health_check_grace_period = 300
    health_check_type         = "EC2"
    tag {
        key                 = "Name"
        value               = "tf_asg"
        propagate_at_launch = true
    }
}

resource "aws_ecs_capacity_provider" "cp" {
    name = "tf-ecs-cp"
    auto_scaling_group_provider {
        auto_scaling_group_arn = aws_autoscaling_group.asg.arn
    }
}

resource "aws_ecs_cluster_capacity_providers" "ccp" {
    cluster_name = aws_ecs_cluster.main_cluster.name
    capacity_providers = [aws_ecs_capacity_provider.cp.name]
    default_capacity_provider_strategy {
        capacity_provider = aws_ecs_capacity_provider.cp.name
        weight = 1
        base = 0
    }
}

resource "aws_ecs_task_definition" "task" {
    family = "tf-ecs-task"
    requires_compatibilities = ["EC2"]
    container_definitions = var.container_definition
}

resource "aws_ecs_cluster" "main_cluster" {
    name = "tf-ecs-cluster"
    tags = {
        Name = "tf-ecs-cluster"
    }
}

resource "aws_ecs_service" "main" {
    name = "tf-ecs-service"
    cluster = aws_ecs_cluster.main_cluster.id
    task_definition = aws_ecs_task_definition.task.arn
    desired_count = var.app_count
    launch_type = "EC2"
    load_balancer {
        target_group_arn = aws_alb_target_group.app.arn
        container_name = var.container_name
        container_port = var.app_port
    }
    tags = {
        Name = "tf-ecs-service"
    }
}
  • Make sure your EC2 instances can access the container repository - docker up, ECR, etc. Check if there are tasks that tried to start and couldn't, they can generate useful error messages. – Tim Aug 28 '22 at 20:49
  • I don't think the docker image is the issue. Should I post my entire code? – Hassan Kamran Sep 06 '22 at 18:23
  • I don't think so. Looks like your ALB can't access your containers, check your security groups and such. Have a look at this AWS troubleshooting guide https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/ts-elb-error-message.html#ts-elb-errorcodes-http503 and this one https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-troubleshooting.html#http-503-issues – Tim Sep 06 '22 at 19:47

1 Answers1

0

Hi so the problem was the I had not assigned the iam role for the auto scaling group, I was not using an ecs optimized image and I did not write the ecs cluster name to the ec2 config file.