-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy-vpc-cli.sh
executable file
·134 lines (118 loc) · 6.85 KB
/
deploy-vpc-cli.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
#! /usr/bin/env bash
## use this to get created subnets:
# echo "INFO: getting subnet IDs by tag:Name"
# subnets_public=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=${cluster_name}-public" --query "Subnets[].SubnetId" --output json)
# subnets_private=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=${cluster_name}-private" --query "Subnets[].SubnetId" --output json)
# export SUBNET_IDS=$(echo "${subnets_public}" | jq -r ". + ${subnets_private} | join(\",\")")
# echo "INFO: Subnet IDs: ${SUBNET_IDS}"
this_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
root_dir=$(cd ${this_dir}/../../../.. && pwd)
if [[ -f ${root_dir}/.env ]]; then source ${root_dir}/.env; fi
if [[ -f ${this_dir}/.env ]]; then source ${this_dir}/.env; fi
export cluster_name=${1:-${CLUSTER_NAME}}
export AWS_REGION=${2:-${AWS_REGION}}
function reconcile_vpc {
local vpc_cidr=${1:-'10.0.0.0/16'}
local cluster_name=${2:-${cluster_name}}
vpc_id=$(aws ec2 describe-vpcs --filters "Name=tag:Name,Values=${cluster_name}" --query "Vpcs[0].VpcId" --output text)
if [[ "${vpc_id}" == "None" ]]; then
echo "INFO: creating VPC"
vpc_id=$(aws ec2 create-vpc --cidr-block "${vpc_cidr}" --query Vpc.VpcId --output text)
aws ec2 create-tags --resources ${vpc_id} --tags "Key=Name,Value=${cluster_name}"
aws ec2 modify-vpc-attribute --vpc-id ${vpc_id} --enable-dns-hostnames
echo "INFO: created VPC ID ${vpc_id}"
fi
echo "INFO: using VPC ID ${vpc_id}"
}
function reconcile_az {
local subnet1_cidr=${1:-'10.0.0.0/24'}
local subnet2_cidr=${2:-'10.0.1.0/24'}
local aws_az=${3:-'us-east-2a'}
local cluster_name=${4:-${cluster_name}}
subnet1_id=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=${cluster_name}-public" "Name=availability-zone,Values=${aws_az}" --query "Subnets[0].SubnetId" --output text)
if [[ "${subnet1_id}" == "None" ]]; then
echo "INFO: create subnet 1"
subnet1_id=$(aws ec2 create-subnet --vpc-id ${vpc_id} \
--cidr-block "${subnet1_cidr}" \
--availability-zone ${aws_az} \
--query Subnet.SubnetId \
--output text)
aws ec2 create-tags --resources ${subnet1_id} \
--tags "Key=Name,Value=${cluster_name}-public"
echo "INFO: created subnet1 ID ${subnet1_id}"
fi
echo "INFO: using subnet1 ID ${subnet1_id}"
subnet2_id=$(aws ec2 describe-subnets --filters "Name=tag:Name,Values=${cluster_name}-private" "Name=availability-zone,Values=${aws_az}" --query "Subnets[0].SubnetId" --output text)
if [[ "${subnet2_id}" == "None" ]]; then
echo "INFO: create subnet 2"
subnet2_id=$(aws ec2 create-subnet --vpc-id ${vpc_id} \
--cidr-block "${subnet2_cidr}" \
--availability-zone ${aws_az} \
--query Subnet.SubnetId \
--output text)
aws ec2 create-tags --resources ${subnet2_id} \
--tags "Key=Name,Value=${cluster_name}-private"
echo "INFO: created subnet2 ID ${subnet2_id}"
fi
echo "INFO: using subnet2 ID ${subnet2_id}"
inetgw_id=$(aws ec2 describe-internet-gateways --filters "Name=tag:Name,Values=${cluster_name}" --query "InternetGateways[0].InternetGatewayId" --output text)
if [[ "${inetgw_id}" == "None" ]]; then
echo "INFO: creating Internet Gateway"
inetgw_id=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --vpc-id ${vpc_id} --internet-gateway-id ${inetgw_id}
aws ec2 create-tags --resources ${inetgw_id} --tags "Key=Name,Value=${cluster_name}"
echo "INFO: created Internet Gateway ID ${inetgw_id}"
fi
echo "INFO: using Internet Gateway ID ${inetgw_id}"
routetable1_id=$(aws ec2 describe-route-tables --filters "Name=tag:Name,Values=${cluster_name}" --query "RouteTables[0].RouteTableId" --output text)
if [[ "${routetable1_id}" == "None" ]]; then
echo "INFO: creating routetable1"
routetable1_id=$(aws ec2 create-route-table --vpc-id ${vpc_id} --query RouteTable.RouteTableId --output text)
aws ec2 associate-route-table --subnet-id ${subnet1_id} --route-table-id ${routetable1_id}
aws ec2 create-route --route-table-id ${routetable1_id} --destination-cidr-block "0.0.0.0/0" --gateway-id ${inetgw_id}
aws ec2 create-tags --resources ${routetable1_id} --tags "Key=Name,Value=${cluster_name}"
echo "INFO: created routetable1 ID ${routetable1_id}"
fi
aws ec2 associate-route-table --subnet-id ${subnet1_id} --route-table-id ${routetable1_id}
echo "INFO: using routetable1 ID ${routetable1_id}"
natgw_id=$(aws ec2 describe-nat-gateways --filter "Name=tag:Name,Values=${cluster_name}" --query NatGateways[0].NatGatewayId --output text)
if [[ "${natgw_id}" == "None" ]]; then
echo "INFO: create NAT Gateway"
ipaddr_id=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
natgw_id=$(aws ec2 create-nat-gateway --subnet-id ${subnet1_id} --allocation-id ${ipaddr_id} --query NatGateway.NatGatewayId --output text)
aws ec2 create-tags --resources ${ipaddr_id} --resources ${natgw_id} --tags "Key=Name,Value=${cluster_name}"
sleep 5
echo "INFO: created NAT Gateway ID ${natgw_id}"
fi
echo "INFO: using NAT Gateway ID ${natgw_id}"
routetable2_id=$(aws ec2 describe-route-tables --filters "Name=tag:Name,Values=${cluster_name}-private" --query "RouteTables[0].RouteTableId" --output text)
if [[ "${routetable2_id}" == "None" ]]; then
echo "INFO: creating routetable2"
routetable2_id=$(aws ec2 create-route-table --vpc-id ${vpc_id} --query RouteTable.RouteTableId --output text)
aws ec2 associate-route-table --subnet-id ${subnet2_id} --route-table-id ${routetable2_id}
aws ec2 create-route --route-table-id ${routetable2_id} --destination-cidr-block 0.0.0.0/0 --gateway-id ${natgw_id}
aws ec2 create-tags --resources ${routetable2_id} ${ipaddr_id} --tags "Key=Name,Value=${cluster_name}-private"
echo "INFO: created routetable2 ID ${routetable2_id}"
fi
aws ec2 associate-route-table --subnet-id ${subnet2_id} --route-table-id ${routetable2_id}
echo "INFO: using routetable2 ID ${routetable2_id}"
}
vpc_cidr='10.0.0.0/16'
declare -A azs_map
az1=${AWS_REGION}a
az2=${AWS_REGION}b
az3=${AWS_REGION}c
azs_map=(
["${az1}"]='10.0.0.0/24 10.0.1.0/24'
["${az2}"]='10.0.2.0/24 10.0.3.0/24'
["${az3}"]='10.0.4.0/24 10.0.5.0/24'
)
reconcile_vpc ${vpc_cidr} ${cluster_name}
for aws_az in ${az1} ${az2} ${az3}; do
for subnet in "${azs_map[${aws_az}]}"; do
subnet1_cidr=$(echo ${subnet} | awk '{print $1}')
subnet2_cidr=$(echo ${subnet} | awk '{print $2}')
done
echo "INFO: reconcile_az ${subnet1_cidr} ${subnet2_cidr} ${aws_az} ${cluster_name}"
reconcile_az ${subnet1_cidr} ${subnet2_cidr} ${aws_az} ${cluster_name}
done