Day 1: Introduction to IaC and Step-by-Step Terraform Setup
What is Infrastructure as Code (IaC)?
Infrastructure as Code (IaC) refers to the practice of managing IT infrastructure through code rather than manual processes. This approach allows teams to define the infrastructure they need in configuration files, which can then be automatically provisioned, updated, or destroyed by IaC tools like Terraform.
Benefits of IaC:
Consistency: Ensures environments are identical by using the same configuration across all stages (dev, test, prod).
Automation: Reduces human error and speeds up the provisioning process.
Scalability: Infrastructure can be replicated or removed easily based on the needs.
Version Control: Just like application code, infrastructure code is tracked in Git, making rollbacks and updates easier.
Why Terraform?
Terraform by HashiCorp is one of the most widely used IaC tools. It enables you to define infrastructure using human-readable code, and it supports multiple cloud providers such as AWS, Azure, and Google Cloud. Terraform allows you to automate the provisioning, maintenance, and destruction of resources, making it an essential tool for DevOps teams.
Why Choose Terraform?
Multi-Cloud Support: You can manage infrastructure across different cloud platforms (AWS, Azure, GCP, etc.) from a single tool.
Version Control and Collaboration: Terraform configurations can be stored in Git repositories, enabling version control and collaboration between teams.
Reusable Configurations: Once you’ve defined your infrastructure in code, it can be reused in multiple environments.

Setting Up Terraform: A Step-by-Step Guide
Here’s how I set up Terraform on my machine using Ubuntu/WSL:
1. Install Terraform
First, I installed Terraform using the following commands on Ubuntu (or WSL for Windows users):
# For Ubuntu/Debian
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Once installed, I confirmed the version by running:
terraform -version
2. Setup Terraform Alias
To make working with Terraform more convenient, I set up an alias for the terraform command:
alias tf=terraform
Now, I could simply type tf instead of terraform, making the process faster.
3. Initialize a New Directory
I created a new directory for my Terraform project and initialized it with:
tf init
This prepares the directory for use with Terraform and downloads the necessary provider plugins.
How Terraform Works
Terraform uses a declarative approach to manage infrastructure. This means you define what you want your infrastructure to look like, and Terraform takes care of creating, updating, or destroying resources to match that desired state.
Terraform Workflow:
Write Configuration: You define your infrastructure in
.tffiles using the HashiCorp Configuration Language (HCL).Plan: Terraform generates an execution plan that shows what actions it will take (e.g., creating, modifying, or deleting resources).
Apply: Once you approve the plan, Terraform applies the changes to your cloud environment.
State Management: Terraform keeps track of the current state of your infrastructure in a state file. This file helps Terraform determine what changes are needed during future runs.
IaC Flow with Terraform
Here’s a visual representation of the typical IaC flow:

Practical Example: My Terraform Use Case
To understand how Terraform works in action, I wrote a simple configuration to provision a few resources: a virtual machine, a network, and a load balancer. Using Terraform, I could automate the entire process with a few lines of code.
Using Terraform to Provision Resources
With Terraform, I first defined the desired state of my infrastructure. I ran tf plan to see the proposed changes:
tf plan
This command showed me exactly what would happen when I applied the configuration. I then applied the changes with:
tf apply
Terraform created the resources, and I could easily scale them or tear them down using the same code.
Destroying Resources
When I no longer needed the resources, I ran:
tf destroy
Terraform automatically destroyed all the resources defined in my configuration, saving costs and ensuring that my environment was clean and consistent.
The Terraform State File
The state file is a key component of Terraform. It tracks the resources Terraform manages and their current state. This allows Terraform to detect any drift between the desired and actual state and apply the necessary changes.
Here’s a look at how Terraform works internally to manage and track state:

Video:
Conclusion
Terraform has proven to be an invaluable tool for automating infrastructure management. By enabling infrastructure as code, Terraform allows teams to maintain consistency, reduce human error, and automate tasks that were once manual and error-prone. The ability to define, provision, and manage infrastructure as code makes Terraform essential for modern DevOps practices.
If you're looking to implement Infrastructure as Code in your workflows, Terraform is an excellent place to start. Whether you're managing a few resources or deploying complex cloud environments, Terraform’s power, flexibility, and multi-cloud support make it a top choice for infrastructure automation.
Comments
Post a Comment