Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 6

Terraform: Defining IaC Responsibilities

Recommended resources

Hello! Welcome to your next lesson.

In our last session, we explored how Docker allows us to package an application and its entire environment into a portable container. This brilliantly solves the "it works on my machine" problem by ensuring consistency from development to production.

But this raises a new question: where do these containers run? They run on infrastructure—servers, networks, firewalls, and databases. As someone who has managed web servers, you know that setting up this infrastructure manually is time-consuming and prone to error. How can we apply the same principles of automation and version control that we use for our application code to our infrastructure?

Today, you will learn to outline the responsibilities of Infrastructure as Code (IaC) tools like Terraform. We'll move beyond the manual-click-in-the-cloud-console approach and discover how to define, deploy, and manage entire data centers using code.

The Problem with Manual Infrastructure Management

Imagine you need to set up a new staging environment for your Laravel application. Using a cloud provider's web dashboard, you would manually:

  • Create a virtual server (an "instance" or "droplet").
  • Configure its networking and security group rules.
  • Provision a managed MySQL database.
  • Set up a Redis instance for caching.

This process is not only slow but also introduces significant risks:

  • Inconsistency: Did you remember to configure the staging environment exactly like production? A tiny difference in a firewall rule or server size can lead to bugs that are hard to diagnose.
  • Lack of Audit Trail: Who changed the production firewall rules last Tuesday? Without a version history, it's impossible to know.
  • Difficulty in Replication: If a disaster strikes and you need to rebuild your infrastructure from scratch, how long would it take?

The following video effectively illustrates these challenges, especially in a world where companies use multiple cloud providers.

Day-16 | Infrastructure as Code | #terraform #IaC

Watch this segment from Abhishek.Veeramalla to understand the core problem that Infrastructure as Code solves. The video uses the example of an engineer at a large company to demonstrate the challenges of managing infrastructure across different platforms.

Watch from 00:56 to 09:18. Pay attention to the issues that arise when trying to automate infrastructure using provider-specific tools (like AWS CloudFormation vs. Azure Resource Manager) and the concept of a hybrid cloud.

As the video highlights, relying on provider-specific tools locks you into one ecosystem and forces you to learn multiple systems. This is inefficient and doesn't scale well.


The Solution: Infrastructure as Code (IaC)

Infrastructure as Code is the practice of managing and provisioning infrastructure through machine-readable configuration files, rather than through a graphical user interface. This means you treat your infrastructure—servers, databases, networks—just like your application code.

How Infrastructure as Code Works
This diagram shows the core IaC workflow. A developer writes infrastructure code, which is stored in version control (like Git). An IaC tool then reads this code and automatically provisions the corresponding infrastructure.

By adopting IaC, you gain the same benefits for your infrastructure that you have for your application code:

  • Automation: Create, update, and destroy entire environments with a single command.
  • Consistency: Guarantee that your staging and production environments are identical.
  • Version Control: Track every change, review modifications through pull requests, and easily roll back if something goes wrong.
  • Collaboration: Teams can work on infrastructure configuration concurrently, just as they do with a shared codebase.

For a concise summary of these principles from the creators of Terraform, have a look at the following document.

What is Infrastructure as Code with Terraform?

This article from HashiCorp, the company behind Terraform, provides a clear and official definition of Infrastructure as Code and its advantages.

Read the introduction, which starts with 'Infrastructure as Code (IaC) tools allow you...' and ends with '...safely collaborate on infrastructure.'


Terraform's Core Responsibilities

Terraform has emerged as a leading IaC tool because of its powerful and flexible approach. Its primary responsibility is to create, manage, and update infrastructure resources across a vast ecosystem of providers. Let's break down how it accomplishes this.

1. A Declarative, Multi-Cloud Approach

The most important concept to grasp about Terraform is that it is declarative. This means you write code that describes your desired end state, and Terraform figures out the steps needed to achieve it.

For example, you don't write commands like "create a server, then configure the network." Instead, you declare: "I want one server with these specifications and one network with these rules."

This contrasts with an imperative approach, where you must specify each step in the process. The declarative model is far more robust for managing complex systems.

This video clip explains the declarative approach and its benefits beautifully.

Terraform explained in 15 mins | Terraform Tutorial for Beginners

Let's watch a segment from TechWorld with Nana that clarifies the declarative model and distinguishes Terraform from other tools.

Please watch two clips from this video: 00:25 - 02:00: This introduces Terraform and the concept of provisioning. 12:45 - 14:35: This part provides an excellent explanation of the 'declarative' vs. 'imperative' approach, which is fundamental to understanding Terraform.

