What is IaC (Infrastructure as Code)

While reading, watching or working with and about DevOps, Cloud or other areas, you have probably stumbled over Infrastructure as Code or it’s abbreviation IaC.

Alexander Pfaller
2 min readMar 13, 2023

General overview

Infrastructure as Code means, as the name suggests, that you simply manage and define your infrastructure in configuration files. Those configuration files can be written in different formats, such as yaml, hcl, json…

A quick yaml example for the Ansible IaC tool to install the Nginx webserver on a debian based linux distribution:

--- 
- name: "install nginx"
hosts: linux
tasks:
- name: install the nginx webserver
ansible.builtin.apt:
name: nginx
state: latest
  • The infrastructure defining code can be stored in a cloud based version control system, such as: Github, GitLab, BitBucket,…
  • There can be either declarative IaC or imperative; both have advantages, disadvantages, and differences you should keep in mind

Declarative

You specify what you want to provision/ the desired state, and the properties of those resources

Pros:
- Requires less coding skills
- Highly repeatable
Cons:
- Less control over the process
- Can overcomplicate simple tasks

terraform {
version = "1.4.0"
}

provider "aws" {
region = "us-east-1"
}

resource "aws_s3_bucket" "my_bucket" {
bucket = "my-sample-s3-bucket"
acl = "public-read"

website {
index_document = "index.html"
}
}

Imperative

You define the steps in a configuration file which the your IaC tool of choice later uses to provision the desired resources/ infrastructure

Pros:
- Control over every step
- Good for simple or adhoc tasks
Cons:
- Less idempotent (than declarative)
- Makes it more error prone (than declarative)

aws s3api create-bucket --bucket my-sample-s3-bucket --region us-east-1

Idempotence

If the current state already matches the desired state, nothing will be changed. If you execute the same command, the results should be the same. That’s why you can run something declarative repeatedly, whereas an imperative IaC may not be able to run it as often.

IaC tools

  • Ansible
  • Terraform
  • Chef
  • Puppet
  • AWS CloudFormation

Benefits of IaC

  • Repeatable process
  • Consistent environments (Infrastructure definition of dev environment can be the same as prod)
  • Reusable components
  • Documented architecture (Most data formats are easy to read, which makes getting familiar with new infrastructure really easy and fast)
  • Automated deployments
  • Reduces errors

--

--