-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.sh
356 lines (317 loc) · 11 KB
/
vpc.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash
########################################################################
### VPC ######################################################
########################################################################
function getVpcCidrRange {
local vpcid=$(getVpcId)
if [ $? -ne 0 ]; then return 1; fi
local cbr=$(aws --profile $PROFILE --region $REGION ec2 describe-vpcs --vpc-ids $vpcid --query "Vpcs[0].CidrBlock" --output text 2>/dev/null)
if [ $? -ne 0 ]; then return 1; fi
if [ -z "$cbr" ]; then return 1; fi
echo "$cbr"
return 0
}
function getVpcId {
if [ ! -z "$VPCID" ]; then
echo "$VPCID"
return 0
fi
if [ ! -z "$PLATFORM" ]; then
local vpcid=$(aws ec2 --profile $PROFILE --region $REGION describe-vpcs --filters "Name=tag:platform-name, Values=$PLATFORM" --query "Vpcs[*].[VpcId]" --output text 2>/dev/null)
if [ ! -z "$vpcid" ]; then
export VPCID=$vpcid
echo "$VPCID"
return 0
else
return 1
fi
fi
return 1
}
########################################################################
### SUBNETS ###################################################
########################################################################
# returns the 'chosen' subnet from all the private subnet ID's
# essentially gets the private subnets then returns the first on in the list
function getPrivateSubnetId {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
local vpcid="$1"
local snids=$(getPrivateSubnetsForVpc "$vpcid")
if [ -z "$snids" ]; then
return 1
fi
for s in $snids; do
echo "$s"
return 0
done
}
function getCommaDelimitedSubnetsForPlatform {
local scheme="private"
local vpcid=$(getVpcId)
local sn=$(aws --profile $PROFILE --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values=$vpcid "Name=tag:scheme,Values=$scheme" --query "Subnets[].SubnetId" --output text)
local ret=""
for i in $sn; do
ret="$ret$i,"
done
ret="${ret%?}"
echo "$ret"
}
function getSpaceDelimitedSubnetsForPlatform {
local scheme="private"
if [ ! -z "$1" ]; then local scheme="$1"; fi
local vpcid=$(getVpcId)
if [ $? -ne 0 ]; then return 1; fi
if [ -z "$vpcid" ]; then return 1; fi
local sn=$(aws --profile $PROFILE --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values=$vpcid "Name=tag:scheme,Values=$scheme" --query "Subnets[].SubnetId" --output text)
if [ $? -ne 0 ]; then return 1; fi
if [ -z "$sn" ]; then return 1; fi
local ret=""
for i in $sn; do
ret="$ret$i "
done
echo "$ret"
return 0
}
function getSubnet {
# gets the first subnet for the vpc for the scheme in $1
local scheme="private"
if [ ! -z "$1" ]; then local scheme="$1"; fi
local vpcid=$(getVpcId)
# aws ec2 describe-subnets --filters Name=vpc-id,Values=vpc-0099b918d2a911701 --region us-east-2 --query "Subnets[].SubnetId" --output text
local sn=$(aws --profile $PROFILE --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values=$vpcid "Name=tag:scheme,Values=$scheme" --query "Subnets[0].SubnetId" --output text 2>/dev/null)
if [ $? -ne 0 ]; then return 1; fi
echo "$sn"
}
# Returns the id of the internet gateway for the given vpcid
function getInternetGatewayIdForVpc {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
local vpcid="$1"
local IGW_ID=$(aws --profile $PROFILE --region $REGION ec2 describe-internet-gateways \
--filters Name=attachment.vpc-id,Values=${vpcid} \
--query "InternetGateways[0].InternetGatewayId" --output text 2>/dev/null)
if [ -z "$IGW_ID" ]; then
echo "no internet gateway found for vpcId $vpcid" >&2
return 1
fi
echo "$IGW_ID"
return 0
}
# Gets the public subnets for the given vpc by checking
# the route table for any subnets associated with the internet gateway
function getPublicSubnetsForVpc {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
# TODO weed out hybrid subnets somehow?
local vpcid="$1"
local igid=$(getInternetGatewayIdForVpc "$vpcid")
local PUBLIC_SUBNETS=$(aws --profile $PROFILE --region $REGION ec2 describe-route-tables \
--query 'RouteTables[*].Associations[].SubnetId' \
--filters "Name=vpc-id,Values=${vpcid}" "Name=route.gateway-id,Values=${igid}" \
--output text)
# weed out hybrid networks
echo "$PUBLIC_SUBNETS"
}
# gets the private subnets for the vpc by first
# getting the public subnets, then getting all subnets
# and excluding the public ones
function getPrivateSubnetsForVpc {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
local vpcid="$1"
local psnids=$(getPublicSubnetsForVpc $vpcid)
local snids="$(getAllSubnetsForVpc $vpcid)"
# now remove the public snids from snids
for i in $snids; do
if [[ ${psnids} != *"$i"* ]]; then
echo "$i"
fi
done
}
# returns the private subnets ensuring there are no control characters
# such as line breaks etc. Returns a space delimited list
function getSpaceDelimitedPrivateSubnetsForPlatform {
local vpcid=$(getVpcId)
local snids=$(getPrivateSubnetsForVpc "$vpcid")
if [ $? -ne 0 ]; then return 1; fi
if [ -z "$snids" ]; then return 1; fi
local ret=""
for i in $snids; do
ret="$ret$i "
done
echo "$ret"
return 0
}
function getAllSubnetsForVpc {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
local vpcid="$1"
local snids=$(aws --profile $PROFILE --region $REGION ec2 describe-subnets \
--filter Name=vpc-id,Values=${vpcid} \
--query 'Subnets[].SubnetId' --output text 2>/dev/null)
if [ $? -ne 0 ]; then
echo "failed to get subnets for vpcId $vpcid" >&2
return 1
fi
if [ -z "$snids" ]; then
echo "no subnets found for vpcId $vpcid" >&2
return 1
fi
# TODO weed out hybrid networks
echo "$snids"
return 0
}
function getSubnetsForVpcId {
if [ -z "$1" ]; then
echo "you must supply vpcId in the first parameter"
return
fi
local vpcid="$1"
local query="\"Subnets[].SubnetId\""
local scheme="private"
if [ ! -z "$2" ]; then local scheme="$2"; fi
if [ "$scheme"="private" ]; then
query="\"Subnets[?MapPublicIpOnLaunch=='false'].SubnetId\""
fi
echo "aws --profile $PROFILE --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values=$vpcid --query $query --output text"
return 0
local sn=$(aws --profile $PROFILE --region $REGION ec2 describe-subnets --filters Name=vpc-id,Values=$vpcid "Name=tag:scheme,Values=$scheme" --query "Subnets[].SubnetId" --output text)
if [ $? -ne 0 ]; then
echo "failed to get subnets for vpcId $vpcid" >&2
return 1
fi
if [ -z "$sn" ]; then
echo "no subnets found for vpcId $vpcid" >&2
return 1
fi
local ret="["
for i in $sn; do
ret+="\"$i\","
done
ret="${ret%?}"
ret+="]"
echo "$ret"
}
function getSubnetsForPlatform {
local scheme="private"
if [ ! -z "$1" ]; then local scheme="$1"; fi
local vpcid="$(getVpcId)"
getSubnetsForVpcId "$vpcid" "$scheme"
}
function deleteSubnetGroup {
if [ -z "$1" ]; then
echo "you must subnet group name in first parameter"
return
fi
if ! isDbSubnetGroupExists $1; then
echo "DB Subnet Group '$1' does not exist. Nothing to delete."
return
fi
aws --profile $PROFILE --region $REGION rds delete-db-subnet-group --db-subnet-group-name "$1"
if [ $? -eq 0 ]; then
echo "DB Subnet Group '$1' has been successfully deleted."
return 0
else
echo "Failed to delete DB Subnet Group '$1'. It might not exist or there might be associated resources."
return 1
fi
}
function listVpcPeeringConnectionsForPlatform {
local rgn=$(getRegion)
local vpc=$(getVpcId)
local pcs=$(aws --profile non-production --region $rgn ec2 describe-vpc-peering-connections --filters "Name=requester-vpc-info.vpc-id,Values=$vpc" --query "VpcPeeringConnections[].VpcPeeringConnectionId" --output text)
echo "$pcs"
}
function deleteVpcPeeringConnectionsForPlatform {
local foo=$(listVpcPeeringConnectionsForPlatform)
local rgn=$(getRegion)
for i in $foo; do
aws --profile non-production --region $rgn ec2 delete-vpc-peering-connection --vpc-peering-connection-id $i
echo "$i"
done
}
function getNatGatewaysForPlatform {
if [ -z $1]; then
local plat=$(getPlatform)
else
local plat=$1
fi
local ngwids=$(aws --profile non-production ec2 describe-nat-gateways --filter "Name=tag:platform-name,Values=$plat" --query "NatGateways[].NatGatewayId" --output text)
echo "$ngwids"
}
function getNatGatewaysForVpcId {
local rgn=$(getRegion)
local vpc=$(getVpcId)
local ids=$(aws --profile non-production --region $rgn ec2 describe-nat-gateways --filter "Name=vpc-id,Values=$vpc" --query "NatGateways[].NatGatewayId" --output text)
echo "$ids"
}
function deleteNatGatewaysForPlatform {
local foo=$(getNatGatewaysForVpcId)
local rgn=$(getRegion)
for i in $foo;do
aws --profile non-production --region $rgn ec2 delete-nat-gateway --nat-gateway-id $i
done
}
###################
# Network Interfaces
###################
function deleteNetworkInterfaces {
local foo=$(getNetworkInterfacesForPlatform)
if [[ -z "$foo" ]]; then
echo "no network intefaces for this platform"
return
fi
local a=( $foo )
for i in ${a[*]}; do
aws --profile $PROFILE --region $REGION ec2 delete-network-interface --network-interface-id $i
done
}
function getNetworkInterfacesForPlatform {
local vpcid=$(getVpcId)
if [ -z "$vpcid" ]; then
return
fi
#aws ec2 --profile non-production --region eu-west-1 describe-network-interfaces --filters "Name=vpc-id,Values=vpc-0219a66f6361bc1ad" --query "NetworkInterfaces[].[NetworkInterfaceId]" --output text
local foo=$(aws ec2 --profile $PROFILE --region $REGION describe-network-interfaces --filters "Name=vpc-id,Values=$VPCID" --query "NetworkInterfaces[].[NetworkInterfaceId]" --output text)
echo "$foo"
}
function listVpcEndpoints {
local rgn=$(getRegion)
local vpcid=$(getVpcId)
local foo=$(aws --profile non-production --region $rgn ec2 describe-vpc-endpoints --filters "Name=vpc-id,Values=$vpcid" --query "VpcEndpoints[].VpcEndpointId" --output text)
echo "$foo"
}
function deleteVpcEndpoints {
local rgn=$(getRegion)
local vpcid=$(getVpcId)
if [ -z "$vpcid" ]; then
return
fi
local foo=$(listVpcEndpoints)
if [[ -z "$foo" ]]; then
echo "no vpc endpoints for this platform"
return
fi
for i in $foo; do
aws --profile non-production --region $rgn ec2 delete-vpc-endpoints --vpc-endpoint-ids $i
done
}
function deleteVpc {
local rgn=$(getRegion)
local vpc=$(getVpcId)
if [ -z "$vpc" ]; then
return
fi
aws --profile non-production --region $rgn ec2 delete-vpc --vpc-id $vpc
}