(Update) Automating Infrastructure On Google Cloud With Terraform - Challenge Lab
(Update) Automating Infrastructure On Google Cloud With Terraform - Challenge Lab
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>"
}
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.
If you are getting error after running above command just try this run terraform init -migrate -
state command first
Next, navigate to modules/instances/instances.tf. Copy the following configuration into the file:
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
}
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
module "storage" {
source = "./modules/storage"
}
terraform {
backend "gcs" {
bucket = "<REPLACE YOUR BUCKET>"
prefix = "terraform/state"
}
required_providers {
google = {
source = "hashicorp/google"
version = "3.55.0"
}
}
}
terraform init
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
}
terraform plan
terraform apply
terraform apply
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"
},
]
}
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
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
}
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"
},
]
}
Add the following resource to the main.tf file and fill in the GCP Project ID:
allow {
protocol = "tcp"
ports = ["80"]
}
source_tags = ["web"]
source_ranges = ["0.0.0.0/0"]
}