-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_nat_ec2.sh
executable file
·188 lines (150 loc) · 7.67 KB
/
create_nat_ec2.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
#!/bin/bash
echo; echo =================================================================================; echo
echo "Carregando variáveis de ambiente"
# Recebe como argumento o arquivo de variáveis de ambiente
. ./$1
image_id=ami-60b6c60a
key_name=producao-infra
instance_type=t2.micro
private_ip_address_NAT1=${cidr}.0.254
private_ip_address_NAT3=${cidr}.2.254
private_ip_address_NAT5=${cidr}.4.254
private_ip_address_NAT7=${cidr}.6.254
echo; echo =================================================================================; echo
for i in nat1 nat3 nat5 nat7; do
echo "Gerando arquivo ${i}-user-data.sh"
sed "s/HOST=@@@@@/HOST=${i}/g" nat-user-data-template.sh > ${i}-user-data.sh
sed -i "s/DOMAIN=#####/DOMAIN=${vpc_name}/g" ${i}-user-data.sh
done
echo; echo =================================================================================; echo
echo "Criando instância NAT1"
ec2_InstanceId_NAT1=$(aws --profile ${profile} ec2 run-instances \
--image-id ${image_id} \
--key-name ${key_name} \
--security-group-ids ${security_group_NAT1} \
--monitoring Enabled=true \
--instance-type ${instance_type} \
--subnet-id ${subnetid_1} \
--private-ip-address ${private_ip_address_NAT1} \
--associate-public-ip-address --user-data file://nat1-user-data.sh \
--output text \
--query 'Instances[*].InstanceId')
echo -n "Aguardando instância NAT1 estar no estado 'running'"
while state=$(aws --profile ${profile} ec2 describe-instances --instance-ids ${ec2_InstanceId_NAT1} --output text --query 'Reservations[*].Instances[*].State.Name'); test "${state}" = "pending"; do
sleep 1; echo -n '.'
done; echo " ${state}"
echo "Alterando Tag Name da instância NAT1 para 'nat1.${vpc_name}'"
aws --profile ${profile} ec2 create-tags \
--resources ${ec2_InstanceId_NAT1} \
--tags Key=Name,Value="nat1.${vpc_name}"
echo "Alterando parâmetro source-dest-check para no"
aws --profile ${profile} ec2 modify-instance-attribute \
--instance-id ${ec2_InstanceId_NAT1} \
--no-source-dest-check
echo "Adicionando a instância NAT1 na tabela de roteamento privada para destinos 0.0.0.0/0"
aws --profile ${profile} ec2 create-route \
--route-table-id ${private1_routetable_id} \
--destination-cidr-block 0.0.0.0/0 \
--instance-id ${ec2_InstanceId_NAT1}
##echo "Parando a instância"
##aws --profile ${profile} ec2 stop-instances --instance-ids ${ec2_InstanceId_NAT1}
echo; echo =================================================================================; echo
echo "Criando instância NAT3"
ec2_InstanceId_NAT3=$(aws --profile ${profile} ec2 run-instances \
--image-id ${image_id} \
--key-name ${key_name} \
--security-group-ids ${security_group_NAT3} \
--monitoring Enabled=true \
--instance-type ${instance_type} \
--subnet-id ${subnetid_3} \
--private-ip-address ${private_ip_address_NAT3} \
--associate-public-ip-address \
--user-data file://nat3-user-data.sh \
--output text \
--query 'Instances[*].InstanceId')
echo -n "Aguardando instância NAT3 estar no estado 'running'"
while state=$(aws --profile ${profile} ec2 describe-instances --instance-ids ${ec2_InstanceId_NAT3} --output text --query 'Reservations[*].Instances[*].State.Name'); test "${state}" = "pending"; do
sleep 1; echo -n '.'
done; echo " ${state}"
echo "Alterando Tag Name da instância NAT3 para 'nat3.${vpc_name}'"
aws --profile ${profile} ec2 create-tags \
--resources ${ec2_InstanceId_NAT3} \
--tags Key=Name,Value="nat3.${vpc_name}"
echo "Alterando parâmetro source-dest-check para no"
aws --profile ${profile} ec2 modify-instance-attribute \
--instance-id ${ec2_InstanceId_NAT3} \
--no-source-dest-check
echo "Adicionando a instância NAT3 na tabela de roteamento privada para destinos 0.0.0.0/0"
aws --profile ${profile} ec2 create-route \
--route-table-id ${private2_routetable_id} \
--destination-cidr-block 0.0.0.0/0 \
--instance-id ${ec2_InstanceId_NAT3}
##echo "Parando a instância"
##aws --profile ${profile} ec2 stop-instances --instance-ids ${ec2_InstanceId_NAT3}
echo; echo =================================================================================; echo
echo "Criando instância NAT5"
ec2_InstanceId_NAT5=$(aws --profile ${profile} ec2 run-instances \
--image-id ${image_id} \
--key-name ${key_name} \
--security-group-ids ${security_group_NAT5} \
--monitoring Enabled=true \
--instance-type ${instance_type} \
--subnet-id ${subnetid_5} \
--private-ip-address ${private_ip_address_NAT5} \
--associate-public-ip-address \
--user-data file://nat5-user-data.sh \
--output text \
--query 'Instances[*].InstanceId')
echo -n "Aguardando instância NAT5 estar no estado 'running'"
while state=$(aws --profile ${profile} ec2 describe-instances --instance-ids ${ec2_InstanceId_NAT5} --output text --query 'Reservations[*].Instances[*].State.Name'); test "${state}" = "pending"; do
sleep 1; echo -n '.'
done; echo " ${state}"
echo "Alterando Tag Name da instância NAT5 para 'nat5.${vpc_name}'"
aws --profile ${profile} ec2 create-tags \
--resources ${ec2_InstanceId_NAT5} \
--tags Key=Name,Value="nat5.${vpc_name}"
echo "Alterando parâmetro source-dest-check para no"
aws --profile ${profile} ec2 modify-instance-attribute \
--instance-id ${ec2_InstanceId_NAT5} \
--no-source-dest-check
echo "Adicionando a instância NAT5 na tabela de roteamento privada para destinos 0.0.0.0/0"
aws --profile ${profile} ec2 create-route \
--route-table-id ${private3_routetable_id} \
--destination-cidr-block 0.0.0.0/0 \
--instance-id ${ec2_InstanceId_NAT5}
##echo "Parando a instância"
##aws --profile ${profile} ec2 stop-instances --instance-ids ${ec2_InstanceId_NAT5}
echo; echo =================================================================================; echo
echo "Criando instância NAT7"
ec2_InstanceId_NAT7=$(aws --profile ${profile} ec2 run-instances \
--image-id ${image_id} \
--key-name ${key_name} \
--security-group-ids ${security_group_NAT7} \
--monitoring Enabled=true \
--instance-type ${instance_type} \
--subnet-id ${subnetid_7} \
--private-ip-address ${private_ip_address_NAT7} \
--associate-public-ip-address \
--user-data file://nat7-user-data.sh \
--output text \
--query 'Instances[*].InstanceId')
echo -n "Aguardando instância NAT7 estar no estado 'running'"
while state=$(aws --profile ${profile} ec2 describe-instances --instance-ids ${ec2_InstanceId_NAT7} --output text --query 'Reservations[*].Instances[*].State.Name'); test "${state}" = "pending"; do
sleep 1; echo -n '.'
done; echo " ${state}"
echo "Alterando Tag Name da instância NAT7 para 'nat7.${vpc_name}'"
aws --profile ${profile} ec2 create-tags \
--resources ${ec2_InstanceId_NAT7} \
--tags Key=Name,Value="nat7.${vpc_name}"
echo "Alterando parâmetro source-dest-check para no"
aws --profile ${profile} ec2 modify-instance-attribute \
--instance-id ${ec2_InstanceId_NAT7} \
--no-source-dest-check
echo "Adicionando a instância NAT7 na tabela de roteamento privada para destinos 0.0.0.0/0"
aws --profile ${profile} ec2 create-route \
--route-table-id ${private4_routetable_id} \
--destination-cidr-block 0.0.0.0/0 \
--instance-id ${ec2_InstanceId_NAT7}
##echo "Parando a instância"
##aws --profile ${profile} ec2 stop-instances --instance-ids ${ec2_InstanceId_NAT5}
echo; echo =================================================================================; echo