-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathazure_infrastructure.sh
More file actions
288 lines (243 loc) · 8.5 KB
/
azure_infrastructure.sh
File metadata and controls
288 lines (243 loc) · 8.5 KB
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
""" Azure infrastructure as a code (Iac) requirementss:
- 3 VM's
- One of the VM's acts a router between the other 2
- Each of the other VM's is in a separate subnet
- The idea is that the other 2 VM's can only communicate with each other through the Routing VM
- The subnets are called 'SubnetA' and 'SubnetB'
- Routing VM is called 'Router VM', 'VM2' is in 'SubnetA' and 'VM3' is in 'SubnetB'
"""
#0. Setting up the variable names
locationRG=uksouth
nameVM1=RouterVM
nameVM1IP=RouterVMPublicIP
vmNameInSubnetA=VM2
vmNameInSubnetB=VM3
nameRG=UnderstandNetworkingRG
nameVnet=UnderstandNetworkingVNET
nameSubnetA=SubnetA
nameSubnetB=SubnetB
addressVnet=10.0.0.0/16
addressSubnetA=10.0.1.0/24 # the subnet preffix for SubnetA
addressSubnetB=10.0.2.0/24 # possible IP Addresses range: {10.0.2.0 - 10.0.2.255}
VM2VMNIC=10.0.1.4 # Private IP for VM2 located in SubnetA
VM3VMNIC=10.0.2.4 # Private IP for VM3 located in SubnetB
RouterVMNIC=10.0.1.9 #eth0 for Router VM located in SubnetA
RouterVMNIC2=10.0.2.5 #eth1 for Router VM located in SubnetB
adminUsername=boris
adminPassword=boris_pass123@@B
privateDNSZone=www.understandnetworking.com
#1. Create a resourse group in Azure
az group create \
--location $locationRG \
--resource-group $nameRG
#2. Create a Virtual Network with 1 Subnet, called 'Subnet A' (Vnet + Subnet)
az network vnet create \
--resource-group $nameRG \
--name $nameVnet \
--address-prefixes $addressVnet \
--subnet-name $nameSubnetA \
--subnet-prefixes $addressSubnetA
#3. Create 'Subnet B' and associate it within the same Virtual Network (Subnet)
az network vnet subnet create \
--resource-group $nameRG \
--vnet-name $nameVnet \
--name $nameSubnetB \
--address-prefix $addressSubnetB
#II. Add a internal DNS for VM
# Extracting the ResourceID for SubnetA and SubnetB
subnetAResourceId=$(az network vnet subnet show --resource-group $nameRG --vnet-name $nameVnet --name $nameSubnetA --query id --output tsv)
subnetBResourceId=$(az network vnet subnet show --resource-group $nameRG --vnet-name $nameVnet --name $nameSubnetB --query id --output tsv)
#4. Creating the VM which will lie in SubnetA
az vm create \
--resource-group $nameRG \
--name $vmNameInSubnetA \
--image Ubuntu2204 \
--admin-username $adminUsername \
--admin-password $adminPassword \
--vnet-address-prefix $$addressVnet \
--subnet-address-prefix $addressSubnetA \
--public-ip-address "" # To prevent the assignment of a public IP
#5. Creating the VM which will lie in SubnetB
az vm create \
--resource-group $nameRG \
--name $vmNameInSubnetB \
--image Ubuntu2204 \
--admin-username $adminUsername \
--admin-password $adminPassword \
--vnet-address-prefix $$addressVnet \
--subnet $subnetBResourceId \
--subnet-address-prefix $addressSubnetB \
--public-ip-address "" # To prevent the assignment of a public IP
#6. Create 'VM1' that will act as a router
# I have associated eth0(NIC1) for RouterVM to be connected to SubnetA, while eth1(NIC2) for RouterVM will be connected to SubnetB
# 6.1 Create RouterVM with NIC1 in SunbetA
az vm create \
--resource-group $nameRG \
--name $nameVM1 \
--image Ubuntu2204 \
--admin-username $adminUsername \
--admin-password $adminPassword \
--subnet $subnetAResourceId \
--private-ip-address $RouterVMNIC \
--public-ip-address "" # To prevent the assignment of a public IP
# 6.2 Creating NIC2 (eth1) for the router VM (SubnetB)
az network nic create \
--resource-group $nameRG \
--name "${nameVM1}NIC2" \
--vnet-name $nameVnet \
--subnet $nameSubnetB
#7. Create a DNS record for VM2 and VM3:
#7.1 Create private dns zone
az network private-dns zone create \
--resource-group $nameRG \
--name $privateDNSZone
#7.2 Link dns zone to vnet
az network private-dns link vnet create \
-g $nameRG \
-n MyDNSLink \
-z $privateDNSZone \
-v $nameVnet \
-e false
#7.3 DNS record for the VM in SubnetA
az network private-dns record-set a add-record \
-g $nameRG \
-z $privateDNSZone \
-n vm2-dns \
-a $VM2VMNIC
#7.4 Create a DNS record for the VM in SubnetB
az network private-dns record-set a add-record \
-g $nameRG \
-z $privateDNSZone \
-n vm3-dns \
-a $VM3VMNIC
# 8. NSG's to restrict communication between VM2 and VM3 (happens through RouterVM instead)
# NSG Rules:
# - Define NSG rules to allow inbound traffic in VM2 and VM3 only from the RouterVM.
# - Deny all other inbound traffic in VM2 and VM3.
# - Deny outbound traffic to VM2 and VM3 to all sources except RouterVM.
# 8.1 Create NSG rules for VM2:
# A) Allow inbound traffic from RouterVM
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM2NSG \
--name allow-router-inbound \
--priority 100 \
--source-address-prefix $RouterVMNIC \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range '*' \
--access Allow \
--direction Inbound \
--protocol '*'
# B) Deny outbound traffic to VM3
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM2NSG \
--name deny-vm3-outbound \
--priority 200 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix $VM3VMNIC \
--destination-port-range '*' \
--access Deny \
--direction Outbound \
--protocol '*'
# C) Allow outbound traffic to RouterVM
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM2NSG \
--name allow-router-outbound \
--priority 300 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix $RouterVMNIC \
--destination-port-range '*' \
--access Allow \
--direction Outbound \
--protocol '*'
# D) Deny all other outbound traffic
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM2NSG \
--name deny-other-outbound \
--priority 400 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range '*' \
--access Deny \
--direction Outbound \
--protocol '*'
# 8.2 Create NSG rules for VM3:
# A) Allow inbound traffic from RouterVM
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM3NSG \
--name allow-router-inbound \
--priority 100 \
--source-address-prefix $RouterVMNIC2 \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range '*' \
--access Allow \
--direction Inbound \
--protocol '*'
# B) Deny outbound traffic to VM2
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM3NSG \
--name deny-vm2-outbound \
--priority 200 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix $VM2VMNIC \
--destination-port-range '*' \
--access Deny \
--direction Outbound \
--protocol '*'
# C) Allow outbound traffic to RouterVM
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM3NSG \
--name allow-router-outbound \
--priority 300 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix $RouterVMNIC2 \
--destination-port-range '*' \
--access Allow \
--direction Outbound \
--protocol '*'
# extract VM3's NSG name and store it in a variable
# D) Deny all other outbound traffic
az network nsg rule create \
--resource-group $nameRG \
--nsg-name VM3NSG \
--name deny-other-outbound \
--priority 400 \
--source-address-prefix '*' \
--source-port-range '*' \
--destination-address-prefix '*' \
--destination-port-range '*' \
--access Deny \
--direction Outbound \
--protocol '*'
############################################################
# This is how you can ssh from the RouterVM to VM2/VM3:
## ssh boris@vm3-dns.www.understandnetworking.com ##
############################################################
# 9. Add public IP to RouterVM
# 9.1 Allocate public ip to RouterVM
az network public-ip create \
--name $nameVM1IP \
--resource-group $nameRG \
--allocation-method Static \
--sku Standard \
--location $locationRG
# 9.2 Associate it with NIC0
NICRouterVM=$(az vm show -g $nameRG -n $nameVM1 --query 'networkProfile.networkInterfaces[].id' -o tsv | awk -F'/' '{print $NF}')
az network nic ip-config update \
--name ipconfigRouterVM \
--nic-name $NICRouterVM \
--resource-group $nameRG \
--public-ip-address $nameVM1IP