Sub topic 3
Exposing an ECS Service Through an Application Load Balancer
Assume you know Docker, IAM, VPC subnets, security groups, the AWS CLI, and basic HTTP health endpoints. This chapter focuses on services that receive traffic through an Application Load Balancer (ALB).
Exposing a service through an Application Load Balancer
An ALB receives client requests and forwards them to healthy ECS tasks. Create a listener, usually on port 80 or 443, and a target group whose target type is ip. Fargate tasks use awsvpc networking, so each task receives its own private IP address.
The service configuration must connect the target group to the container name and container port. Allow the ALB security group to reach the task security group on that port. Allow client traffic to reach the ALB.
Health checks and the grace period
The target group checks an HTTP path such as /health. A task can be running while its application is still starting. Set health-check-grace-period-seconds to give startup time before ECS replaces a task because of failing load balancer checks. Keep the endpoint fast and independent of optional dependencies.
Container health checks provide a second signal. A failed essential container usually stops the task. Both checks must use valid ports, paths, and commands.
Safe deployments with the circuit breaker
During a rolling update, ECS starts replacement tasks before stopping old tasks, subject to deployment limits. Enable the deployment circuit breaker with rollback so ECS stops a deployment that cannot reach a healthy state and returns to the last working task definition.
Scaling and capacity choices
Target tracking adjusts desired task count toward a metric such as average CPU utilization. Set minimum and maximum capacities, then choose cooldowns that prevent rapid oscillation.
Fargate Spot costs less but tasks can be interrupted. Use a capacity provider strategy with at least one regular Fargate task for critical services. Spot suits interruption-tolerant workers.
Storage
Fargate provides task-local ephemeral storage. It disappears when the task stops. Use Amazon EFS when multiple tasks need shared, persistent files. Configure EFS mount targets, network access, encryption, and a task-definition volume.
Troubleshooting and cost
Start with ECS service events, stopped-task reasons, CloudWatch Logs, and ALB target health. ECS Exec can open a shell in a running task when the task and IAM configuration enable Systems Manager access. Use it for diagnosis, not permanent administration.
Fargate charges for requested vCPU and memory while tasks run. Also budget for the ALB, log storage, data transfer, and EFS. Right-size tasks, remove idle capacity, and use Spot where interruptions are acceptable.
Exercise
Why can a task be running but still receive no ALB traffic?
Show answer
Its target can fail the load balancer health check, or the security groups, port mapping, listener, or target-group configuration can be incorrect.