As more and more organizations move to the cloud, the need for powerful infrastructure automation tools has never been greater. That's where Terraform comes in, a cutting-edge tool that allows you to automate the deployment, management, and scaling of your infrastructure with ease. With Terraform, you can define your entire infrastructure as code, making it easy to maintain, version, and reproduce. Whether you're deploying infrastructure to AWS, Google Cloud, or another cloud provider, Terraform provides a powerful and flexible platform for automating your workflows. In this blog post, we'll take a look at how to automate infrastructure on google cloud with terraform.
Task A. Create the configuration files Task B. Import infrastructure
Task C.Configure a remote backend
Task D. Modify and update infrastructure
Task E.Destroy resources
Task F. Use a module from the Registry
Task G. Configure a firewall
................................................................................................................
Task A. Create the configuration files
1.In Cloud Shell, create your Terraform configuration files and a directory structure that resembles the following:
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
- Fill out the variables.tf files in the root directory and within the modules. Add three variables to each file: region, zone, and project_id. For their default values, use us-east1, <filled in at lab start>, and your Google Cloud Project ID.
variable "region" {
default = "us-central1"
}
variable "zone" {
default = "us-central1-a"
}
variable "project_id" {
default = "<FILL IN PROJECT ID>"
}
3.Add the Terraform block and the Google Provider to the main.tf
file. Verify the zone argument is added along with the project and region arguments in the Google Provider block
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "4.47.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}
4.Initialize Terraform.
Run terraform init
in Cloud Shell in the root directory to initialize terraform.
.............................................................
Task B. Import infrastructure:
1.
Go to Compute Engine > VM Instances. Click on tf-instance-1. Copy the Instance ID down somewhere to use later.
Go to Compute Engine > VM Instances. Click on tf-instance-2. Copy the Instance ID down somewhere to use later.
First, add the module reference to the end of main.tf
module "instances" {
source = "./modules/instances"
}
Next, Copy the following configuration into the file modules/instances/instances.tf:
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
}
2.use the terraform import command to import them into your instances module.
To import the first instance, use the following command, using the Instance ID for tf-instance-1 you copied down earlier.
terraform import module.instances.google_compute_instance.tf-instance-1 <Instance ID - 1>
To import the second instance, use the following command, using the Instance ID for tf-instance-2 you copied down earlier.
terraform import module.instances.google_compute_instance.tf-instance-2 <Instance ID - 2>
3.Apply your changes. Note that since you did not fill out all of the arguments in the entire configuration, the apply will update the instances in-place.
terraform plan
terraform apply
.......................................................................................
Task C:Configure a remote backend
1.Create a Cloud Storage bucket resource inside the storage module. For the bucket name, use Bucket Name.
Add the following code to the modules/storage/storage.tf file:
resource "google_storage_bucket" "storage-bucket" {
name = "Enter bucket_name"
location = "US"
force_destroy = true
uniform_bucket_level_access = true
}
Next, add the following to end of the main.tf file:
module "storage" {
source = "./modules/storage"
}
Run the following 2 commands in cloud shell:
terraform init
terraform apply
Configure this storage bucket as the remote backend inside the main.tf file. Be sure to use the prefix terraform/state so it can be graded successfully. Next, update the main.tf file so that the terraform block looks like the following
terraform {
backend "gcs" {
bucket = "Enter bucket_name"
prefix = "terraform/state"
}
required_providers {
google = {
source = "hashicorp/google"
version = "4.47.0"
}
}
}
3.Run the following to initialize the remote backend.
terraform init
....................................................................................................................
Task D: Modify and update infrastructure
1.Navigate to modules/instances/instance.tf. Replace the entire contents of the file with the following:
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
resource "google_compute_instance" "tf-instance-3" {
name = "tf-instance-3"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
2.Run the following commands
terraform init
terraform apply
........................................................................................................
Task E.Destroy resources
1.Taint the tf-instance-3 resource by running the following command:
terraform taint module.instances.google_compute_instance.tf-instance-3
2.Run the following commands to apply the changes:terraform init
terraform apply
3.Remove the following chunk of code from instances.tf file:
resource "google_compute_instance" "tf-instance-3" {
name = "tf-instance-3"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "default"
}
}
4.run the following command:
terraform apply
......................................................................................................
Task F. Use a module from the Registry :
1.Copy and paste the following code at the end of the main.tf file:
module "vpc" {
source = "terraform-google-modules/network/google"
version = "6.0.0"
project_id = "Enter your project id here"
network_name = "Enter VPC Name"
routing_mode = "GLOBAL"
subnets = [
{
subnet_name = "subnet-01"
subnet_ip = "10.10.10.0/24"
subnet_region = "us-east1"
},
{
subnet_name = "subnet-02"
subnet_ip = "10.10.20.0/24"
subnet_region = "us-east1"
}
]
}
2.Run the following commands:
terraform init
terraform apply
3.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.
Navigate to modules/instances/instances.tf. Replace the entire contents of the file with the following:
resource "google_compute_instance" "tf-instance-1" {
name = "tf-instance-1"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "Enter vpc_name"
subnetwork = "subnet-01"
}
}
resource "google_compute_instance" "tf-instance-2" {
name = "tf-instance-2"
machine_type = "n1-standard-2"
zone = var.zone
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
network_interface {
network = "Enter vpc_name"
subnetwork = "subnet-02"
}
}
4.Run the following commands:
terraform init
terraform apply
........................................................................................................
Task G. Configure a firewall:
1.Create a firewall rule resource in the main.tf file, and name it tf-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/<Enter your PROJECT_ID>/global/networks/Enter VPC_Name"
allow {
protocol = "tcp"
ports = ["80"]
}
source_tags = ["web"]
source_ranges = ["0.0.0.0/0"]
}
2.Run the following commands:
terraform init
terraform apply
..........................................................................................................
Congratulations , You have completed the lab😀
Comments
Post a Comment