Skip to content

Fundamentals of Infrastructure as Code (IaC)

This is the first chapter of the book. In this chapter, you will learn the fundamental concepts related to Infrastructure as Code (IaC), including what it is, its benefits, and why it matters. You’ll also explore the differences between declarative and imperative approaches, followed by an introduction to Terraform—its advantages, key concepts, installation steps, and best practices.

What is Infrastructure as Code (IaC)?

Infrastructure as Code, often abbreviated as IaC, is a modern approach to managing cloud infrastructure by writing code to define and provision resources, rather than setting them up manually through a portal or command-line interface.

Just like application developers write code to build and maintain software, infrastructure teams use code to describe servers, networks, storage, and other cloud services. This code can be stored in version control systems, shared across teams, reviewed, and reused—making the entire process more predictable and efficient.

By treating infrastructure as code, teams can automate the deployment of environments, reduce manual steps, and avoid configuration drift. The result is a consistent, repeatable, and scalable method of provisioning infrastructure that improves speed, reliability, and collaboration across development and operations teams.

Architecture Diagram

The following diagram shows the high level architecture of IaC process.

Alt text

Benefits of Infrastructure as Code (IaC)

Adopting Infrastructure as Code offers a number of practical advantages that streamline and strengthen modern infrastructure management.

Faster Deployment: With IaC, infrastructure setup becomes quicker and more predictable. Instead of manually configuring environments each time, teams can automate provisioning, saving valuable time and reducing delays during development or scaling phases.

Consistency and Reliability: Manual processes often introduce inconsistencies or errors. IaC eliminates this risk by using repeatable code to define infrastructure, ensuring that every environment—development, staging, or production—is built the same way every time.

Scalability Made Easy: As application demand grows or shrinks, IaC makes it simple to adjust infrastructure accordingly. Whether scaling up for peak traffic or scaling down to reduce costs, changes can be made through configuration updates rather than hands-on interventions.

Improved Collaboration: When infrastructure is treated as code and stored in version control systems like Git, teams can work together more effectively. Changes are tracked, peer-reviewed, and documented, just like application code, making it easier to collaborate and audit.

Greater Agility and Flexibility: IaC supports agile development practices by enabling rapid iteration and experimentation. Developers can spin up test environments on demand and quickly implement changes in response to evolving requirements or feedback.

Several tools are available to help implement IaC, such as Terraform, AWS CloudFormation, and Ansible. These tools make it possible to define, manage, and provision infrastructure in a consistent, automated, and repeatable manner, forming a crucial part of today’s cloud-native workflows.

Why Infrastructure as Code Matters

Traditional infrastructure setup involved manually configuring servers, databases, networks, and more. This approach was slow, error-prone, and inconsistent across environments.

With IaC, you can:

  • Provision resources quickly and consistently
  • Track changes using version control systems like Git
  • Eliminate configuration drift between environments
  • Enable collaboration between teams using shared code
  • Integrate infrastructure into CI/CD pipelines

Declarative vs. Imperative Approaches

When managing infrastructure as code, it's important to understand the difference between declarative and imperative approaches—two distinct ways to describe how infrastructure should be created and managed.

Declarative Approach

The declarative style focuses on what the desired end state should look like. You describe the final outcome, and the tool figures out the steps needed to reach that state. Terraform uses this approach.

For example, if you define a virtual machine, a storage account, or a Kubernetes cluster in Terraform, you're simply stating that these resources should exist with specific properties. Terraform compares the current infrastructure to the desired state and makes the necessary changes automatically.

Benefits of Declarative Style:

  • Easier to read and maintain
  • Reduces risk of human error
  • Automatically handles dependencies
  • Encourages idempotency (applying the same configuration repeatedly leads to the same result)

Imperative Approach

The imperative style is more focused on how to reach a goal. It involves executing a series of commands or instructions step-by-step to build the desired infrastructure.

Tools like Ansible (though often used declaratively), shell scripts, or cloud CLI commands often fall into this category. You must explicitly define each action—like creating a network, provisioning a VM, or configuring a firewall—one command at a time.

Drawbacks of Imperative Style:

  • Requires more effort to maintain
  • Harder to scale and automate
  • Higher chance of inconsistency between environments

Why Declarative Wins for IaC

In the context of infrastructure provisioning, the declarative model offers a cleaner, more reliable, and automated way to manage environments. It aligns better with modern DevOps practices, where speed, repeatability, and consistency are critical.

Terraform's use of declarative configuration makes it easier to model complex environments, track changes over time, and collaborate across teams—while reducing the need for manual intervention.

Introducing Terraform

Terraform, developed by HashiCorp, is one of the leading tools in the Infrastructure as Code (IaC) ecosystem. It provides a powerful, consistent way to define and manage infrastructure using simple, human-readable configuration files written in the HashiCorp Configuration Language (HCL).

With Terraform, you can describe your entire infrastructure — from virtual machines and storage to networking and security — in code. This configuration can then be versioned, reviewed, and applied across environments in a repeatable and automated fashion.

One of Terraform’s standout features is its ability to work across multiple cloud providers. Whether you're deploying resources on Azure, AWS, Google Cloud, or even hybrid and on-premises environments, Terraform offers a unified approach through a single configuration language and tooling.

Terraform also introduces concepts like execution plans, state management, and modular design. These help you understand what changes will be made before they’re applied, track the current state of your infrastructure, and organize your code into reusable components. Over time, Terraform can serve as the single source of truth for your infrastructure landscape.

Key Advantages of Using Terraform

  • Multi-Cloud Flexibility: Terraform supports a wide range of cloud providers and services, making it ideal for organizations that operate in hybrid or multi-cloud environments.

  • Declarative Configuration: With Terraform, you describe what you want, not how to do it. This declarative style simplifies the code and makes it easier to reason about the desired infrastructure state.

  • Best Practices Built-In: Terraform encourages infrastructure management practices such as version control, peer review, and automated testing—bringing infrastructure closer to modern software development workflows.

  • Reusable Modules: By structuring code into modules, Terraform promotes reuse and standardization. Teams can build, share, and maintain common infrastructure components across projects.

  • Strong Community and Ecosystem: Terraform benefits from an active, open-source community. You'll find a wealth of official and community-contributed modules, integrations, and guidance to help you adopt and scale with confidence.

Key Concepts in Terraform

Before diving into code, let’s understand the core building blocks:

Concept Description
Providers Interfaces to manage resources in a specific platform (e.g., Azure, AWS).
Resources Individual infrastructure components (e.g., azurerm_kubernetes_cluster, azurerm_postgresql_server).
Modules Reusable containers for multiple resources grouped together.
Variables Input parameters to customize deployments.
State Terraform keeps track of real-world infrastructure using a .tfstate file.
Plan Shows the changes Terraform will make before applying.
Apply Executes the changes to match the desired state.

Installing and Configuring Terraform

Let’s set up Terraform on your local machine.

Step 1: Install Terraform

Follow the official Terraform installation guide or use the CLI below:

# macOS (Homebrew)
brew tap hashicorp/tap
brew install hashicorp/tap/terraform

# Windows (Chocolatey)
choco install terraform

Step 2: Verify the Installation

terraform version

You should see the installed version printed on your terminal.

Writing Your First Terraform Script

Let’s create a minimal Terraform file to deploy a simple resource (we’ll expand this in labs):

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

Save this as main.tf.

Then run:

terraform init      # Initializes the project
terraform plan      # Shows what will be created
terraform apply     # Applies the configuration

Best Practices for IaC Projects

  • Use remote state storage for collaboration (e.g., Azure Storage Account)
  • Structure your project using modules and environments
  • Keep secrets out of code—use Azure Key Vault
  • Store configurations in Git for version control
  • Integrate Terraform into CI/CD pipelines
  • Use tags for cost tracking and resource governance

Summary

In this chapter, we introduced the fundamentals of Infrastructure as Code and Terraform. You learned about:

  • The value of IaC in building scalable infrastructure
  • The difference between declarative and imperative approaches
  • Terraform’s key components and workflow
  • How to install and run your first Terraform configuration

In the next chapter, we’ll dive into the Azure-specific setup and start building the foundation for our microservices infrastructure using Terraform.