-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-data
36 lines (28 loc) · 1.54 KB
/
user-data
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
#cloud-config
bootcmd:
- echo HELLO FROM USER DATA SCRIPT | tee /msg > /dev/kmsg
- apt-get update
- apt-get install -y awscli jq
# fyi. this script works but hasn't been tested in user data yet.. so yaml/cloud-init issues beware
- |-
set -eu
export AWS_REGION="$(curl 169.254.169.254/latest/meta-data/placement/region)"
export AWS_DEFAULT_REGION="$(curl 169.254.169.254/latest/meta-data/placement/region)"
# Format of the results:
# operation per line
# disassociate:<assoicationId>:<tmpRouteTable> -- disassociate assoicationId and delete tmpRouteTable
# <origTableId>:<assoicationId>:<tmpRouteTable> -- replace assoicationId with origTableId and delete tmpRouteTable
results="$(aws ec2 describe-route-tables --filter "Name=tag-key,Values=OrigRouteTableId" |jq -r '.RouteTables|.[].Tags|.[]| select(.Key == "OrigRouteTableId")|.Value'|uniq)"
echo "$results" | while read line; do
origTableId="$(echo "$line"|cut -d: -f 1)";
assoicationId="$(echo "$line"|cut -d: -f 2)";
tmpRouteTable="$(echo "$line"|cut -d: -f 3)";
echo "origTableId: ${origTableId}, assoicationId: ${assoicationId}, tmpRouteTable: ${tmpRouteTable}"
if echo "$origTableId"| grep "disassociate"; then
aws ec2 disassociate-route-table --association-id "$assoicationId"
else
aws ec2 replace-route-table-association --association-id "$assoicationId" --route-table-id "$origTableId"
fi
aws ec2 delete-route-table --route-table-id "$tmpRouteTable"
done
- cloud-init clean && reboot