Infrastructure as Code

Infrastructure as Code (IaC) is a methodology that uses software development practices to manage and provision infrastructure. Instead of manually configuring servers, networks, and other IT resources, IaC leverages code to define and automate these processes.

The concept of IaC emerged in the early 2000s as a response to the growing complexity of IT environments. Early tools like Puppet and Chef pioneered the use of declarative languages to describe infrastructure configurations. Over time, IaC gained popularity with the rise of cloud computing and DevOps practices.

Tools

Ansible is a configuration management tool, created by Michael DeHaan and released as an open-source project in 2012. It quickly gained popularity due to its simplicity and agentless architecture. Ansible uses a YAML-based language, which is easy to read. An example to install Apache web server:

- name: Install Apache Web Server
  hosts: web_servers
  become: yes
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present
    - name: Start Apache service
      service:
        name: apache2
        state: started

Terraform was released in 2014 by HashiCorp, a company known for its open-source infrastructure tools, such as Packer and Consul. Terraform uses it's own language (HCL) to define infrastructure. Following code ensures that an aws EC2 instance is allocated:

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  key_name      = "my-key-pair"
  tags = {
    Name = "My Instance"
  }
}

Pulumi takes a different aproach, it uses programming languages for code, providing a more familiar and intuitive experience for developers. It supports multiple languages, following is creating a EC2 instance in JavaScript:

import * as pulumi from "@pulumi/pulumi"
import * as aws from "@pulumi/aws"

const instance = new aws.ec2.Instance("web-server", {
    ami: "ami-0c55b159cbfafe1f0",
    instanceType: "t2.micro",
    keyName: "my-key-pair",
    tags: {
        Name: "web-server"
    }
})

Unlike other IaC tools that are primarily for DevOps engineers, SST (Serverless Stack Toolkit) is tailored specifically for developers to build serverless applications. It is built on top of Pulumi, and uses TypeScript for code.

Why IaC

Setting up deployment environments is often tedious, repetitive, and time-consuming, and at the same time requiring significant expertise.

While automation can address certain aspects, IaC goes beyond simple automation. It provides version control and documentation for infrastructure, and IaC tools use high-level languages to abstract away much of the complexity involved in infrastructure management.