How can I use a static or Elastic IP address for an Amazon ECS task on Fargate?
Last updated: 2022-08-12
I want to use a static or Elastic IP address for an Amazon Elastic Container Service (Amazon ECS) task on AWS Fargate.
You can't add a static IP address or Elastic IP address directly to a Fargate task. To use a static IP or Elastic IP with Fargate tasks, you must first create a Fargate Service with a Network Load Balancer. You can then attach the Elastic IP address of the task to the Load Balancer.
Choose one of the following options:
- To create a static IP address for a Fargate task for inbound traffic, complete the following steps in the Resolution section.
- To create a static IP address for a Fargate task for outbound traffic, create a NAT gateway. In this scenario, a static IP address is required by the downstream consumer. You must place your Fargate task on a private subnet. You can use the NAT gateway IP address for an IP allow list.
Create a network load balancer, and then configure routing for your target group
- Open the Amazon EC2 console.
- In the navigation pane, under Load Balancing, choose Load Balancers.
- Choose Create Load Balancer.
- On the Select load balancer type page, choose Create for Network Load Balancer.
- On the Create Network Load Balancer page, for Load balancer name, enter a name for your load balancer.
- For Scheme, select either Internet-facing or internal.
- For IP address type, select IPv4.
- In the Network mapping section, for VPC, select the Amazon Virtual Private Cloud (Amazon VPC) for your Fargate task.
- For Mappings, select at least one Availability Zone and one subnet for each zone.
Note: Enabling multiple Availability Zones increases the fault tolerance of your applications. For internet-facing load balancers, you can select an Elastic IP address for each Availability Zone. This provides your load balancer with static IP addresses. Or, for an internal load balancer, you can assign a private IP address from the IPv4 range of each subnet instead of letting AWS assign one for you.
- In the Listeners and routing section, keep the default listener or add another listener.
Note: The default listener accepts TCP traffic on port 80. You can keep the default listener settings, modify the protocol or port of the listener, or choose Add listener to add another listener.
- For Protocol, select your protocol.
- For Port, select your port.
- Under Default action, choose Create target group.
Note: The target group is used by the Network Load Balancer listener rule that forwards the request to the target group.
- On the Specify group details page, for Choose a target type, select IP addresses.
Note: The target type Instances isn't supported on Fargate.
- For Target group name, enter a name for your target group.
- In the Health checks section, keep the default settings.
- Choose Next.
Note: Load balancers distribute traffic between targets within the target group. When a target group is associated with an Amazon ECS service, Amazon ECS automatically registers and deregisters containers with the target group. Because Amazon ECS handles target registration, you don't need to add targets to your target group.
- On the Register targets page, choose Create target group.
- Navigate to the Create Network Load Balancer page.
- In the Listeners and routing section, for Forward to, select the target group that you created.
- Choose Create load balancer.
Create an Amazon ECS service
Create an Amazon ECS service. Be sure to specify the target group in the service definition when you create your service.
When each task for your service is started, the container and port combination specified in the service definition is registered with your target group. Then, traffic is routed from the load balancer to that container.