Terraform & Infrastructure as Code (IaC)
1. What is Infrastructure as Code (IaC)?
First, imagine you want to create infrastructure in Google Cloud.
For example, you need:
2 Virtual Machines (VMs).
1 VPC network.
1 Firewall rule.
1 Load Balancer.
There are three ways to create these resources.
1. Google Cloud Console (GUI)
You manually click buttons and fill in details to create resources.
2. Cloud Shell (CLI)
You execute commands to create resources.
3. Terraform (Infrastructure as Code)
You write configuration files that describe the infrastructure you need. Terraform creates it automatically.
Simple definition:
Infrastructure as Code (IaC) means creating and managing cloud infrastructure using code instead of manually creating resources.
For example, instead of manually creating 10 VMs, you write a Terraform configuration and Terraform creates them for you.
2. Why do we need Infrastructure as Code?
Suppose your company has three environments:
Development environment
Testing environment
Production environment
You need almost identical infrastructure in all three environments.
Without IaC:
Manually create resources three times.
More time required.
Possibility of human errors.
Difficult to maintain consistency.
With IaC:
Write the configuration once.
Reuse the configuration.
Create infrastructure whenever needed.
Delete infrastructure when it is no longer required.
Important benefits:
Automation – Infrastructure gets created automatically.
Reusability – Reuse the same configuration.
Consistency – Development, testing and production can have similar configurations.
Scalability – Easily create more resources.
Cost saving – Delete temporary infrastructure when not needed.
Version control – Store infrastructure code in Git.
3. What is Terraform?
Terraform is an IaC tool that helps you create, update and delete cloud infrastructure using configuration files.
For example, Terraform can create:
Compute Engine virtual machines.
VPC networks.
Firewall rules.
Cloud Storage buckets.
Load Balancers.
VPN tunnels.
Cloud Routers.
Remember: Instead of manually creating each resource, you describe what you want in a file, and Terraform creates it.
4. What is Declarative Configuration?
This is a very important concept for interviews.
Terraform follows a declarative approach.
There are two approaches:
Approach | Meaning |
|---|---|
Imperative | Tell the system HOW to do something. |
Declarative | Tell the system WHAT you want. |
Example:
Imperative approach:
Create a VM.
Configure its machine type.
Attach a network.
Configure firewall rules.
Declarative approach (Terraform):
You simply declare:
"I need 2 VMs, one network and one firewall rule."
Terraform figures out the steps needed to achieve the desired configuration.
Terraform is declarative because we specify the desired state, and Terraform determines how to achieve it.
5. What is HCL?
HCL stands for HashiCorp Configuration Language.
It is the language used to write Terraform configuration files.
Example:
resource "google_compute_network" "my_vpc" {
name = "my-network"
auto_create_subnetworks = true
}
Here:
resource→ We want to create a resource.google_compute_network→ Resource type (Google Cloud VPC).my_vpc→ Terraform's local resource name.name→ Actual VPC name.auto_create_subnetworks→ Automatically create subnetworks in regions.
Terraform configuration files generally use the .tf extension.
Example: main.tf
6. Understanding Terraform configuration blocks
Your lecture mentions three important blocks.
A. Provider block
Provider tells Terraform which cloud platform to use.
Example:
provider "google" {
project = "my-project-id"
region = "asia-south1"
}
Here, Google Cloud is the provider.
B. Resource block
Resource specifies what infrastructure you want to create.
Example:
resource "google_compute_network" "my_vpc" {
name = "my-network"
auto_create_subnetworks = true
}
This creates a VPC network.
C. Output block
Output displays useful information after Terraform creates resources.
Example:
output "network_name" {
value = google_compute_network.my_vpc.name
}
This displays the created network name.
7. Understanding Terraform workflow
Terraform mainly uses three commands.
Write infrastructure configuration
Main .tf -Write infrastructure configuration
1. terraform init
Initialize Terraform and download providers
2. terraform plan
Preview what Terraform will change
3. terraform apply
Create or update infrastructure
Google Cloud Resources
Let's understand each command.
1. terraform init
terraform init
Initializes the Terraform working directory.
Downloads the required provider plugins.
Prepares Terraform to work with Google Cloud.
Think: Setup and initialization.
2. terraform plan
terraform plan
Checks your configuration.
Compares desired infrastructure with the current state.
Shows what Terraform plans to create, update or delete.
Does not make those infrastructure changes.
Think: Preview before execution.
3. terraform apply
terraform apply
terraform applyExecutes the planned changes.
Creates or updates the required infrastructure.
Usually asks for confirmation before proceeding.
Think: Actual deployment.
Bonus command:
terraform destroy
terraform destroyDeletes the infrastructure managed by the Terraform configuration (after confirmation).
Think: Remove infrastructure.
8. Practical example
Your instructor creates an auto-mode VPC network and an HTTP firewall rule.
Let's understand the configuration.
File name: main.tf
provider "google" {
project = "my-project-id"
region = "asia-south1"
}
resource "google_compute_network" "my_vpc" {
name = "my-network"
auto_create_subnetworks = true
mtu = 1460
}
resource "google_compute_firewall" "http_firewall" {
name = "allow-http"
network = google_compute_network.my_vpc.name
allow {
protocol = "tcp"
ports = ["80", "8080"]
}
}
What is happening?
Provider: Connects Terraform to Google Cloud.
Network: Creates an auto-mode VPC.
MTU: Sets the network MTU to 1460 bytes.
Firewall: Allows TCP traffic on ports 80 and 8080.
Port 80 is commonly used for HTTP traffic.
Now execute:
terraform init
terraform plan
terraform apply
Your VPC and firewall rule will be created.
9. What are Terraform modules?
Imagine you create the same infrastructure for 10 different applications.
Instead of writing the same configuration repeatedly, you can create reusable components called modules.
For example:
Network module
VM module
Firewall module
Database module
You can reuse these modules across multiple projects.
Think of a module as a reusable function in Java.
10. Terraform vs Cloud Shell
Feature | Cloud Shell | Terraform |
|---|---|---|
Type | Command-line environment | IaC tool |
Working method | Execute commands | Write configuration files |
Automation | Through scripts | Built-in declarative workflow |
Resource creation | Commands generally executed sequentially | Can create independent resources in parallel |
Reusability | Scripts | Modules and configurations |
Preview changes | Depends on command |
|
One important point: Terraform uses Google Cloud APIs to create and manage resources.
Interview-Ready Answer
What is Terraform?
Terraform is an Infrastructure as Code (IaC) tool developed by HashiCorp that allows us to provision and manage cloud infrastructure using declarative configuration files written in HCL. Instead of manually creating resources, we define the desired state in code, and Terraform automatically creates, updates, or removes the infrastructure. It supports multiple cloud providers and enables consistent, repeatable, and automated deployments.



Comments
Post a Comment