There are many cases that might you need to have direct IP public address on your ECS task. When your tasks are on EC2, situation is a little easier, but if you are on Fargate, you need to do some tricks to get a reachable IP public.
Before checking the script, don’t forget to consider these facts:
-
For getting a reachable public IP on task, you need to run the cluster on public subnet.
-
If you are in a subnet that is connected to NAT gateway, your task would not be reachable.
-
If you need to just get your NAT IP, you can take it easy and just run commands like this in your entry point scripts:
NAT_PUBLIC_IP=$(curl checkip.amazonaws.com)
-
If your app inside container, doesn’t need to know its public IP, so you are good, you don’t need to any special thing. Only enable
Auto-assign public IP
in your cluster service settings or in CDK.
In my case, that container is running a SIP application server, it needs to know its public interface to use in outbound SIP messages. So, I added below scripts as my container entry point, and then use the public IP value in app settings and the starting up the process.
|
|
if you installed some of the packages in building image, so you don’t need install again. Just install aws-cli
. Then we get task eni-id
and with ec2 describe-network-interfaces
we get IP public address that is assigned by AWS to task ENI.
Don’t forget that you need the specific role for running ec2 describe-network-interfaces
. Then add below in your task IAM role:
|
|