PART: 1 Terraform & Infrastructure as Code

🧱 What is Infrastructure as Code (IaC)?
Infrastructure as Code (IaC) is a modern DevOps practice that lets you define, provision, and manage your IT infrastructure using code instead of manually creating resources through a cloud provider's web interface.
Instead of clicking buttons on AWS, Azure, or GCP dashboards, you can write scripts to define infrastructure like:
Servers (EC2, VM, etc.)
Storage (S3, Azure Blob, etc.)
Databases (RDS, SQL, etc.)
Load balancers, VPCs, firewalls, and more.
The result?
- Your infrastructure is now version-controlled, reproducible, and automated — just like application code.
🌟 Enter Terraform
Terraform is a powerful, open-source Infrastructure as Code tool developed by HashiCorp.
While AWS has CloudFormation, Azure has ARM Templates, and OpenStack has Heat Templates, Terraform provides a cloud-agnostic solution that works across all major providers, including:
AWS
Azure
Google Cloud
OpenStack
Kubernetes
And many more
It solves the problem of tool overload by letting you use just one tool and language (HCL) to manage infrastructure across any environment.
🏢 Real-World Example: DevOps at Flipkart
Imagine you're working as a DevOps engineer at Flipkart, which runs around 300 applications.
To run these applications, you need to provision servers and other infrastructure resources. You have multiple hosting options:
AWS
Azure
Google Cloud
On-premises data centers (using tools like OpenStack)
✅ You Choose AWS
After evaluation, you decide to go with AWS. You start automating infrastructure using:
EC2 for compute
S3 for storage
RDS for databases
To avoid manual creation via the AWS Console, you use CloudFormation Templates (CFT) to automate resource provisioning.
Now, if a developer requests 10 EC2 instances, you can quickly deploy them by running your CFT script — no manual effort.
🚧 The Problem Begins...
What happens if Flipkart decides to switch from AWS to Azure?
Your CloudFormation templates become useless.
You now need to learn Azure's equivalent: ARM Templates.
If you move to on-prem infrastructure using OpenStack, you must now write Heat Templates.
Every time you change a provider, you:
Throw away your old scripts.
Learn a new tool.
Rebuild everything.
This leads to redundant work, increased complexity, and slower delivery.
🌐 The Hybrid Cloud Reality
In today’s world, most companies don’t use just one cloud. They operate in a Hybrid Cloud model:
Some services on AWS
Others on Azure
Sensitive workloads on on-prem
Now, as a DevOps engineer, you must know:
AWS CloudFormation
Azure Resource Manager
OpenStack Heat
This multiplies your learning curve and slows you down.
✅ Solution: Terraform to the Rescue
Terraform is designed to solve this exact problem.
Instead of learning different tools for every cloud provider, Terraform lets you write just one language, and use that same code to:
Provision infrastructure on any cloud
Manage hybrid environments
Scale across multiple providers
🧠 How Terraform Works Internally
Let’s say you want to create an EC2 instance on AWS.
Without Terraform:
You’d write a script or manually use AWS CLI.
Or you’d use Python, Shell, or HTTP tools to manually call AWS APIs.
With Terraform:
You write a simple, human readable Terraform config file.
Terraform internally:
Looks at the cloud provider’s APIs
Translates your config into API requests
Sends those requests to the provider
Creates the requested infrastructure
Terraform has built-in modules for different resources like EC2, S3, etc.
For example:
Instead of writing a full Python or shell script to make API calls to AWS, Terraform already knows what APIs to call.
You just write in HCL and let Terraform handle the API calls for you.
📡 What Is an API? (And Why It Matters)
📍 API = Application Programming Interface
APIs are the way software systems communicate programmatically with each other.
Example:
You open Google on your browser. That’s using a User Interface (UI).
But if a script wants to get data from Google, it will make an API call.
So, you can either:
Use a browser (manual),
Or send an HTTP GET/POST request (automated, programmatic).
💡 In DevOps:
AWS, Azure, GCP, OpenStack — all expose APIs.
These APIs allow you to create, read, update, delete infrastructure.
However, calling these APIs manually requires:
Coding (Python, Shell, etc.)
Authentication management
Payload formatting
🔁 API as Code – Terraform’s Magic
Terraform introduces the concept of API as Code:
You write declarative Terraform scripts and Terraform talks to the APIs for you.
Here’s how:
You write Terraform code (like “create EC2 instance”).
Terraform reads your intent.
It converts that into an API request.
Sends the request to the provider (AWS, Azure, etc.).
Receives the response and reports success/failure.
✅ You don’t write any API calls yourself — Terraform does it all under the hood.
💥 Summary: Why Terraform is a Game-Changer
| Feature | Value |
| ☁️ Cloud-Agnostic | Works with AWS, Azure, GCP, OpenStack, etc. |
| 🧠 One Language | Learn HCL once, use everywhere |
| ⚙️ API Abstraction | No need to write Python/Shell or raw API calls |
| 🛠️ Reusable Modules | Build once, use across environments |
| 📊 State Management | Tracks existing infrastructure |
| 📝 Human-Readable | Configs are easy to understand |
| 🔁 Portability | Easily migrate infra across clouds |
| 🌐 Supports Hybrid Cloud | Manage on-prem + cloud seamlessly |



