1

I try to find public IP addresses of all EC2 instances within a ECS cluster. I can see EC2 instances id's with command:

aws ecs list-container-instances --cluster myCluster

But this won't show IP addresses. I searched ECS CLI documentation but I can't find a command to retrieve associated IP addresses for each EC2 instance. What is easiest way to do this?

Thanks.

JavaRed
  • 638
  • 3
  • 10
  • 29
  • 2
    Feed the output of list-container-instances to describe-instances using --output text and xargs like shown here https://stackoverflow.com/questions/38148397/is-there-a-way-to-pipe-the-output-of-one-aws-cli-command-as-the-input-to-another – jontro Jan 09 '18 at 00:15

1 Answers1

0

This is what i got, not pretty - should have used python and boto2. It uses the aws cli piped to jq and more bash which i'm learning.

#!/bin/bash
c_instances=`aws ecs list-container-instances --cluster your-cluster --region=eu-west-1 | jq '.containerInstanceArns | join (" ")' | tr -d '"'`

i_ids=`aws ecs describe-container-instances --cluster your-cluster --region=eu-west-1  --container-instances $c_instances | jq '.containerInstances | .[] | .ec2InstanceId'|tr '\n' ' ' | tr -d '"'`

ips=`aws ec2 --region=eu-west-1 describe-instances --instance-ids $i_ids |jq '.Reservations | .[] | .Instances | .[] .NetworkInterfaces | .[] .PrivateIpAddresses| .[] .Association| .PublicIp '`
echo $ips
Narayan
  • 91
  • 5