How do I retrieve the public IP for a fargate task using the CLI?

Mike N. picture Mike N. · Mar 19, 2018 · Viewed 11k times · Source

I have a cluster running with services and am also able to launch fargate tasks from the command line. I can get the taskArn as a response to launching the task and I can wait for the task to be in the running state by using "aws ecs wait". I'm stuck figuring out how to get the the public IP of the task. I can find it via the web page easily enough and can access the machine via it's public IP...

How do I get the public IP of a fargate task using the CLI interface?

I'm using the following commands to launch the task, wait for it to run and retrieve the task description:

$ aws ecs run-task --launch-type FARGATE --cluster xxxx --task-definition xxxx --network-configuration xxxx
$ aws ecs wait <taskArn>
$ aws ecs describe-tasks --cluster xxxx --task <taskArn> | grep -i ipv4

The last command only gives me the private IP...

"privateIpv4Address": "10.0.1.252",
"name": "privateIPv4Address"

I've also tried using:

$ ecs-cli ps --cluster xxxx

But the Ports column is blank.

Name                                       State    Ports  TaskDefinition
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxxx  RUNNING         xxxx:1
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxxx  RUNNING         xxxx:1

Answer

Mike N. picture Mike N. · Mar 19, 2018

Once I realized that describe-tasks gave me the ENI id along with the private IP I was able to get the public IP with:

$ aws ec2 describe-network-interfaces --network-interface-ids eni-xxxxxxxx