By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I create an Application Load Balancer and then automatically register Amazon ECS tasks?

4 minute read
0

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:

  1. Open the Amazon Elastic Compute Cloud (Amazon EC2) console
  2. In the navigation pane, choose Target groups.
  3. Choose Create target group.
  4. 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.
  5. For Target group name, enter a name.
  6. For Protocol : Port, choose a protocol and port combination.
  7. For VPC, choose the virtual private cloud (VPC) that you want to attach to your target group.
  8. (Optional) For Health checks, specify a health check protocol and path.
  9. Choose Next.
  10. 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:

  1. Open the Amazon EC2 console.
  2. In the navigation pane, choose Load Balancers.
  3. Choose Create Load Balancer.
  4. On the Compare and select load balancer type page, under Application Load Balancer, choose Create.
  5. For Load balancer name, enter a name for your load balancer.
  6. For Network mapping, specify your VPC and Availability Zones or subnets.
    Note: Choose subnets from at least two different Availability Zones.
  7. For Security groups, choose the security group that you want to associate with the load balancer.
  8. For Listeners and routing, choose a protocol and port. For Default action, select your target group.
  9. Choose Create load balancer.

Use the previous Application Load Balancer to create an Amazon ECS service

Complete the following steps:

  1. Open the Amazon Elastic Container Store (Amazon ECS) console.
  2. In the navigation pane, choose Clusters, and then select your cluster.
  3. In the Services section, choose Create.
  4. In the Environment section, choose a LaunchType or CapacityProvider strategy.
  5. 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.
  6. 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.
  7. Choose Create.
  8. Choose View Service, and then choose the Tasks tab.
  9. 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:

  1. Open the Amazon EC2 console.
  2. Choose Target Groups, and then choose your target group.
  3. 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

AWS OFFICIAL
AWS OFFICIALUpdated 4 months ago