I want to create an Application Load Balancer, and automatically register Amazon Elastic Container Service (Amazon ECS) tasks.
Resolution
Create a target group
Complete the following steps:
- Open the Amazon Elastic Compute Cloud (Amazon EC2) console.
- In the navigation pane, choose Target groups.
- Choose Create target group.
- For Choose a target type, choose Instances or IP addresses.
Important: If your service's task definition uses the awsvpc network mode for the AWS Fargate launch type, then you must choose IP addresses. Tasks that use awsvpc are associated with an elastic network interface, not an EC2 instance.
- For Target group name, enter a name.
- For Protocol : Port, choose a protocol and port combination.
- For VPC, choose the virtual private cloud (VPC) that you want to attach to your target group.
- (Optional) For Health checks, specify a health check protocol and path.
- Choose Next.
- On the Register targets page, choose Create target group.
Create an Application Load Balancer and associate your target group with it
Complete the following steps:
- Open the Amazon EC2 console.
- In the navigation pane, choose Load Balancers.
- Choose Create Load Balancer.
- On the Compare and select load balancer type page, under Application Load Balancer, choose Create.
- For Load balancer name, enter a name for your load balancer.
- For Network mapping, specify your VPC and Availability Zones or subnets.
Note: Choose subnets from at least two different Availability Zones.
- For Security groups, choose the security group that you want to associate with the load balancer.
- For Listeners and routing, choose a protocol and port. For Default action, select your target group.
- Choose Create load balancer.
Use the previous Application Load Balancer to create an Amazon ECS service
Complete the following steps:
- Open the Amazon Elastic Container Store (Amazon ECS) console.
- In the navigation pane, choose Clusters, and then select your cluster.
- In the Services section, choose Create.
- In the Environment section, choose a LaunchType or CapacityProvider strategy.
- In the Deployment configuration section, enter the following information:
For Application type, choose Service.
For Task definition, choose a task definition family and revision. If you don't have a task definition, then create one.
Important: To use a load balancer, you must use containerPort in your task definition.
For Service name, enter a service name.
For Desired tasks, enter the number of tasks.
- In the Load balancing section, enter the following information:
For Load balancer type, choose Application Load Balancer.
For Container, select the container in the task definition that you want to register as Target in the target group.
For Application Load Balancer, select Use an existing load balancer.
From the Load balancer dropdown list, select the your Application Load Balancer.
For Listener, select the Use an existing listener, and then choose the listener that you pointed to the target group.
For Target group, select the Use an existing target group.
For Target group name, choose your target group.
- Choose Create.
- Choose View Service, and then choose the Tasks tab.
- Choose the refresh icon, and then verify that your tasks are in the RUNNING state.
View your tasks registered in the target group
Complete the following steps:
- Open the Amazon EC2 console.
- Choose Target Groups, and then choose your target group.
- To view the registered targets in the target group, choose the Targets tab.
Note: To troubleshoot failed health checks for your Application Load Balancer, see How can I get my Amazon ECS tasks that use the Amazon EC2 launch type to pass the Application Load Balancer health check?
Related information
Create an Application Load Balancer
Use load balancing to distribute Amazon ECS service traffic