Sub topic 11
Exposing a service through an Application Load Balancer
This chapter assumes you know ECS services, awsvpc networking, and security groups. It shows how to put an Application Load Balancer (ALB) in front of a Fargate service so clients reach your tasks through one stable address.
Why a load balancer
Fargate tasks come and go. Each task gets its own private IP address in awsvpc mode, and that address changes when the task is replaced. An ALB gives clients one DNS name. It spreads requests across healthy tasks and sends traffic to new tasks as ECS starts them.
The pieces
The ALB setup has four parts:
- Load balancer. It lives in public subnets if it faces the internet.
- Listener. It accepts traffic on a port, such as 443, and forwards it by rule.
- Target group. It holds the destinations that receive traffic.
- ECS service. It registers and deregisters its tasks in the target group automatically.
Use the IP target type
With awsvpc networking, the target group must use target-type ip. The instance type does not work with Fargate, because tasks have no EC2 instance to register. Create the target group like this:
aws elbv2 create-target-group \
--name web \
--protocol HTTP \
--port 8080 \
--vpc-id vpc-0123456789abcdef0 \
--target-type ip \
--health-check-path /healthThe ALB checks the path you set here. A task that fails the check receives no traffic.
Connect the service
You attach the target group when you create the service. The earlier create-service command did this with --load-balancers:
containerNamemust match thenamein the container definition (web).containerPortmust match a port inportMappings(8080).--health-check-grace-period-secondsgives a new task time to start before ECS counts ALB health failures against it. The next chapter covers it in detail.
Security groups
Use two security groups so the tasks are reachable only through the ALB:
| Security group | Inbound rule |
|---|---|
| ALB | TCP 443 from `0.0.0.0/0` |
| Tasks | TCP 8080 from the ALB security group |
Referencing the ALB's security group as the source, instead of a CIDR range, keeps the rule correct as tasks and ALB nodes change addresses. The tasks run with assignPublicIp=DISABLED in private subnets. They need a NAT gateway or VPC endpoints to pull images and send logs.
Common errors
- Targets stay "unhealthy". The health check path returns a non-2xx status, or the security group blocks the ALB.
- Tasks restart in a loop. The grace period is shorter than the app's startup time, so ECS replaces tasks that were still starting.
- Registration fails at service creation. The
containerNameorcontainerPortdoes not match the task definition, or the target group is not attached to a listener. - Wrong target type. A target group created with
instancecannot be used with awsvpc tasks.
Exercise
Your Fargate service uses awsvpc networking. Why must its ALB target group use target type ip, and which security group source should the task's inbound rule use?
Show answer
In awsvpc mode each task has its own network interface and IP address, and Fargate has no EC2 instance to register, so targets must be IP addresses. The task security group should allow the container port with the ALB's security group as the source, not a CIDR range.