Complete the code to define an ECS cluster.
resource "aws_ecs_cluster" "example" { name = "[1]" }
The ECS cluster name is set to "example-cluster" to identify the cluster.
Complete the code to create an Application Load Balancer (ALB).
resource "aws_lb" "app_alb" { name = "app-alb" internal = false load_balancer_type = "[1]" subnets = var.subnet_ids }
The ALB type must be set to "application" for ECS integration.
Fix the error in the ECS task definition to specify the container port mapping.
container_definitions = jsonencode([
{
"name": "web",
"image": "nginx",
"portMappings": [
{
"containerPort": [1],
"hostPort": 0,
"protocol": "tcp"
}
]
}
])The container port for a web server like nginx is typically 80.
Fill both blanks to define a listener and target group for the ALB.
resource "aws_lb_target_group" "app_tg" { name = "app-tg" port = [1] protocol = "[2]" vpc_id = var.vpc_id }
The target group listens on port 80 using HTTP protocol for typical web traffic.
Fill all three blanks to configure the ECS service with ALB integration.
resource "aws_ecs_service" "app_service" { name = "app-service" cluster = aws_ecs_cluster.example.id task_definition = aws_ecs_task_definition.app_task.[1] desired_count = 2 launch_type = "FARGATE" network_configuration { subnets = var.subnet_ids security_groups = [aws_security_group.app_sg.id] assign_public_ip = true } load_balancer { target_group_arn = aws_lb_target_group.app_tg.[2] container_name = "web" container_port = [3] } }
The ECS service requires the task definition revision, the target group ARN, and the container port 80 for ALB integration.
