forked from bfolkens/ecs-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs-host
executable file
·76 lines (67 loc) · 1.61 KB
/
ecs-host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/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` [-1h] -p profile-name -c cluster-name -s service-name"
}
# if less than two arguments supplied, display usage
if [ $# -le 1 ]
then
display_usage
exit 1
fi
# options were specified
while getopts "1hp:c:s:" opt; do
case $opt in
1)
single_output_mode=true
;;
p)
profile=$OPTARG
;;
c)
cluster=$OPTARG
;;
s)
service_name=$OPTARG
;;
h)
display_usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Script
containerInstanceArns=`(aws ecs list-tasks \
--profile ${profile} \
--cluster ${cluster} \
--query taskArns \
--max-items 100 \
--output text \
| head -1 | \
xargs aws ecs describe-tasks \
--profile ${profile} \
--cluster ${cluster} \
--query "tasks[]" \
--tasks) | \
jq -r ".[] | select(.containers[].name | contains(\"${service_name}\")) | .containerInstanceArn" | tr '\n' ' '`
ec2InstanceIds=`aws ecs describe-container-instances \
--profile ${profile} \
--cluster ${cluster} \
--container-instances ${containerInstanceArns} | jq -r '.containerInstances[].ec2InstanceId'`
ec2PublicIps=`aws ec2 describe-instances \
--profile ${profile} \
--instance-ids ${ec2InstanceIds} | jq -r ".Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp"`
if [[ $single_output_mode ]]
then
ec2PublicIps_array=( $ec2PublicIps )
echo ${ec2PublicIps_array[0]}
else
echo $ec2PublicIps
fi