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
Then in your main.tf:
terraform { required_providers { google = { source = "hashicorp/google" } }
} provider "google" { project = var.project_id region = var.region zone = var.zone
}
In all your variables.tf files:
Variable “zone” { Default = ”ZONE”
} Variable “project_id” { Default = ”PROJECT_ID”
} Variable “region” { Default = ”REGION”
}
NOTE:
To be added in the VARIABLES.TF in the root, instances.tf and storage.tf
Then run:
terraform init
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:
- Instance name: Give your instance a descriptive name.
- Region and Zone: Choose the region and availability zone where you want to deploy your instance.
- Machine type: Select the machine type that suits your requirements, such as CPU and memory specifications.
- Boot disk: Choose the operating system and disk size for the boot disk.
- Networking: Configure network settings such as VPC network, subnetwork, external IP address, and firewall rules.
- 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" }
}
Then add the following to your main.tf:
module “instances”{ source = “./modules/instances”
}
Then run:
terraform init
Next:
Run this command:
terraform import module.instances.google_compute_instance.INSTANCE_NAME-1 INSTANCE_ID
Then:
terraform import module.instances.google_compute_instance.INSTANCE_NAME-2 INSTANCE_ID
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 }
Then add the following to your main.tf file:
module “compute_storage”{ source = “./modules/storage”
}
Then run:
terraform init
Then run:
terraform apply
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" }
Run:
terraform init
It should prompt you, type yes
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" }
}
Then run:
terraform init
Then:
terraform apply
Task 5
To delete resource: You just remove the third instance configuration from your instances.tf file and then run
terraform apply
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 }, ] }
After adding the block, run:
terraform init
Then run:
terraform apply
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]
}
run :
terraform init
then :
terraform apply
Discover more from Coursity
Subscribe to get the latest posts sent to your email.