Can two Amazon ECS services share one Application Load Balancer (ALB) and still have independent public routes?
In my EKS series, we’ve been running containers on Kubernetes. This time we’ll use ECS and Fargate to deploy two services, catalog and orders. We’ll check that each service gets its own HTTPS hostname on the same ALB, then remove the routes one at a time to see what happens.
You can follow along with the CloudFormation template and lab scripts.
What Amazon ECS Gateway does
AWS introduced ECS Gateway in October 2026 to manage how incoming requests reach ECS services. If you want the services to discover and communicate with each other, that’s what ECS Service Connect is for. This lab is about traffic coming in. ECS Gateway launch announcement · ECS Service Connect
We could already share an ALB by configuring listener rules for each hostname. With ECS Gateway, ECS does that work when a service connects and cleans it up when the service disconnects. We’ll associate a gateway with our cluster, but that alone won’t expose anything. Each service opts in by adding a gateway connector that points to a named port in its task definition. ALB host-based routing · Create and manage a gateway
When the first compatible service connects, ECS creates the shared ALB and sets up the service’s hostname, certificate, listener rule, and target group. The next compatible service can reuse the same ALB and listener. It still gets its own hostname and route, so the ALB knows which target group should receive each request. Connect services to a gateway
In this lab, the ALB handles HTTPS and forwards requests to the containers over HTTP on port 80.
Prerequisites
- An authenticated AWS CLI v2 with the ECS Gateway commands. I ran the lab in
us-east-1. - Permission to deploy CloudFormation, create the lab resources and IAM roles, and pass those roles to ECS.
- Git, Bash, and curl. On Windows, Git Bash works for the commands below.
This lab creates public demo endpoints. Don’t put sensitive data in them. Fargate, the ALB, public IPv4 addresses, and log usage can incur charges. Use a disposable stack and delete it when you’re done. Give yourself about half an hour for the walkthrough. The first ALB and the disconnect phases can take several minutes.
1. Create a gateway without exposing any services
git clone https://github.com/Femi-lawal/ecs-gateway-shared-alb-lab.git
cd ecs-gateway-shared-alb-lab
export AWS_REGION=us-east-1
export STACK_NAME=ecs-gateway-lab
aws sts get-caller-identity --region "$AWS_REGION" --no-cli-pager
Check the Account value against the AWS account you intend to use for the lab. In that account, open CloudFormation → Stacks in the Region set by AWS_REGION and confirm that the stack name is unused. If it’s already in use, set STACK_NAME to a different, unused name before continuing. CloudFormation updates an existing stack with that name instead of creating a second one. AWS CloudFormation deploy reference
Each apply brings the same lab stack to the state defined by the selected phase. The phases aren't additive. For example, applying catalog after both removes orders because that service isn't part of the catalog phase.
./lab.sh apply base
./lab.sh status
You don't need to create a VPC or cluster beforehand. lab.sh calls aws cloudformation deploy with Phase=base. The resource definitions are in template.yaml.
If the deploy fails. If the stack appears in CloudFormation, open its Events tab. Look for the resource-level CREATE_FAILED or UPDATE_FAILED event that triggered the rollback and read its Status reason. The rollback message for the whole stack tells you what CloudFormation did, not which resource caused it. View CloudFormation stack events
Stopping early? If CloudFormation created your lab stack, you can run ./lab.sh delete once any in-progress deployment or rollback finishes. Check the account, Region, and stack name again first. You don’t need to reach Step 3 or save any URLs or ARNs. The identifiers in the later steps are for verifying cleanup, not for running teardown.
What CloudFormation creates
Here are the cluster and gateway association from the template's Resources section.
Cluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Ref AWS::StackName
GatewayAssociation:
Type: AWS::ECS::GatewayClusterAssociations
Properties:
Gateway: !GetAtt Gateway.Arn
Cluster: !GetAtt Cluster.Arn
-
Cluster.
!Ref AWS::StackNamenames the cluster after the stack. The cluster groups ECS services and tasks in a Region. When we add a service in the next step, the service selects Fargate to run its tasks and supplies the subnets they run in. There's no EC2 worker fleet to create for this lab. Amazon ECS clusters - Network. The template creates a VPC and two public subnets in different Availability Zones. The gateway and tasks use the same subnets here for simplicity. They don't have to use identical subnets, but they do need to be in the same VPC, and the gateway must cover every Availability Zone the service is configured to use. How ECS selects compatible gateways
-
Gateway and association. The gateway is internet-facing, with
MaxAlbCount: 1for this experiment. The infrastructure role gives ECS permission to manage the ALB and related ingress resources. The association makes the gateway available to services in this cluster. It doesn't expose any service or create an ALB on its own. Create and manage an ECS Gateway
In the ECS console, select your deployment Region, open Clusters, and choose the cluster named after your stack. There should be no services yet. Then open Gateways and choose the gateway with the same name. Its Associated clusters tab should show an active association, while Load balancers and Gateway routes should both show zero. That's our starting point.
Picking up after a break? Restore the lab’s AWS_REGION and STACK_NAME values if you opened a new shell. Read the stack status and its Phase parameter before choosing what to run next.
aws cloudformation describe-stacks \
--stack-name "$STACK_NAME" \
--region "$AWS_REGION" \
--query "Stacks[0].{Status:StackStatus,Phase:Parameters[?ParameterKey=='Phase'].ParameterValue | [0]}" \
--output table --no-cli-pager
Once the stack is stable and the gateway exists, run ./lab.sh status to check the actual routes and ALBs. Resume from what you see rather than replaying apply base. If you lost the saved catalog URL and haven’t detached it yet, run catalog_url="$(./lab.sh url catalog)" before Step 4. If you’ve already detached it, skip the old-URL probe and verify the remaining route and both service counts instead.
2. Connect the first service
Now that the gateway is associated with the cluster, we can connect catalog.
./lab.sh apply catalog
./lab.sh status
./lab.sh probe catalog
There are two pieces to this setup. The task definition tells ECS what to run, including the Nginx container and its port. The service tells ECS where to run it and how many copies to keep running. We’ll start with one Fargate task.
In CatalogTask, we give port 80 the name web.
NetworkMode: awsvpc
ContainerDefinitions:
- Name: web
# Image and startup command omitted here.
PortMappings:
- Name: web
ContainerPort: 80
AppProtocol: http
The Catalog service uses that task definition and refers to web in the connector.
Cluster: !Ref Cluster
TaskDefinition: !Ref CatalogTask
LaunchType: FARGATE
DesiredCount: 1
GatewayConnectors: !If [ConnectCatalog, [{ PortName: web }], []]
PortName: web matches the port mapping's name, not the container name or the port number. The ConnectCatalog condition is true in this phase, so the service opts in. Connect ECS services to a gateway
The service's network configuration uses the two lab subnets and gives the tasks public IPs so they can pull the image and send logs without a NAT gateway. The task security group in the template has no inbound rules. ECS separately manages the rule that allows traffic from the ALB. The task execution role is what ECS uses to send the container logs to CloudWatch. It's separate from the gateway infrastructure role.
Why an active route can still return HTTP 503
My first probe returned HTTP 503 even though the route was ACTIVE. ECS had finished setting up the route, but a healthy target wasn't ready to serve traffic yet. I checked the route, the target, and the request separately.
-
Route. Run
./lab.sh statusand check that the route forcatalogisACTIVE. That tells you the ingress is provisioned, not that Nginx is ready. -
Target. Run
./lab.sh url catalogto get the service hostname. Find the ALB under the gateway’s Load balancers tab, then open its HTTPS listener in the EC2 console. Find the rule for that hostname, follow its forward action to the target group, and check for aHealthytarget on the Targets tab.
If no target has registered, check the catalog service’s Events in ECS. If a target stays unhealthy, check Status details. The default health check in this lab expects HTTP 200 from / on port 80.
-
Request. Retry
./lab.sh probe catalog. A200with the bodycatalogis the end-to-end check. At this point the gateway reports one ALB.
Monitor an ECS Gateway · Check ALB target health
Me, waiting for the first target to pass its health checks. Refreshing the console does not count as a health check. I checked.
GIF by cintascotch on GIPHY.
3. Connect orders and prove the ALB is shared
Before connecting orders, save the ARN of the ALB created for catalog. We’ll compare it after the second service connects and look up that exact ALB during cleanup. Use the same shell for the rest of the lab so the saved ARN and catalog URL are still available.
shared_alb_arn="$(
aws ecs describe-gateway-resources \
--gateway "$STACK_NAME" \
--region "$AWS_REGION" \
--query 'gatewayResources.resources.hostSharedAlb[0].loadBalancerArn' \
--output text --no-cli-pager
)"
printf 'ALB before orders: %s\n' "$shared_alb_arn"
If the lookup returns None, use ./lab.sh status to check whether the first ALB has been created, then retry the lookup. You don’t need to reapply the previous phase.
./lab.sh apply both
./lab.sh status
./lab.sh probe catalog
./lab.sh probe orders
catalog_url="$(./lab.sh url catalog)"
We should get HTTP 200 from both URLs, with catalog in one response body and orders in the other. That tells us each hostname reached the right service. To confirm they used the same ALB, check how ECS groups the routes in ./lab.sh status. This is the command behind that part of the output.
aws ecs describe-gateway-resources \
--gateway "$STACK_NAME" \
--region "$AWS_REGION" \
--query 'gatewayResources.resources.hostSharedAlb[].{ALB:loadBalancerArn,Routes:gatewayRouteArns}' \
--output json --no-cli-pager
Here’s the shape of the result, with the ARNs redacted.
[
{
"ALB": "[shared ALB ARN]",
"Routes": ["[first route ARN]", "[second route ARN]"]
}
]
We should see one ALB entry with two route ARNs. Compare the ALB ARN with $shared_alb_arn. If they match, orders joined the same ALB that was already serving catalog. Use the AWS-generated service URLs for the probes, since the ALB needs the service hostname to pick the right rule. Monitor an Amazon ECS Gateway
Now open the shared ALB’s HTTPS listener and look at Rules. Each service hostname has its own rule, and each rule forwards to a different target group.
Two services, one ALB. Even my containers have figured out how to split the rent.
GIF via Nick at Nite on GIPHY.
4. Disconnect catalog without deleting it
Before you run this phase, make a prediction. What will happen to the old catalog URL? Will orders still answer? How many tasks will ECS report for each service?
./lab.sh apply orders-only
./lab.sh status
In the orders-only phase, ConnectCatalog becomes false, so the same service resource now gets GatewayConnectors: []. That removes the connector without changing the service’s desired task count. It starts an ECS deployment, so check ./lab.sh status again if the old route is still being removed. Once the gateway lists only the active orders route, check the service counts and the surviving endpoint.
aws ecs describe-services \
--cluster "$STACK_NAME" \
--services catalog orders \
--region "$AWS_REGION" \
--query 'services[].{Service:serviceName,Desired:desiredCount,Running:runningCount}' \
--output table --no-cli-pager
./lab.sh probe orders
At steady state, both rows should show Desired and Running as 1. The probe should return orders and HTTP 200. Finally, try the old URL you saved in Step 3.
curl -i --max-time 15 "$catalog_url"
In my run, the old catalog hostname stopped resolving, while orders still returned HTTP 200. ECS reported one running task for each service. We removed a public route without deleting the service behind it. The error you get from the old URL can depend on DNS caching and cleanup timing. This is a check after the deployment, not a test for downtime during it. AWS CLI service details
Switch to the ECS cluster’s Services tab and check the task counts. The gateway no longer lists a route for catalog, but ECS still shows one running task for each service.
5. Remove the last route and clean up
./lab.sh apply no-ingress
./lab.sh status
With neither service connected, ECS removes the unused managed ALB. The association remains active, and you can repeat the service count check to verify that both services still have running tasks. If the ALB count isn’t zero immediately, check again after cleanup catches up. Don’t manually edit the managed listeners or remove the tags ECS uses to manage them.
A zero ALB count is not the same thing as a deleted stack. The tasks and the rest of the lab are still there until you delete the CloudFormation stack.
The only thing that should outlive this lab is the article, not the bill.
Save the stack’s unique ID before deleting it. CloudFormation lets you look up a deleted stack by ID, not by name.
stack_id="$(
aws cloudformation describe-stacks \
--stack-name "$STACK_NAME" \
--region "$AWS_REGION" \
--query 'Stacks[0].StackId' \
--output text --no-cli-pager
)"
./lab.sh delete
The script waits for CloudFormation to finish deleting the stack. The lab disables gateway deletion protection so the gateway can be removed. AWS enables it by default. Gateway deletion protection
I checked both the stack and the ALB. This first command should print DELETE_COMPLETE.
aws cloudformation describe-stacks \
--stack-name "$stack_id" \
--region "$AWS_REGION" \
--query 'Stacks[0].StackStatus' \
--output text --no-cli-pager
If you saved the ALB ARN in Step 3, this command should return LoadBalancerNotFound. That confirms the original ALB is gone. An access error is not confirmation. If you lost the ARN, skip this lookup and use the zero ALB count you checked before deleting the stack.
aws elbv2 describe-load-balancers \
--load-balancer-arns "$shared_alb_arn" \
--region "$AWS_REGION" --no-cli-pager
Look up a deleted CloudFormation stack · LoadBalancerNotFound in Elastic Load Balancing
Conclusion
We started with a gateway and no ALB. ECS created the ALB when catalog connected, reused it for orders, and removed it after the last route was gone. The useful part is that the platform team can set up the gateway once, while each application team decides which service port to expose.
One task per service was enough for this lab, but two subnets don’t give a single task a replica to take over. For a service that needs to tolerate an Availability Zone outage, run multiple tasks and verify that ECS spreads them across Availability Zones. We didn’t test failover here.
Trying this on an existing service. Use the service’s existing infrastructure configuration instead of pointing the lab script at it. Confirm an eligible gateway is associated with the cluster, keep the existing ALB configuration in place, and add a connector for the service’s named port. This starts a deployment and adds a second ingress path. Once the gateway route is active, test its generated HTTPS URL before changing the original path. A connector can attach the service to every compatible gateway on that cluster. It doesn’t select one by name, so check which gateways are eligible and whether their access types match what you intend to expose. Connect and migrate existing ECS services
Sharing matters operationally too. Metrics for the whole ALB combine traffic from the services using it. To troubleshoot one app, start with that service’s target group and route. For the shared infrastructure, monitor the ALB itself. Gateway traffic and target health









Top comments (0)