Build Infrastructure with Terraform on Google Cloud

TASK TO BE DONE:
Task 1. Create the configuration files
Task 2. Import infrastructure
Task 3. Configure a remote backend
Task 4. Modify and update infrastructure
Task 5. Destroy resources
Task 6. Use a module from the Registry
Task 7. Configure a firewall

Task 1. Create the configuration files

main.tf
variables.tf
modules/
└── instances
├── instances.tf
├── outputs.tf
└── variables.tf
└── storage
├── storage.tf
├── outputs.tf
└── variables.tf

Step 1:

Run:

mkdir -p modules/storage modules/instances

Run:
touch modules/instances/variables.tf modules/instances/outputs.tf modules/instances/instances.tf modules/storage/variables.tf modules/storage/outputs.tf modules/storage/storage.tf

Run:
touch main.tf outputs.tf variables.tf

Image description

Then in your main.tf:

terraform { required_providers { google = { source = "hashicorp/google" } }
} provider "google" { project = var.project_id region = var.region zone = var.zone
} 
Enter fullscreen mode
Exit fullscreen mode

Image description

In all your variables.tf files:

Variable “zone” { Default = ”ZONE”
} Variable “project_id” { Default = ”PROJECT_ID”
} Variable “region” { Default = ”REGION”
} 
Enter fullscreen mode
Exit fullscreen mode

NOTE:
To be added in the VARIABLES.TF in the root, instances.tf and storage.tf

Image description

Then run:

terraform init
Enter fullscreen mode
Exit fullscreen mode

Image description

Task 2. Import Terraform configuration

Step 1:
STEPS IN CREATING VMs

Navigate to Google Cloud Console

  • Select or Create a Project: if you haven’t already, create a new project or select an existing project where you want to create your compute instances
  • Navigate to Compute Engine: in the Google Cloud Console, navigate to the Compute Engine section. You can find it in the left sidebar under the “Compute” section.
  • Create a New Instance: click on the “Create” button to create a new virtual machine instance.
  • Configure Instance Details: fill out the necessary details for your instance, including:
  1. Instance name: Give your instance a descriptive name.
  2. Region and Zone: Choose the region and availability zone where you want to deploy your instance.
  3. Machine type: Select the machine type that suits your requirements, such as CPU and memory specifications.
  4. Boot disk: Choose the operating system and disk size for the boot disk.
  5. Networking: Configure network settings such as VPC network, subnetwork, external IP address, and firewall rules.
  6. Advanced Configuration (Optional): you can also configure additional options such as GPU, metadata, and startup scripts if needed.
  • Review and Create: review the configuration of your instance to ensure everything is correct.
  • Once you’re satisfied, click the “Create” button to provision the instance.

Repeat to create another instance. We’re creating the instances so we can import them later into our terraform configuration and terraform can be responsible for managing them.

Step 2:
In your instance.tf:

resource "google_compute_instance" "INSTANCE_NAME-1" { name = "tf-instance-1" machine_type = "e2-micro" zone = var.zone boot_disk { initialize_params { image = "debian-cloud/debian-11" Size = “10” } } network_interface { network = "default" }
} resource "google_compute_instance" "INSTANCE_NAME-2" { name = "tf-instance-2" machine_type = "e2-micro" zone = var.zone boot_disk { initialize_params { image = "debian-cloud/debian-11" Size = “10” } } network_interface { network = "default" }
} 
Enter fullscreen mode
Exit fullscreen mode

Image description

Then add the following to your main.tf:

module “instances”{ source = “./modules/instances”
}
Enter fullscreen mode
Exit fullscreen mode

Image description

Then run:

terraform init
Enter fullscreen mode
Exit fullscreen mode

Image description

Next:
Run this command:

terraform import module.instances.google_compute_instance.INSTANCE_NAME-1 INSTANCE_ID
Enter fullscreen mode
Exit fullscreen mode

Then:

terraform import module.instances.google_compute_instance.INSTANCE_NAME-2 INSTANCE_ID
Enter fullscreen mode
Exit fullscreen mode

Image description

NOTE: INSTANCE_ID can be gotten from the console.

Task 3. Configure a remote backend

In your storage.tf file add the following

resource "google_storage_bucket" "STORAGE_BUCKET" { name = "# REPLACE WITH YOUR PROJECT ID" location = "US" uniform_bucket_level_access = true Force_destroy = true } 
Enter fullscreen mode
Exit fullscreen mode

Image description

Then add the following to your main.tf file:

module “compute_storage”{ source = “./modules/storage”
} 
Enter fullscreen mode
Exit fullscreen mode

Image description

Then run:

terraform init
Enter fullscreen mode
Exit fullscreen mode

Image description

Then run:

terraform apply
Enter fullscreen mode
Exit fullscreen mode

Image description

After the bucket is created add the following to your main.tf in the terraform block

 backend "gcs" { bucket = "# REPLACE WITH YOUR BUCKET NAME" prefix = "terraform/state" }
Enter fullscreen mode
Exit fullscreen mode

Image description

Run:

terraform init 
Enter fullscreen mode
Exit fullscreen mode

It should prompt you, type yes

Image description

Task 4

To update infrastructure:

You only need to change machine-type argument from “e2-micro” to the type provided. This should be done in your instances.tf file

The add a new instance resource in the same file

resource "google_compute_instance" "THIRD INSTANCE NAME" { name = "THIRD INSTANCE NAME" machine_type = "SPECIFIED MACHINE TYPE" zone = var.zone boot_disk { initialize_params { image = "debian-cloud/debian-11" Size = “10” } } network_interface { network = "default" }
} 
Enter fullscreen mode
Exit fullscreen mode

Image description

Image description

Image description

Then run:

terraform init
Enter fullscreen mode
Exit fullscreen mode

Image description

Then:

terraform apply 
Enter fullscreen mode
Exit fullscreen mode

Image description

Task 5

To delete resource: You just remove the third instance configuration from your instances.tf file and then run

terraform apply
Enter fullscreen mode
Exit fullscreen mode

Image description

Task 6
Add to your main.tf file

module "vpc" {
source = "terraform-google-modules/network/google"
version = "9.0.0"
project_id = "<PROJECT ID>"
network_name = "VPC_NAME"
routing_mode = "GLOBAL" subnets = [ { subnet_name = "subnet-01" subnet_ip = "IP_RANGE" subnet_region = var.region }, { subnet_name = "subnet-02" subnet_ip = "IP_RANGE" subnet_region = var.region }, ] }
Enter fullscreen mode
Exit fullscreen mode

Image description

Image description

Image description

After adding the block, run:

terraform init 
Enter fullscreen mode
Exit fullscreen mode

Image description

Then run:

terraform apply
Enter fullscreen mode
Exit fullscreen mode

Image description

Task 7

Add to your main.tf file:

resource “google_compute_firewal”l “FIREWALL_NAME”{ name = “FIREWALL_NAME” network = module.vpc.network_name allow { protocol = "tcp" ports = ["80"] } source_ranges = [0.0.0.0/0]
} 
Enter fullscreen mode
Exit fullscreen mode

Image description

run :

terraform init 
Enter fullscreen mode
Exit fullscreen mode

Image description

then :

terraform apply
Enter fullscreen mode
Exit fullscreen mode

Image description


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top