Day 1: Introduction to IaC and Step-by-Step Terraform Setup

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.

  • 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.


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.

  • 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.


Here’s how I set up Terraform on my machine using Ubuntu/WSL:

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

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.

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.


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.

  1. Write Configuration: You define your infrastructure in .tf files using the HashiCorp Configuration Language (HCL).

  2. Plan: Terraform generates an execution plan that shows what actions it will take (e.g., creating, modifying, or deleting resources).

  3. Apply: Once you approve the plan, Terraform applies the changes to your cloud environment.

  4. 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.


Here’s a visual representation of the typical IaC flow:


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.

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.

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 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:



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

Popular posts from this blog

Day 4: Terraform State File and Remote Backend

Day 5: Understanding Terraform Variables in AWS