Search code examples
amazon-web-servicesaws-cliamazon-ecs

Fetch external link of ecs task running from aws cli


How to fetch the external link of aws ecs task running which has the public ip of container instance and port of the task running?

I am managing task/service execution from cli and would like to fetch the external link which is shown in the aws UI from aws cli. I tried describe-tasks command of aws cli but it doesn't return the public IP address of the instance the task is running on.

Is there a way to fetch the same from aws cli?

Thanks in advance!


Solution

  • This is quite the pain in the neck, but it will do what you want:

    1. List your tasks by service name

    aws ecs list-tasks --cluster mycluster --service-name my-service-name

    1. Get the details of a task using the task ARN from the above.

    aws ecs describe-tasks --cluster mycluster --tasks arn:aws:ecs:us-east-1:999999999999:task/ad0ba3e9-ac3b-4a4c-a1af-de3e06f46dfa

    1. The task description includes two pieces of information you need: the network bindings, which includes the port (look for the one that is mapped to the service port you care about), and the container instance ARN. (NOT the container ARN. Don't make that mistake and be super-confused when the next step fails, like I did). Using the container instance's ARN, get the details of the container instance:

    aws ecs describe-container-instances --cluster mycluster --container-instances arn:aws:ecs:us-east-1:999999999999:container-instance/707e5193-51e3-454b-ba09-9745c5d7f527

    1. As part of this description, you should be able to get the EC2 instance ID & query EC2 for more details:

    aws ec2 describe-instances --instance-ids i-c91aee40d92c23b3c ^^^^ NOT ECS

    The output of this command should include the private IP of the EC2 instance. This IP + previously computed port should correspond to the external link from the web UI.

    NOTE: Data has been anonymized, but should still LOOK like what you can expect to see. Also I omitted AWS region and profile params, which I have set to defaults using environment variables.