Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 571
pmann Avatar asked Aug 30 '16 05:08

pmann


3 Answers

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.

like image 161
Tim Keating Avatar answered Oct 17 '22 06:10

Tim Keating


This should show your Ingress IP address which is a public URL and is typically then mapped to your domain as a Type A record using AWS route53 console so your users can reach same just using your URL domain.com/lalala

aws ecs describe-services --service my-http-service

to discover this command and others generally the aws command line tool is actually very friendly ... just issue

aws help

then view that top level list of available subcommands then drill down by issuing

aws ecs help

and repeat above burrowing deeper into the available commands until you reach what could work ... as in

ecs ecs describe-services help
like image 42
Scott Stensland Avatar answered Oct 17 '22 08:10

Scott Stensland


You can get to the answer you want by running:

aws ecs describe-tasks --cluster foo --tasks 8591006e-6f05-4886-bc4a-58d9063d3852

This will contain the networkBindings section like this:

"networkBindings": [
                    {
                        "protocol": "tcp", 
                        "bindIP": "0.0.0.0", 
                        "containerPort": 7000, 
                        "hostPort": 32769
                    }, 
                    {
                        "protocol": "tcp", 
                        "bindIP": "0.0.0.0", 
                        "containerPort": 7001, 
                        "hostPort": 32768
                    }
                ]

Then you're golden!

like image 1
user1316072 Avatar answered Oct 17 '22 08:10

user1316072