Skip to content

Instantly share code, notes, and snippets.

@bfolkens
Last active August 11, 2016 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfolkens/b93658a30853bea9495319c5c780f213 to your computer and use it in GitHub Desktop.
Save bfolkens/b93658a30853bea9495319c5c780f213 to your computer and use it in GitHub Desktop.
Small utility script to emit the public ip address of an AWS ECS service
#!/bin/bash
#
# Usage: ./ecs-host profile-name cluster-name service-name
#
display_usage() {
echo "Retrieve ip address for service name."
echo -e "\nusage: `basename $0` profile-name cluster-name service-name"
}
# if less than two arguments supplied, display usage
if [ $# -le 1 ]
then
display_usage
exit 1
fi
# check whether user had supplied -h or --help . If yes display usage
if [[ ( $# == "--help") || $# == "-h" ]]
then
display_usage
exit 0
fi
# Script
profile=$1
cluster=$2
service_name=$3
containerInstanceArn=$((aws ecs list-tasks --profile=$profile --cluster=$cluster --query=taskArns --output text | xargs aws ecs describe-tasks --profile=$profile --cluster=$cluster --query "tasks[]" --tasks) | jq -r ".[] | select(.containers[].name | contains(\"${service_name}\")) | .containerInstanceArn")
ec2InstanceId=$(aws ecs describe-container-instances --profile=$profile --cluster=$cluster --container-instances=$containerInstanceArn | jq -r '.containerInstances[].ec2InstanceId')
aws ec2 describe-instances --profile=$profile --instance=$ec2InstanceId | jq -r ".Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp"
@bfolkens
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment