-
Notifications
You must be signed in to change notification settings - Fork 2
/
Automating Infrastructure on Google Cloud with Terraform- Challenge Lab.txt
362 lines (252 loc) · 6.67 KB
/
Automating Infrastructure on Google Cloud with Terraform- Challenge Lab.txt
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
Task 1. Create the configuration files
Run in Cloud Shell
touch main.tf
touch variables.tf
mkdir modules
cd modules
mkdir instances
cd instances
touch instances.tf
touch outputs.tf
touch variables.tf
cd ..
mkdir storage
cd storage
touch storage.tf
touch outputs.tf
touch variables.tf
cd
Add the following to the each variables.tf file, and fill in the GCP Project ID:
variable "region" {
default = "us-central1"
}
variable "zone" {
default = "us-central1-a"
}
variable "project_id" {
default = "<REPLACE PROJECT ID>"
}
Add the following to the main.tf file :
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
module "instances" {
source = "./modules/instances"
}
Run terraform init in Cloud Shell in the root directory to initialize terraform.
Task 2. Import infrastructure
Next, navigate to modules/instances/instances.tf. Copy the following configuration into the file:
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-1"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-1"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
}
Navigate to Compute Engine > VM Instances. Click on tf-instance-1. Copy the Instance ID
Navigate to Compute Engine > VM Instances. Click on tf-instance-2. Copy the Instance ID
Run in cloud shell
terraform import module.instances.google_compute_instance.tf-instance-1 <Instance ID - 1>
terraform import module.instances.google_compute_instance.tf-instance-2 <Instance ID - 2>
terraform plan
terraform apply
Task 3. Configure a remote backend
Add the following code to the modules/storage/storage.tf file
resource "google_storage_bucket" "storage-bucket" {
name = "<YOUR-BUCKET>"
location = "US"
force_destroy = true
uniform_bucket_level_access = true
}
Next, add the following to the main.tf file:
module "storage" {
source = "./modules/storage"
}
Run cloud shell :
terraform init
terraform apply
Next, update the main.tf
terraform {
backend "gcs" {
bucket = "<REPLACE YOUR BUCKET>"
prefix = "terraform/state"
}
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
terraform init
Task 4. Modify and update infrastructure
navigate to modules/instances/instances.tf modify tf-instance-1 and tf-instance-2
add tf-instance-3
resource "google_compute_instance" "Instance Name" {
name = "Instance Name"
machine_type = "n1-standard-2"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
}
Run cloud shell :
terraform init
terraform apply
Task 5. Taint and destroy resources
terraform taint module.instances.google_compute_instance.Instance_name
terraform plan
terraform apply
navigate to modules/instances/instances.tf remove tf-instance-3
terraform apply
Task 6. Use a module from the Registry
In the Terraform Registry, browse to the Network Module.
Copy and paste the following into the main.tf
module "vpc" {
source = "terraform-google-modules/network/google"
version = "~> 3.4.0"
project_id = "PROJECT_ID"
network_name = "VPC_NAME"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = "subnet-01"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-central1"
},
{
subnet_name = "subnet-02"
subnet_ip = "10.10.20.0/24"
subnet_region = "us-central1"
subnet_private_access = "true"
subnet_flow_logs = "true"
description = "This subnet has a description"
},
]
}
run cloud shell :
terraform init
terraform apply
Next, navigate to the instances.tf file and update the configuration resources to connect tf-instance-1 to subnet-01 and tf-instance-2 to subnet-02
resource "google_compute_instance" "tf-instance-1"{
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "VPC_NAME"
subnetwork = "subnet-01"
}
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
}
resource "google_compute_instance" "tf-instance-2"{
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "VPC_NAME"
subnetwork = "subnet-02"
}
metadata_startup_script = <<-EOT
#!/bin/bash
EOT
allow_stopping_for_update = true
}
module "vpc" {
source = "terraform-google-modules/network/google"
version = "~> 3.4.0"
project_id = "PROJECT_ID"
network_name = "VPC_NAME"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = "subnet-01"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-central1"
},
{
subnet_name = "subnet-02"
subnet_ip = "10.10.20.0/24"
subnet_region = "us-central1"
subnet_private_access = "true"
subnet_flow_logs = "true"
description = "This subnet has a description"
},
]
}
run cloud shell :
terraform init
terraform apply
Task - 6 : Configure a firewall
Add the following resource to the main.tf file and fill in the GCP Project ID:
resource "google_compute_firewall" "tf-firewall"{
name = "tf-firewall"
network = "projects/<PROJECT_ID>/global/networks/VPC_Name"
allow {
protocol = "tcp"
ports = ["80"]
}
source_tags = ["web"]
source_ranges = ["0.0.0.0/0"]
}
run cloud shell :
terraform init
terraform apply