Amazon’s new Application Load Balancer (ALB) combined with their EC2 Container Service (ECS) makes deploying scalable dockerized services a breeze. There are a few “gotchas” during setup, however, and a couple things aren’t as obvious as perhaps they should be. We’ll cover these non-obvious ECS setup items in this article.
A fairly common use case is to deploy your application or service as a docker container. For maximum flexibility with ECS and ALB, you’ll want to deploy it such that:
- You take advantage of dynamic host ports for your docker instances.
- You can deploy as few or as many instances of your service as you wish (on as few or as many hosts as you wish).
- Newly deployed instances of your service are automatically added to your ALB load balancer’s target group (with the correct dynamically assigned port).
Let’s Get Started
I won’t get into detail on general ECS setup, because it’s a deep topic already thoroughly covered by the AWS documentation. Instead I’ll focus in on the specific small details you need to pay attention to to achieve the goals above.
As a brief review – the high level process for getting a new service and cluster going in ECS is:
- Create a Repository
- Add a docker image to that Repository
- Create a Task Definition (which references the docker image you uploaded)
- Create a Cluster (where you define the EC2 hosts / instances)
- Create a Service in your new Cluster that uses the Task Definition you created
The configuration during steps 3 and 5 are where the dynamic, auto-load-balancing “magic” happens. Let’s cover this in a bit more detail.
Creating a Task Definition (Step 3)
During Task Definition, you’ll specify the details of one or more of the Docker containers you’ll need to provide the service. This is where we want to make sure we tell ECS that we’ll be using dynamic ports for the Docker host.
Adding a container will “slide out” a side-menu to allow you to configure the container details. For dynamic ports, the thing you’ll want to pay close attention to is the “Port mappings” section.
The “Host port” must be set to zero for dynamic mapping. The tooltip doesn’t tell you this, however this can be found deeper in the AWS documentation.
Creating a Service (Step 5)
During Service creation is when we tell ECS about our Load Balancer. Note – at present, this must be done during the initial creation of the service. You can not add it after creation by updating the service (the option is no longer available).
What you’re looking for here is the “Optional configurations” / Elastic load balancing section.
One you choose “Configure ELB” you’ll be presented with a screen to setup your load balancer (either the new Application Load Balancer or the “classic” ELB). By clicking the “Add to ELB” button on this screen, you’ll be able to configure the Local Balancer path, protocol, and target group.
If you’re new to ECS or if you’ve run into trouble with getting dynamic ports allocation to work properly with the AWS load balancers, these are the key areas. Once complete – ECS will take care of managing the Target Group for you, adding and removing targets with their dynamically allocated ports as needed or directed by your scaling policy.