2. Provider-Based Architecture

How can Terraform manage resources on AWS, DigitalOcean, and even GitHub simultaneously? It does this through a plugin-based architecture centered on Providers. A provider is a plugin responsible for understanding the API of a specific service.

Terraform Architecture Diagram
This diagram shows how Terraform Core interacts with providers. The user writes configuration files (.tf). Terraform Core processes them and uses the appropriate provider (e.g., AWS, Azure) to communicate with the target API. The state file tracks the created resources.

When you declare you want an AWS EC2 instance, Terraform uses the AWS provider, which knows how to make the correct API calls to AWS to create that instance. With over 1,000 providers available, you can manage almost any part of your technology stack with Terraform.

What is Infrastructure as Code with Terraform?

To solidify your understanding of providers, read this section from the official Terraform documentation.

Read the section titled 'Manage any infrastructure'. Focus on how providers act as the bridge between your Terraform code and the service's API.

3. State Management

If you declare you want five servers, and you already have three, how does Terraform know to only create two more? It knows this by maintaining a state file.

The state file is a JSON file (by default, terraform.tfstate) that acts as a source of truth. It stores a record of all the infrastructure that Terraform has created and currently manages. Before making any changes, Terraform reads this state file to understand what already exists. It then compares the current state to the desired state in your configuration files and creates a plan to bridge the gap.

This is one of Terraform's most critical responsibilities, as it enables the tool to manage the entire lifecycle of your infrastructure.

What is Infrastructure as Code with Terraform?

The official documentation explains the role of the state file clearly.

Read the short section titled 'Track your infrastructure' to understand the purpose of the state file.

4. The Standard Workflow: Plan and Apply

Terraform's workflow is safe and predictable. The two most important commands are plan and apply.

  • terraform plan: Terraform analyzes your configuration, checks the state file, and shows you an execution plan. It details exactly what it will create, update, or destroy. This is a "dry run" that lets you review changes before they happen.
  • terraform apply: Terraform executes the plan and makes the changes to your infrastructure.

This two-step process provides a critical safety net, preventing accidental changes.

Test your understanding!

You have a Terraform configuration that defines a server and a database. You run terraform apply and both are created.

Later, you edit the configuration file to change the server's size from "small" to "medium" and you remove the database definition entirely.

What do you expect terraform plan to show you?

Show answer

The terraform plan output will show that it intends to perform two actions:

  1. Modify: The server resource will be updated to change its size.
  2. Destroy: The database resource will be destroyed because it's no longer present in your configuration but exists in the state file.

Terraform's Place in the Automation Stack

In this course, we'll be using both Terraform and Ansible. It's crucial to understand their distinct roles.

  • Terraform is for Provisioning: It creates the foundational infrastructure—the servers, networks, load balancers, and databases.
  • Ansible is for Configuration Management: It configures what's on those servers—installing Docker, setting up users, copying files, and managing services.

Think of it this way: Terraform builds the house, and Ansible furnishes it. They are often used together in a deployment pipeline.

This final reading provides an excellent, concise comparison.

Terraform Infrastructure as Code (IaC) Guide

This FAQ section from a Firefly guide clearly summarizes the distinction between Terraform and other tools like Ansible.

Read the last section of the article, starting from the 'FAQs' heading. Focus on the answers to 'What is the difference between Terraform and other IaC?' and 'What is the difference between Ansible and Terraform for IaC?'.


Conclusion

Today, we've explored the powerful concept of Infrastructure as Code and defined the core responsibilities of Terraform as a leading IaC tool. By managing infrastructure through code, we bring automation, consistency, and version control to what was once a manual and error-prone process.

Key Takeaways:

  • IaC solves the problems of manual infrastructure management by treating infrastructure as code.
  • Terraform is a declarative, multi-cloud IaC tool.
  • Its main responsibility is to provision and manage the lifecycle of infrastructure resources.
  • It achieves this through key features:
    • Providers that connect to different cloud and service APIs.
    • A state file that tracks managed resources.
    • A safe plan-and-apply workflow.
  • Terraform is primarily for provisioning infrastructure, while tools like Ansible are for configuring it.

You now have a solid conceptual understanding of three pillars of modern deployment: Git for version control, Docker for application packaging, and Terraform for infrastructure provisioning. There's one foundational piece left to explore.

In our next lesson, we will dive deeper into the tool that "furnishes the house" by explaining how configuration management tools like Ansible fit into an automated deployment pipeline.