Skip to main content
Terraform Basics
CHAPTER 01

Introduction to Terraform and Infrastructure as Code

Updated: May 15, 2026
15 min read

# CHAPTER 1

Introduction to Terraform and Infrastructure as Code

1. Introduction

Welcome to the modern era of cloud computing. For decades, building a data center meant ordering physical servers, waiting weeks for delivery, and manually plugging in cables. When the cloud (like AWS or Azure) arrived, we stopped plugging in cables and started clicking buttons on a website. But clicking buttons is slow, error-prone, and impossible to replicate. The next evolution is Infrastructure as Code (IaC). Terraform is the undisputed industry leader in IaC. In this chapter, we will learn how Terraform allows you to define entire data centers using code, spinning up hundreds of servers in seconds with mathematical precision.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Infrastructure as Code (IaC) and explain its necessity in modern DevOps.
  • Differentiate between manual cloud provisioning and automated provisioning.
  • Explain the concept of "Declarative" infrastructure.
  • Understand the core Terraform workflow (Write, Plan, Apply).
  • Articulate the business benefits of infrastructure automation.

3. Beginner-Friendly Explanation

Imagine building a custom house.
  • The Old Way (Manual Provisioning): You stand in an empty field. You tell a builder: "Put a brick here. Now put a brick next to it. Now get some wood." If you want a second house exactly the same, you have to stand in the field and repeat all the instructions perfectly. You will probably make a mistake.
  • The Terraform Way (Infrastructure as Code): You hire an architect and draw a highly detailed Blueprint. You hand the Blueprint to a magical robot (Terraform). The robot reads the Blueprint and builds the house perfectly in 10 seconds. If you want 50 identical houses, you just tell the robot: "Build this Blueprint 50 times."

Terraform takes your written blueprint (code) and automatically builds the cloud servers (the house).

4. What is Infrastructure as Code (IaC)?

IaC is the practice of managing and provisioning computing infrastructure through machine-readable definition files, rather than physical hardware configuration or interactive configuration tools (like the AWS Web Console). By treating our servers, databases, and firewalls as *code*, we gain immense benefits:
  • Version Control: We can save our infrastructure in GitHub. If a server is misconfigured, we can "Roll Back" our code to yesterday's version to fix it instantly.
  • Disaster Recovery: If a hurricane destroys a data center, we don't have to spend a month rebuilding it. We just run our Terraform code in a different region, and our entire company is back online in 10 minutes.
  • Peer Review: Before a database is created, senior engineers can review the Terraform code to ensure it meets security standards.

5. Declarative vs. Imperative

This is a critical concept for interviews.
  • Imperative (Bash Scripts, Chef): Focuses on the *HOW*. "Log into server. Run 'apt-get install nginx'. Start the service." It is a list of steps. If the script fails halfway, your system is in a broken state.
  • Declarative (Terraform): Focuses on the *WHAT*. You simply declare: "I want a server running Nginx." You do not tell Terraform *how* to do it. Terraform calculates the necessary steps, talks to the cloud APIs, and makes reality match your declaration. If reality already matches your declaration, Terraform does absolutely nothing.

6. Mini Project: The Terraform Workflow

While we will install Terraform in the next chapter, you must understand the holy trinity of the Terraform workflow:
  1. 1. Write: You author configuration files in a language called HCL (HashiCorp Configuration Language).
  1. 2. Plan (terraform plan): Terraform reads your code and creates an execution plan. It tells you exactly what it *will* do without actually doing it. (e.g., "I will create 1 server and destroy 0 servers.")
  1. 3. Apply (terraform apply): Upon your approval, Terraform executes the plan, interacting with cloud APIs to build the infrastructure.

7. Real-World Scenarios

A retail company was preparing for Black Friday. They expected 10x their normal website traffic. Because they had built their AWS infrastructure manually over 3 years, nobody knew exactly how to duplicate it to handle the load. They had to guess, click buttons for a week, and hope they didn't miss a firewall rule. The website crashed. The following year, they adopted Terraform. When Black Friday approached, they simply changed a single line of code in their Terraform file: instance_count = 50, ran terraform apply, and the cloud scaled instantly and perfectly, handling the traffic without a glitch.

8. Best Practices

  • Never Click Buttons: Once you adopt Terraform, you must establish a strict rule: "Nobody is allowed to create or modify resources using the Cloud Web Console." If an engineer clicks a button to change a firewall rule manually, Terraform doesn't know about it. The next time Terraform runs, it will see that reality doesn't match the code, and it will *delete* the engineer's manual change to enforce the blueprint.

9. Security Recommendations

  • Auditability: Because all infrastructure is defined in code, security teams can run automated scanners (like Checkov or tfsec) against your Terraform files *before* the infrastructure is even built. This allows you to catch open firewalls or unencrypted databases during the coding phase, rather than discovering them during a live hack.

10. Troubleshooting Tips

  • Mental Shift: If your server breaks, do not SSH into the server to fix it. Fix the Terraform code, and let Terraform fix the server. Maintaining the integrity of the code as the "Single Source of Truth" is paramount.

11. Exercises

  1. 1. Explain the difference between Declarative and Imperative infrastructure management.
  1. 2. What are three business benefits a company gains by transitioning from manual cloud provisioning to Infrastructure as Code?

12. FAQs

Q: Does Terraform only work with AWS? A: No! Terraform is "cloud-agnostic." It can manage AWS, Azure, Google Cloud, DigitalOcean, GitHub, and even local VMware clusters. You use the exact same workflow (plan and apply) regardless of the provider.

13. Interview Questions

  • Q: Define Infrastructure as Code (IaC) and contrast the Declarative paradigm used by Terraform with the Imperative paradigm used by traditional shell scripting.
  • Q: A developer manually modifies a Security Group rule in the AWS Console to allow SSH access. When the CI/CD pipeline runs terraform apply later that day, what will happen to that Security Group rule and why?

14. Summary

In Chapter 1, we introduced the paradigm shift of Infrastructure as Code. We recognized that manual, click-ops cloud management is unscalable and dangerous. By adopting Terraform's declarative model, we transition to defining our desired end-state in version-controlled blueprints. We explored the core Write -> Plan -> Apply workflow, establishing that Terraform serves as the single source of truth for our infrastructure, capable of building, modifying, and destroying cloud resources with mathematical consistency.

15. Next Chapter Recommendation

We understand the theory and the workflow. Now, we need the tool. Proceed to Chapter 2: Installing and Configuring Terraform.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·