-
Notifications
You must be signed in to change notification settings - Fork 3
/
r53-healthchk-sg.sh
executable file
·150 lines (128 loc) · 3.32 KB
/
r53-healthchk-sg.sh
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
#
# Build a Route 53 Health Check security group containing AWS health check CIDRs
# Requires:
# * the aws-cli
# * a valid profile in ~/.aws/config or ${AWS_CONFIG_FILE}
# Usage statement
#
usage ()
{
echo " Build a Route 53 Health Check security group in your VPC."
echo " >> Usage: $0 -n <profile_name> -r <region> [ -p <port> ]"
echo " Note: default_port=80"
exit 1
}
check ()
{
if [ $? -ne 0 ]; then
echo " Error: Couldn't find $1. Please check."
exit 1
fi
}
while getopts "g:n:p:r:v:h" opt; do
case $opt in
g)
NAME=$OPTARG
;;
n)
PROFILE=$OPTARG
;;
p)
PORT=$OPTARG
;;
r)
REGION=$OPTARG
;;
v)
VPCID=$OPTARG
;;
[h?])
usage
exit
;;
esac
done
# Test for args
#
if [[ $PROFILE == "" || $REGION == "" || $VPCID == "" ]] ; then
usage
fi
if [[ $NAME == "" ]]; then
NAME=route53-healthchk
fi
if [[ $PORT == "" ]]; then
PORT=80
fi
get_cidr_block () {
v6=$1
r53CIDRS=$(curl https://ip-ranges.amazonaws.com/ip-ranges.json 2> /dev/null | grep -B2 ROUTE53_HEALTHCHECKS | grep ip${v6}_prefix | awk -F\" '{print $4}')
for cidr in ${r53CIDRS}; do
ip_ranges="${ip_ranges}{\"CidrIp${v6}\": \"${cidr}\"},"
# echo -n "."
done
# Remove last character
ip_ranges="${ip_ranges%?}"
echo "\"Ip${v6}Ranges\": [${ip_ranges}]"
}
get_ip_permissions () {
protocol="tcp"
port=$1
echo "[{\"IpProtocol\": \"${protocol}\", \"FromPort\": ${port}, \"ToPort\": ${port}, $(get_cidr_block ''), $(get_cidr_block 'v6')}]"
}
# Our variables
#
DESC=Route-53-health-check-security-group
NUMBER='^[0-9]+$'
# Test for the port number
#
if ! [[ $PORT =~ $NUMBER ]]; then
echo " Error: Invalid port number."
exit 1
fi
# Test for the aws-cli
#
which aws > /dev/null 2>&1
check "the aws-cli commands"
# Test the profile
#
aws ec2 describe-regions --profile $PROFILE > /dev/null 2>&1
check "profile $PROFILE"
# Test the region
#
aws ec2 describe-regions --region-names $REGION --profile $PROFILE > /dev/null 2>&1
check "the region $REGION"
# Test for valid VPC-Id
#
aws ec2 describe-vpcs --vpc-ids $VPCID --profile $PROFILE --region $REGION > /dev/null 2>&1
check "$VPCID in region $REGION"
# Check for an existing security-group
#
SGId=`aws ec2 describe-security-groups --filters Name=group-name,Values=$NAME --profile $PROFILE --region $REGION --query SecurityGroups[].GroupId | grep sg-`
if [[ $SGId != "" ]]; then
SGId="$(echo -e $SGId | tr -d '[:space:]' | tr -d '"')"
echo " $SGId already exists, using."
else
# Create our security group and record the Id
#
echo -n "Creating R53 health check security group "
aws ec2 create-security-group --group-name $NAME --description $DESC --vpc-id $VPCID --profile $PROFILE --region $REGION --output json > /tmp/sg-id.$$
SGId=`cat /tmp/sg-id.$$ | grep GroupId | awk -F\" '{print $4}'`
fi
# Populate the security group
#
echo "$(get_ip_permissions $PORT)" > /tmp/ippermissions.$$
populated=`aws ec2 authorize-security-group-ingress --group-id $SGId --profile $PROFILE --region $REGION --ip-permissions file:///tmp/ippermissions.$$`
echo -n "."
# Tag it
#
aws ec2 create-tags --resources $SGId --tags Key=Name,Value=$NAME --profile $PROFILE --region $REGION
# echo ""
echo -n " done!"
echo ""
echo "Security group Id: $SGId"
# Clean up
#
rm -f /tmp/sg-id.$$
rm -f /tmp/ippermissions.$$
exit 0