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"
}
}