The Wayback Machine - https://web.archive.org/web/20261202112206/https://www.geeksforgeeks.org/devops/what-is-infrastructure-as-code-iac/

Infrastructure as Code (IaC)

Last Updated : 26 Nov, 2026

Infrastructure as Code (IaC) is a modern DevOps practice where servers, networks, and cloud resources are automatically provisioned and managed using code instead of manual configuration. It helps maintain consistency, speed, and reliability across deployments.

  • Automates infrastructure setup using scripts or configuration files.
  • Ensures consistent environments across development, testing, and production.
  • Enables faster and repeatable deployments.
  • Reduces human errors caused by manual configuration.
  • Improves scalability and disaster recovery.
  • Supports version control for infrastructure changes (like Git).

It treats infrastructure components like software, allowing you to use development practices like version control, testing, and CI/CD pipelines.

Core Concepts

1. Idempotency

This is a critical concept in IaC. An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application. In IaC terms, this means that running your IaC script over and over will always result in the same defined state. If the infrastructure already matches the desired state, the tool will simply do nothing. This makes applying updates safe and predictable.

2. Immutable Vs Mutable Infrastructure

  • Mutable Infrastructure: This is the traditional "pet" model. Servers are provisioned and then updated, patched, and modified in place over their lifetime. This process can lead to configuration drift and makes servers fragile and unique.
  • Immutable Infrastructure: This is the modern "cattle" model favored by IaC. When a change is needed, the existing server is not modified. Instead, a new server is provisioned from a fresh image with the desired changes, and the old one is terminated. This ensures a clean, predictable, and fully reproducible state.

Declarative Vs Imperative Approaches

IaC tools follow one of two main approaches to defining infrastructure:

FeatureDeclarative ApproachImperative Approach
PhilosophySpecifies the desired state (the "what") of the infrastructure.Details the exact steps or commands (the "how") to achieve the desired state.
ExecutionThe IaC tool determines and performs the actions needed to reach the desired state.Requires the user to execute commands in the correct sequence.
State ManagementThe tool tracks the current state of the infrastructure, simplifying updates and teardown.Does not inherently track state; the user is responsible for managing changes.
User FocusSimplifies the process; users define what they want.Demands detailed instructions; the user defines how to achieve the result.
Handling ChangesAutomatically calculates and applies the necessary changes to match the new desired state.The user must write a new script to figure out and apply the changes manually.
ExampleDefining a resource in a Terraform file: resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro"}Writing a shell script: #!/bin/bashaws ec2 run-instances \ --image-id ami-123 \ --instance-type t2.micro

IaC in the DevOps Lifecycle

IaC is a fundamental enabler of DevOps practices by bridging the gap between development and operations:

  • IaC integrates seamlessly into Continuous Integration/Continuous Delivery (CI/CD) pipelines. A code change can trigger a pipeline that not only tests the application but also provisions or updates the necessary infrastructure to run it.
  • Teams can instantly spin up new, production-like environments for development, testing, or user acceptance testing (UAT). This allows developers to test their code in a realistic setting before merging, catching bugs earlier.
  • When infrastructure is code, it lives in the same VCS as the application code. Developers and operations engineers can collaborate on the same files, review changes through pull requests, and gain a shared understanding of the entire system.

IaC tools are designed for different infrastructure management tasks and can be grouped into two main categories:

1. Infrastructure Provisioning Tools

These tools create, modify, and destroy infrastructure components such as virtual machines, networks, and databases.

  • Terraform: An open-source tool by HashiCorp that supports multiple cloud providers, including AWS, Azure, and GCP. It uses HCL to define infrastructure.
  • AWS CloudFormation: AWS's native IaC tool that uses JSON or YAML templates to provision and manage AWS resources.

2. Configuration Management Tools

These tools automate software installation, configuration, and service management on existing servers.

  • Ansible: An open-source, agentless tool that uses YAML playbooks and SSH to automate configuration tasks.
  • Puppet: An agent-based tool that uses a declarative approach to maintain the desired state of systems.
  • Chef: Uses Ruby-based recipes and cookbooks to automate server configuration and software management.
  • SaltStack: An event-driven automation tool known for its speed and scalability, supporting both agent-based and agentless management.

Use Cases

Infrastructure as Code has a wide range of applications across different domains:

  • Provisioning and managing all cloud resources, such as virtual machines, storage, databases, and VPCs.
  • Automating the creation of development, staging, and production environments as part of a CI/CD pipeline.
  • Defining and deploying an entire application stack, including web servers, application servers, load balancers, and databases.
  • Automating the deployment and management of networks, including creating and managing subnets, security groups, and firewalls.
  • Automating the deployment and management of big data infrastructure, such as setting up clusters for Apache Hadoop or Apache Spark.
  • Quickly and reliably recreating an entire production infrastructure in a different region in the event of a failure.
Comment