Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What Is Infrastructure as Code and Why It Matters
Programming

What Is Infrastructure as Code and Why It Matters

A practical introduction to Infrastructure as Code using Terraform and Ansible

Eugene Obiedkov

by Eugene Obiedkov

Full Stack Developer

Feb, 2026・
8 min read

facebooklinkedintwitter
copy
What Is Infrastructure as Code and Why It Matters

Infrastructure as Code (IaC) is an approach where infrastructure is defined using code and managed automatically. Servers, networks, load balancers, and other resources stop being the result of manual actions and instead become a reproducible state stored in a repository.

Put simply, IaC answers one key question:

How can we guarantee that our infrastructure always looks exactly the way we expect it to?

Without IaC, infrastructure lives "in reality": someone creates a server, someone tweaks settings in a cloud UI, someone forgets to document a change. With IaC, infrastructure lives "in code", and the real environment is continuously aligned with that code.

What IaC Looks Like in Practice

In most real-world setups, infrastructure is split into two distinct layers.

The first layer is resource provisioning.
This includes virtual machines, networks, subnets, firewalls, and managed cloud services. At this level, the goal is to define what should exist.

The second layer is server configuration.
This covers installing packages, configuring services, managing users, and setting environment variables. Here, the goal is to define how an existing server should be configured.

Different tools are used for these layers, and understanding this separation is essential to understanding IaC.

Terraform: Managing the Shape of Infrastructure

It creates and modifies infrastructure resources in the cloud or in a data center. Its core idea is declarative configuration: you describe the desired end state, and Terraform figures out what actions are required to reach it.

When you write Terraform code, you don't say "reate a server".
You say, "a server with these properties must exist".

If the server already exists, nothing happens.
If it differs from the definition, Terraform brings it back to the desired state.

Let’s create a minimal setup: a single virtual server in the cloud.

provider "aws" {
  region = "eu-central-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0abcdef12345"
  instance_type = "t3.micro"
}

This code doesn’t describe stepsβ€”it describes state. Terraform compares this desired state with what exists in Amazon Web Services.

Before making changes, you usually run terraform plan, which shows exactly what will change without applying anything. Then terraform apply creates the server.

A key takeaway: if you manually delete the server in the cloud and run Terraform again, it will be recreated. Infrastructure always converges back to the state defined in code.

Run Code from Your Browser - No Installation Required

Run Code from Your Browser - No Installation Required

Why Terraform Doesn’t Configure Servers

Terraform is excellent at creating servers, but it is not designed to install nginx, configure systemd, or edit application configs. This is a deliberate architectural choice.

Terraform manages infrastructure as resources, not as operating systems.

That’s why a second tool is almost always used after Terraform: Ansible.

Ansible: Managing Server Configuration

Ansible works on already existing servers and handles their configuration. It connects over SSH and enforces the desired system state: installing packages, deploying configuration files, and managing services.

If Terraform defines the "skeleton" of the infrastructure, Ansible provides the "muscles and organs".

A First Ansible Playbook

Here's a basic example that installs and starts nginx:

- hosts: web
  become: true

  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Ensure nginx is running
      service:
        name: nginx
        state: started

This playbook describes state, not a script. nginx must be installed and running.

If nginx is already installed, nothing changes. If the service is stopped, Ansible starts it.

Ansible doesn’t blindly execute commandsβ€”it checks the current state and fixes any drift.

Start Learning Coding today and boost your Career Potential

Start Learning Coding today and boost your Career Potential

How Terraform and Ansible Work Together

A typical IaC workflow looks like this:

  • Terraform provisions the infrastructure and outputs data such as IP addresses, DNS names, and network details;

  • These outputs are used to generate an Ansible inventory;

  • Ansible connects to the servers created by Terraform and configures them.

The result is a fully functional infrastructure built from scratch using a single automated workflow.

The key point is that Terraform and Ansible do not overlap in responsibility. Each tool does one job well, and that separation is what makes the system reliable.

Conclusion

The Terraform + Ansible combination is the most common and practical entry point into Infrastructure as Code. Once you’re comfortable with it, you can move on to CI/CD pipelines, Kubernetes, and more advanced architectures with a strong foundation already in place.

FAQ

Q: What is Infrastructure as Code (IaC)?
A: Infrastructure as Code is an approach where infrastructure is defined and managed using code. Instead of manual configuration, the desired state of servers, networks, and services is stored in a repository and automatically applied.

Q: Why is Infrastructure as Code important?
A: IaC ensures that infrastructure is consistent, reproducible, and predictable. It helps prevent configuration drift, simplifies recovery, and makes infrastructure changes easier to track and review.

Q: Why is infrastructure usually split into two layers?
A: Infrastructure is split into provisioning and configuration layers to separate responsibilities. One layer defines what resources exist, while the other defines how those resources are configured, making the system more reliable and easier to maintain.

Q: What role does Terraform play in IaC?
A: Terraform is used to provision infrastructure resources such as virtual machines, networks, and managed cloud services. It describes the desired state and automatically reconciles real infrastructure with that state.

Q: Why is Terraform declarative rather than imperative?
A: A declarative approach allows you to describe what the infrastructure should look like instead of how to build it step by step. This makes configurations simpler, safer, and easier to reason about.

Q: Why doesn’t Terraform configure software on servers?
A: Terraform is designed to manage infrastructure resources, not operating system state. Mixing server configuration into Terraform breaks separation of concerns and leads to fragile infrastructure.

Q: What is Ansible used for in this setup?
A: Ansible configures existing servers by installing packages, managing services, and applying configuration files, ensuring each server matches the desired system state.

Q: How do Terraform and Ansible work together?
A: Terraform creates the infrastructure and outputs connection details, while Ansible uses that information to connect to servers and apply configuration, resulting in fully provisioned and configured infrastructure.

Q: What happens if infrastructure is changed manually?
A: Manual changes are treated as drift. The next Terraform run will restore the infrastructure to match the code, and Ansible can be re-run to reapply the correct configuration.

Q: Who benefits most from Infrastructure as Code?
A: IaC benefits everyoneβ€”from solo developers to large teamsβ€”by making infrastructure easier to manage, scale, and reproduce across environments.

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Was this article helpful?

Share:

facebooklinkedintwitter
copy

Content of this article

We're sorry to hear that something went wrong. What happened?
some-alt