Chapter 1: Infrastructure as Code - Here We Go
Before we get to Terraform, let me take you back in time so we understand why Terraform exist at first place.
If you go back two decades, everyone used those physical servers (produced by IBM, HP, and Cisco), which took weeks to setup correctly before we could run the applications on them.
Then came the time of virtualization. Sharing computing resources across multiple OS installations using hypervisor-based virtualization technologies such as VMware became the new normal. It reduced the time to spin up a server to run your application but also increased complexity.
Subsequently, we got AWS, which revolutionized computing, and a new era of cloud computing became streamlined. After AWS, other big tech companies such as Microsoft and Google launched their cloud offerings named Azure and Google Cloud Platform, respectively.
In the cloud, you can spin up a server in a few minutes with just a few clicks. Creating and managing a few servers was very easy, but as the number of servers and their configurations grew, manual tracking became a significant challenge.
That’s where Infrastructure as Code (IaC) and Terraform came to the rescue, and trust me, once you understand what they can do, you’ll wonder how you ever lived without them.
What is Infrastructure as Code?
Infrastructure as Code is exactly what it sounds like – managing and provisioning your infrastructure (servers, networks, databases, etc.) through code instead of manual processes. Instead of clicking through web consoles or running manual commands, you write code that describes what you want your infrastructure to look like.
The Problems IaC Solves
Manual configuration chaos and deployment failures
- “It works on my machine” syndrome
- Scaling nightmares across multiple environments
- Lost documentation and tribal knowledge
- Slow disaster recovery
Then came Terraform, and it changed the game
So what is Terraform? Terraform is an open-source Infrastructure as Code tool developed by HashiCorp that makes managing infrastructure as simple as writing a shopping list.
Here’s what makes Terraform special:
1. It’s Written in Go
Terraform is built in Golang, which gives it superpowers for creating infrastructure in parallel. While other tools are still thinking about what to do, Terraform is already building your servers, networks, and databases simultaneously.
2. Uses HCL (HashiCorp Configuration Language)
Terraform uses HCL, which is designed to be human-readable and easy to understand. Don’t worry if you haven’t heard of HCL – it’s so intuitive that you’ll be writing infrastructure code in no time.
Here’s a simple example of what Terraform code looks like:
resource "aws_instance" "web_server" {
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "My Web Server"
Environment = "Production"
}
}
See how readable that is? We’re creating an AWS instance (a virtual server) called “web_server” with specific settings. Even if you’ve never seen Terraform code before, you can probably guess what this does.
3. Cloud-Agnostic Magic
Here’s where Terraform really shines – it works with ANY cloud provider. AWS, Azure, Google Cloud, DigitalOcean, even on-premises systems. You learn Terraform once, and you can manage infrastructure anywhere.
4. State Management
Terraform keeps track of what it has created in something called a “state file.” This means it knows exactly what exists and what needs to be changed, created, or destroyed. It’s like having a super-smart assistant who remembers everything.
Why Terraform Became the King of IaC
You might be wondering: “Why should I learn Terraform when there are other tools like AWS CloudFormation or Azure Resource Manager?”
Great question! Here’s why Terraform has become the go-to choice for infrastructure management:
1. One Tool to Rule Them All
Most cloud providers have their own IaC tools (AWS CloudFormation, Azure ARM templates, etc.), but they only work with their specific cloud. Terraform works with over 1,000 providers, from major cloud platforms to niche services. Learn it once, use it everywhere.
2. Huge Community and Ecosystem
Terraform has a massive community creating and sharing modules (think of them as infrastructure blueprints). Need to set up a web application with a database? There’s probably a module for that. Want to configure monitoring? There’s a module for that too.
3. Declarative Approach
With Terraform, you describe what you want (the end state), not how to get there. You say “I want a web server with these specifications,” and Terraform figures out all the steps needed to make it happen.
4. Plan Before You Apply
One of Terraform’s best features is the ability to see exactly what changes will be made before applying them. It’s like having a crystal ball that shows you the future of your infrastructure.
Real-World Example: Why You Need This
Let me paint you a picture of why this matters. Imagine you’re working at a company that needs to:
- Deploy a web application across development, staging, and production environments.
- Ensure all environments are identical
- Scale up during peak times
- Quickly recover from disasters
- Maintain security and compliance standards
Without Terraform: You’d spend weeks manually setting up each environment, documenting every step, praying nothing breaks, and probably making small mistakes that cause mysterious issues months later.
With Terraform: You write the infrastructure code once, test it in development, then deploy identical environments to staging and production with a single command. Need to scale up? Change a number in your code and redeploy. Disaster recovery? Run the same code in a different region.