Created
January 20, 2022 01:23
-
-
Save Y4suyuki/083e09249e4715f4401bda746c00acc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
set -eu | |
cluster='' | |
p='' | |
usage() { | |
echo "Usage: $0 [-c CLUSTER_NAME] [-p AWS_PROFILE]" 1>&2 | |
} | |
## Get options | |
while getopts "c:p:h" options; do | |
case "${options}" in | |
h) | |
usage | |
exit 0 | |
;; | |
c) | |
cluster=${OPTARG} | |
;; | |
p) | |
p=${OPTARG} | |
;; | |
*) | |
echo "unknown option." | |
usage | |
exit 1 | |
esac | |
done | |
## Validation | |
if [ -z $cluster ]; then | |
echo "please set CLUSTER_NAME with -c option" | |
exit 1 | |
fi | |
if [ -z $p ]; then | |
echo "please set AWS_PROFILE with -p option" | |
exit 1 | |
fi | |
declare -a containerInstanceArns | |
while read -r line; do | |
IFS=' ' | |
containerInstanceArns+=($(echo ${line} | tr -d '"')); | |
done < <(aws ecs list-container-instances --profile $p --cluster $cluster | jq ".containerInstanceArns[]") | |
echo container instance arns | |
for a in "${containerInstanceArns[@]}"; do | |
echo "$a" | |
done | |
## Get ECS Instance instance IDs | |
declare -a ec2InstanceIds | |
for arn in "${containerInstanceArns[@]}"; do | |
res=$(aws ecs describe-container-instances --profile $p --cluster $cluster --container-instances "$arn" | jq ".containerInstances[] | .ec2InstanceId") | |
ec2InstanceIds+=($(echo $res | tr -d '"')) | |
done; | |
## GET ECS instances private ips | |
echo "++++++++ RESULT ++++++++" | |
for x in "${ec2InstanceIds[@]}"; do | |
aws ec2 describe-instances --profile "$p" --instance-ids $x | jq ".Reservations[] | .Instances[] | .PrivateIpAddress" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment