Introduction to Infrastructure as Code
Understand what Infrastructure as Code is, its main benefits such as repeatability and version control, and the distinction between declarative and imperative IaC tools.
Summary
Read Summary
Flashcards
Save Flashcards
Quiz
Take Quiz
Quick Practice
How does Infrastructure as Code (IaC) treat the management of hardware, network, and software resources?
1 of 7
Summary
Infrastructure as Code: A Modern Approach to Infrastructure Management
What Is Infrastructure as Code?
Infrastructure as Code (IaC) is a method for managing all the computing resources that make up your environment—servers, networks, databases, storage, and everything else—by describing them in text files. Rather than manually clicking through cloud dashboards or running ad-hoc commands, you write descriptions of your desired infrastructure and let automated tools handle the actual provisioning and configuration.
Think of it this way: if application source code is the blueprint for what your software does, Infrastructure as Code is the blueprint for what your computing environment looks like. Just like application code, Infrastructure as Code can be written, edited, reviewed, tested, and stored in version control systems. This fundamental shift in how we manage infrastructure has become standard practice in modern cloud environments.
How Infrastructure as Code Works
The basic workflow is straightforward:
Write a description: An engineer describes the desired state of the system using a specialized language. Common languages include YAML (YAML Ain't Markup Language), JSON (JavaScript Object Notation), or HashiCorp Configuration Language (HCL). These languages let you specify what resources you need—how many servers, what size, which networks, what software should be installed—without worrying about the exact steps to get there.
The tool reads and executes: A specialized tool reads these text files and automatically creates or modifies real cloud resources to match your description. Tools like Terraform, CloudFormation, and Ansible handle the heavy lifting, communicating with cloud providers or your own data centers to provision infrastructure.
It works everywhere: Infrastructure as Code can provision resources across public clouds (AWS, Microsoft Azure, Google Cloud Platform), traditional on-premises data centers, or hybrid environments that combine both. This flexibility is one reason IaC has become so popular.
Desired-State Versus Manual Commands
A crucial distinction in Infrastructure as Code is the difference between declaring your end goal versus manually issuing commands to get there.
With manual commands, you might ssh into a server and run a series of shell scripts: create this network, launch that server, install this software, configure that setting. If something goes wrong midway through, you have to remember what step you were on and what still needs to be done. If you need to do it again next week, you have to remember or rediscover the exact sequence of commands.
With Infrastructure as Code, you describe the final state you want—"I need 3 web servers with these specifications running this software"—and the tool figures out what needs to happen. You don't specify the steps. The tool determines whether it needs to create new resources or modify existing ones to match your description. This is called declarative programming, and it's fundamentally different from imperative programming where you spell out each step.
Why Infrastructure as Code Matters: Key Advantages
Repeatability and Consistency
The same code executed multiple times produces an identical environment every time. This eliminates infrastructure drift—a common problem where manual changes, forgotten updates, or inconsistencies cause different environments to diverge. If you needed to rebuild your entire production infrastructure tomorrow, your Infrastructure as Code would recreate it exactly as it was.
Version Control and Auditability
Because Infrastructure as Code files are plain text, they can be stored in Git or other version-control systems. This provides a complete audit trail: who changed what infrastructure, when, and ideally why (if they wrote good commit messages). You can see the entire history of your infrastructure the same way you see the history of your application code. Need to understand why a configuration changed six months ago? Check the commit history. Need to roll back a change? Use Git's rollback capabilities.
Collaboration and Review
Teams can use the same collaborative practices they use for application code. Pull requests let colleagues review proposed infrastructure changes before they're applied. Automated tests can catch errors. Code reviews catch configuration mistakes. This shift from "infrastructure is magic that only DevOps knows" to "infrastructure is code that teams can collectively understand" is transformative.
Speed and Consistency Across Environments
Deployments that used to take days—complex orchestration of many manual steps—can now be completed in minutes. More importantly, development, testing, staging, and production environments can stay synchronized. Because they're all described in the same code, they remain consistent. A developer can deploy the exact same infrastructure code locally as runs in production, reducing the "it works on my machine" problem.
Two Approaches: Declarative and Imperative Tools
Infrastructure as Code tools fall into two broad categories, and understanding the difference is important.
Declarative Tools
Declarative tools work like this: you describe the desired final state of your infrastructure, and the tool figures out what needs to happen. You write something like "I want a load balancer with these three servers behind it," and the tool handles the implementation details. Tools like Terraform and CloudFormation use declarative approaches.
Advantage: You focus on "what" not "how." The tool handles complexity and can optimize changes.
Imperative (Procedural) Tools
Imperative tools work like this: you write an ordered sequence of commands that explain how to build your infrastructure, step by step. You write something like "First create this network, then create these servers, then configure the load balancer." Tools like Ansible and traditional shell scripts use imperative approaches.
Advantage: You have explicit control over each step and the order they happen in.
The Common Goal: Idempotence
Whether declarative or imperative, modern Infrastructure as Code tools share an important property called idempotence. This means that if you run the same Infrastructure as Code multiple times, it produces the same result after the first run. Subsequent runs detect that the desired state already exists and make no changes.
For example, if your code creates a server, the first run creates it. The second run detects that the server already exists and leaves it alone (assuming nothing has changed). This is crucial because it means you can safely re-run Infrastructure as Code without accidentally creating duplicate resources or causing unintended changes.
Core Concept: Desired-State Reconciliation
At the heart of Infrastructure as Code is a concept called desired-state reconciliation. Here's how it works:
The tool maintains knowledge of two states: the current state (what infrastructure actually exists right now) and the desired state (what Infrastructure as Code says should exist). When you apply Infrastructure as Code, the tool compares these two states and calculates the difference. It then makes only the minimal necessary changes to transform current state into desired state.
This is powerful because:
It's safe: The tool won't make unnecessary changes
It's efficient: You don't re-do work that's already done
It's predictable: You always know exactly what will change and what won't
For example, if your Infrastructure as Code specifies 3 web servers but only 2 currently exist, the tool creates 1 new server. If 4 servers exist, the tool terminates 1. If 3 servers exist exactly as specified, the tool changes nothing.
Choosing the Right Tool
No single Infrastructure as Code tool is right for every situation. When selecting a tool, consider:
Cloud provider support: Does the tool support your cloud platforms (AWS, Azure, GCP, on-premises, or others)?
Language preference: Do you prefer to write configurations in YAML, JSON, HCL, or another language?
Declarative or imperative: Does your team work better with declaring desired state or writing ordered procedures?
Learning curve: How quickly can your team become productive with the tool?
There's no "wrong" answer—successful organizations use various combinations of tools depending on their specific needs.
Flashcards
How does Infrastructure as Code (IaC) treat the management of hardware, network, and software resources?
As a set of text files that can be written, edited, reviewed, and stored like application source code.
In Infrastructure as Code, what determines the specific steps needed to reach the final environment state?
The IaC tool (based on the desired-state description).
How does Infrastructure as Code eliminate "drift" caused by manual changes?
By executing the same code multiple times to always produce an identical environment (repeatability).
In the context of Infrastructure as Code tools, what is the definition of a "declarative" tool?
A tool where you describe the final state of the infrastructure and the tool determines the necessary steps.
In the context of Infrastructure as Code tools, what is the definition of an "imperative" (procedural) tool?
A tool where you specify an ordered series of tasks to configure each component.
What is the meaning of "idempotence" in Infrastructure as Code?
Applying the same code multiple times leaves the system unchanged after the first successful run.
What is the process of "desired-state reconciliation" performed by an Infrastructure as Code tool?
The tool compares the current state with the desired state and makes only the necessary changes.
Quiz
Introduction to Infrastructure as Code Quiz Question 1: What does the term “Infrastructure as Code” primarily describe?
- A modern approach to managing hardware, network, and software resources as code (correct)
- A method for writing application source code in multiple languages
- A technique for compressing data files for transmission
- An approach to manually configuring servers via ad‑hoc scripts
Introduction to Infrastructure as Code Quiz Question 2: Which benefit does storing Infrastructure as Code definitions in a version‑control system provide?
- A complete history of who changed what, when, and why (correct)
- Automatic scaling of cloud resources without user input
- Real‑time monitoring of network traffic
- Encryption of all configuration files by default
Introduction to Infrastructure as Code Quiz Question 3: What advantage does repeatability provide in Infrastructure as Code?
- The same code can be run repeatedly to produce identical environments (correct)
- It eliminates the need for any testing before deployment
- It allows resources to change automatically over time
- It requires each developer to write unique scripts for every run
What does the term “Infrastructure as Code” primarily describe?
1 of 3
Key Concepts
Infrastructure Management Practices
Infrastructure as Code
Desired‑State Configuration
Declarative Infrastructure Tools
Imperative Infrastructure Tools
Idempotence
Version Control and Tools
Version Control
HashiCorp Configuration Language
Cloud Computing
Public Cloud Computing
Definitions
Infrastructure as Code
A practice of managing and provisioning computing infrastructure through machine‑readable definition files rather than manual processes.
Desired‑State Configuration
An approach where the target state of resources is declared, and a system automatically reconciles the actual state to match it.
Declarative Infrastructure Tools
Tools that let users specify *what* the final infrastructure should look like, leaving the tool to determine *how* to achieve it.
Imperative Infrastructure Tools
Tools that require users to script the exact sequence of steps needed to configure each component of the infrastructure.
Idempotence
The property that applying the same infrastructure definition multiple times results in no further changes after the first successful application.
Version Control
A system for tracking changes to infrastructure code, enabling history, collaboration, and rollback of configurations.
Public Cloud Computing
Delivery of computing resources such as servers, storage, and networking over the internet by providers like AWS, Azure, and Google Cloud Platform.
HashiCorp Configuration Language
A domain‑specific language used by tools like Terraform to describe infrastructure resources in a concise, declarative syntax.