Table Of Contents

Back to Labs Content

  • Software Architecture
  • Cloud Computing

Terraform Labs: Automating Google Cloud Infrastructure Deployment

Sunday, February 16, 2025 at 2:15:22 AM GMT+8


How Does Terraform Work?

1. Declarative Configuration

Terraform uses a special language called HashiCorp Configuration Language (HCL). Instead of writing step-by-step instructions like in traditional programming, you just describe what you want (e.g., "I need a virtual machine with 2 CPUs and 4GB RAM"), and Terraform figures out how to make it happen.

2. Configuration Interpretation

When you run a Terraform command, it reads your configuration files and understands what infrastructure you want to create or update.

3. Interaction with Cloud APIs

Terraform then communicates with cloud providers like Google Cloud, AWS, or Azure by sending API requests. This tells the cloud provider to create, update, or delete the resources you defined.

4. Execution by Cloud Providers

The cloud provider takes Terraform’s instructions and builds your infrastructure—creating things like virtual machines, networks, and storage based on your configuration.

5. State Management

Terraform keeps track of everything it has created in a state file. This file helps Terraform know what’s already there, so it only makes necessary changes when you update your configuration.

Why Use Terraform?

Terraform makes infrastructure management simpler, repeatable, and error-free. Instead of manually clicking around in cloud dashboards, you can automate everything with a few lines of code. This saves time and reduces mistakes.

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open-source tool that codifies APIs into declarative configuration files that can be shared among team members, treated as code, edited, reviewed, and versioned.

In this lab, you create a Terraform configuration with a module to automate the deployment of Google Cloud infrastructure.

Task 1: Setting Up Terraform and Cloud Shell

Installing Terraform

Terraform is pre-installed in Cloud Shell. Verify the installed version.

1 Open Google Cloud Console and click Activate Cloud Shell.

2 If prompted, click Continue.

3 Run the following command to check the Terraform version:

terraform --version

- Expected output:

Terraform v1.3.3
Note: These lab instructions work with Terraform v1.3.3 and later.

4 Create a directory for Terraform configurations:

mkdir tfinfra

5 Open Cloud Shell Editor and navigate to the tfinfra folder.

Initializing Terraform

Terraform uses plugins to support various cloud providers. Initialize Terraform by setting Google as the provider.

1 Create a new file named provider.tf tfinfra folder.

2 Add the following configuration:

provider "google" {}

3 Save the file.

4 Run the Terraform initialization command:

cd tfinfra
terraform init

- Expected output:

provider.google: version = "~> 4.43.0"
Terraform has been successfully initialized!

Task 2: Creating mynetwork and Its Resources

Configuring mynetwork

1 Create a new file named mynetwork.tf inside tfinfra.

2 Add the following configuration:

resource "google_compute_network" "mynetwork" {
  name                    = "mynetwork"
  auto_create_subnetworks = true
}

3 Save the file.

Configuring Firewall Rules

4 Add the following firewall rules to mynetwork,tf:

resource "google_compute_firewall" "mynetwork-allow-http-ssh-rdp-icmp" {
  name    = "mynetwork-allow-http-ssh-rdp-icmp"
  network = google_compute_network.mynetwork.self_link

  allow {
    protocol = "tcp"
    ports    = ["22", "80", "3389"]
  }
  allow {
    protocol = "icmp"
  }
  source_ranges = ["0.0.0.0/0"]
}

5 Save the file.

Configuring VM Instance

1 Create a new folder named instance inside tfinfra.

2 Create a new file main.tf inside the instance folder.

3 Add the following basic configuration:

resource "google_compute_instance" "vm_instance" {
  name         = "my-vm-instance"
  machine_type = "e2-medium"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }
  network_interface {
    network = google_compute_network.mynetwork.self_link
  }
}

4 Save the file.

To rewrite the Terraform configuration files to a canonical format and style, run the following command:

terraform fmt

To initialize Terraform, run the following command:

terraform init

Expected output:

...
* provider.google: version = "~> 4.43.0"

Terraform has been successfully initialized!

Conclusion

You have successfully set up Terraform in Cloud Shell and created configurations to deploy Google Cloud infrastructure, including a VPC network, firewall rules, and VM instances. Terraform’s execution workflow ensures smooth infrastructure deployment with minimal manual intervention. This setup can be expanded with additional configurations and modules to efficiently automate more complex infrastructure deployments.

Happy learning and coding with Terraform!


Another Recommended Labs Content

DatabaseDatabase ArchitectureSoftware ArchitectureSystem Design

Understanding Database Partitioning vs Sharding: Concepts, Benefits, and Challenges

When dealing with large volumes of data, efficient database management becomes essential. Two widely used techniques to improve performance and scalability are database partitioning and database sharding. Although often confused, these approaches differ fundamentally in architecture, complexity, and suitable use cases. This article explores these differences in detail, helping you decide which fits your application best.

Domain-Driven Design (DDD) is a powerful approach to software development that places the business domain—not the technology—at the center of your design decisions. First introduced by Eric Evans, DDD is essential for developers and architects who want to build systems that reflect real-world complexity and change.

Message BrokerSystem DesignSoftware Architecture

Understanding RabbitMQ: A Favorite Simple Messaging Service!

RabbitMQ is a robust, open-source message broker that facilitates communication between applications by sending and receiving messages. Whether you're building a microservices architecture or a distributed system, RabbitMQ ensures reliable, scalable, and asynchronous messaging. In this blog, we’ll walk through its core components and concepts, from producers to consumers, and dive into some advanced features like round-robin dispatching and virtual hosts.