The EC2 host port that can be allocated as the docker published ports (dynamic ports) are ephemeral port ranges: 49153–65535 and 32768–61000 according to the document.
It looks to me they are over-wrapping (49153-61000?), but it is what the document says. Should be 32768-65535? Please someone explain why.
- How do I set up dynamic port mapping for Amazon ECS?
If dynamic port mapping is set up correctly, then you'll see the registered targets in the target group and the assigned port for the task. You'll also see the task in the registered targets for the following ephemeral port ranges: 49153–65535 and 32768–61000.
For instance, you can see port 32776 is allocated dynamically for a ECS task container.
$ sudo status ecs
ecs start/running, process 3176
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6b95c013c6ba *****.dkr.ecr.us-east-2.amazonaws.com/\ecs_monolith_myecs/xyz "node server.js" 3 minutes ago Up 3 minutes 0.0.0.0:32776->3000/tcp ecs_monolith_ABC-1-XYZ-feb4d68dd3d2bfad7500
dbe356e8de8c amazon/amazon-ecs-agent:latest "/agent" 45 minutes ago Up 45 minutes (healthy) ecs-agent
Hence we can open up 49153–65535 and 32768–61000 (or 32768-65535) for incoming traffic from the ELB targat groups which the ECS service is updating for the dynamic port mapping.
Using Security Group (SG) of ALB
For ALB, we can limit the traffic from the SG attached to ALB. But NLB has no SG the reason of which seems to be NLB is not running on EC2, hence no SG and as ALB runs on EC2, hence with SG.
NLB appears in your VPC as an ENI but it actually isn't. It's an extension of a component we have called HyperPlane. Whereas ALB and CLB are running on EC2 instances managed by AWS so they have "real" ENIs attached to them hence being able to use security groups.
Using ELB IP CIDR Blocks
NLB have a static IP. However, if the target type of the target group of ELB is instance, then the source IP is preserved as in Target Groups for Your Network Load Balancers.
If you specify targets using an instance ID, the source IP addresses of the clients are preserved and provided to your applications.
If you specify targets by IP address, the source IP addresses are the private IP addresses of the load balancer nodes. If you need the IP addresses of the clients, enable Proxy Protocol and get the client IP addresses from the Proxy Protocol header.
If the network mode of ECS is awsvpc, then we could use IP to make sure the traffic is only from NLB, otherwise, it looks there is no way to limit the source other than using the external client IP range.