Infrastructure as code with Terraform and Ansible: a practical introduction for small companies
Ask yourself one question: if your main server disappeared tonight, how long would it take to build an identical one? If the answer starts with “well, first we would need to remember how it was set up”, infrastructure as code is for you.
The idea
Infrastructure as code (IaC) means describing servers, networks and their configuration in text files instead of clicking through web consoles. The files live in Git, changes are reviewed, and the same files can rebuild the environment at any time. Your infrastructure gets a history, documentation and an undo button.
Terraform: creating the infrastructure
Terraform describes what should exist in your cloud or virtualisation platform: virtual machines, networks, firewall rules, DNS records, storage buckets. You write the desired state, Terraform compares it with reality and shows a plan of changes before applying them. It works with AWS, Azure, Google Cloud, Hetzner, DigitalOcean, Cloudflare and hundreds of other providers.
resource "aws_lightsail_instance" "web" {
name = "web-1"
availability_zone = "eu-central-1a"
blueprint_id = "ubuntu_24_04"
bundle_id = "small_3_0"
}
OpenTofu is an open-source fork of Terraform with the same language, a good option if licensing matters to you.
Ansible: configuring what runs on it
Ansible describes how a server should be configured: packages, users, SSH settings, firewall, Docker, application files. It connects over SSH, needs no agent and applies the same playbook to one server or a hundred. Running it again changes only what drifted from the desired state.
- hosts: web
become: true
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
update_cache: true
How they work together
- Terraform creates the server, network and DNS.
- Ansible installs and configures the software.
- Your CI/CD pipeline deploys the application.
All three are code in a repository. A new environment for testing, or a replacement after a disaster, becomes a matter of running a few commands.
A realistic first project
Do not try to convert everything at once. A good first step for a small company:
- Describe DNS records and one production server in Terraform.
- Write an Ansible playbook for that server’s base setup: updates, users, SSH hardening, firewall, monitoring agent, Docker.
- Store secrets outside the repository, for example in a password manager or a secrets service.
- Rebuild the server from scratch in a test account to prove it works.
This usually fits into 20 to 40 engineer hours and immediately reduces the risk of “nobody knows how this server was built”.
When it is not worth it
If you run only SaaS tools and no servers, IaC brings little. If you have one server that rarely changes, a well-written document and good backups may be enough for now. IaC pays off as soon as you have several environments, frequent changes or a business that cannot wait days for a rebuild.
Infrastructure as code is part of our DevOps Growth plan. Combined with CI/CD, it turns your infrastructure into something you can rebuild in minutes.