Last active
August 11, 2016 17:25
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to https://github.com/bfolkens/ecs-tools