In one minute
- How companies build and run hundreds or thousands of identical Linux servers, using code instead of hands.
- Setting up one server by hand is a skill. Running a thousand identical ones is engineering. Hand-built servers slowly become different from each other ("drift"), and nobody can rebuild them.
- You build a ready-made golden image (with Packer) that already contains the OS, updates, security settings and agents. Each new server starts from that image and finishes its setup on first boot (with cloud-init). To change or update anything, you build a new image and replace the old servers.
- 👉 Any cloud setup with more than a few servers, Kubernetes node groups, and monthly patching.
Think of it like: a bakery. Instead of baking each cake from scratch by hand (and each one coming out slightly different), you use one tested recipe and a mould (the golden image). Every cake comes out the same. If the recipe changes, you bake new cakes. You do not try to fix old ones.
Pets versus cattle
Two ways to think about servers.
| Pets | Cattle | |
|---|---|---|
| Built | By hand, over time | Automatically, from code |
| When broken | You log in and nurse it back | You replace it with a fresh one |
| Each one unique? | Yes, each one is different | No, they are all the same |
| Risk | Drift; "snowflake" servers nobody can rebuild | Low: any server can be rebuilt |
Modern infrastructure treats servers as cattle. If a server misbehaves, delete it, and let the Auto Scaling group or Kubernetes start a clean one.
Remember it: Pets have names and get nursed. Cattle have numbers and get replaced.
The layers of server setup
A server is set up in four layers, from "changes rarely" to "changes every release".
Base image (golden AMI) → First boot (cloud-init / user data) → Ongoing config (Ansible) → App deploy (CI/CD) built weekly runs once per instance optional every release| Layer | What it puts in place | How often it changes |
|---|---|---|
| Golden image | OS, updates, hardening, agents, runtimes | Weekly or monthly |
| cloud-init | Hostname, environment, fetching secrets, joining a cluster | Every time a server starts |
| Config management | Settings that change between image builds | When needed |
| App deployment | The app version | Every release |
The more you bake into the image, the faster and more reliably servers start.
Golden images with Packer
Packer builds a server image automatically.
It starts a temporary server, runs your setup steps on it, saves the result as an image (an AMI on AWS, or an Azure, GCP or Docker image), then deletes the temporary server.
packer { required_plugins { amazon = { source = "github.com/hashicorp/amazon", version = ">= 1.3.0" } }} source "amazon-ebs" "base" { region = "ap-south-1" instance_type = "t3.small" ssh_username = "ubuntu" ami_name = "acme-ubuntu-2404-{{timestamp}}" source_ami_filter { filters = { name = "ubuntu/images/hvm-ssd-gp3/ubuntu-noble-24.04-amd64-server-*" } owners = ["099720109477"] most_recent = true }} build { sources = ["source.amazon-ebs.base"] provisioner "shell" { inline = [ "sudo apt-get update", "sudo apt-get -y upgrade", "sudo apt-get install -y amazon-cloudwatch-agent fail2ban auditd" ] } provisioner "ansible" { playbook_file = "hardening.yml" }}Reading it in plain words:
- source: start from the newest official Ubuntu 24.04 image, on a small temporary server in Mumbai (
ap-south-1). - build: install updates and agents with shell commands, then apply the hardening playbook with Ansible.
- The result is saved as an image named
acme-ubuntu-2404-<time>.
packer init .packer validate .packer build .What goes IN a golden image: OS updates, CIS hardening, monitoring and log agents, security agents, the container runtime (for Kubernetes nodes), standard users and SSH settings, time sync and kernel tuning.
What stays OUT: secrets, settings that differ per environment, and usually the app itself.
First boot with cloud-init
cloud-init runs once, the first time a new server starts. Almost every cloud image has it.
The image is the same for every server. cloud-init adds the parts that are different for this server.
You pass instructions as user data when you launch the server:
#cloud-confighostname: orders-apitimezone: Asia/Kolkatapackages: - jqwrite_files: - path: /etc/orders/orders.env permissions: "0640" content: | ENVIRONMENT=prod REGION=ap-south-1runcmd: - systemctl enable --now ordersIn plain words: set the hostname and timezone, install jq, write a settings file, then start the app.
Debugging cloud-init:
cloud-init status --longsudo cat /var/log/cloud-init-output.logsudo cloud-init schema --system # validate the user dataSecrets: fetch them at boot from a secrets manager (AWS Secrets Manager, SSM Parameter Store or Vault), using the server's IAM role. Never put secrets in user data. Anyone who can view the server's details can read it.
Configuration management
Tools that describe how a server should look and make it so.
👉 In the "cattle" world, Ansible is mostly used inside Packer to build images. It is also used for long-lived machines such as databases or older systems.
| Approach | Tools | Notes |
|---|---|---|
| Push | Ansible | No agent needed, uses SSH, simple |
| Pull | Puppet, Chef, Salt | An agent on each server checks in for changes |
| Immutable | Packer + Terraform | Rebuild instead of changing |
Immutable rollouts
"Immutable" means you never change a running server. To change anything, you build a new image and replace the servers.
Commit → Pipeline builds AMI → Scan and test → Update launch template (Terraform) → Instance refresh / rolling update → Old instances terminated- AWS Auto Scaling instance refresh replaces servers in small batches, keeping enough healthy ones running at all times.
- For Kubernetes nodes, EKS managed node groups or Karpenter move nodes to the new image, moving pods off each node safely first.
- Rollback is easy: just point back to the previous image.
Think of it like: replacing train carriages one at a time while the train keeps running, instead of repairing each carriage while passengers sit in it.
The patching pipeline
An automatic, repeating process that keeps every server up to date.
- A scheduled pipeline runs Packer, starting from the vendor's newest base image.
- The new image is scanned (Inspector, Trivy, OpenSCAP) and quickly tested.
- It is promoted through dev, staging and production, with automatic tests at each stage.
- Compliance checks flag images older than N days, and old images are removed.
This replaces the risky habit of logging in and running apt upgrade on production servers.
Running a fleet
| Need | Tools |
|---|---|
| Run a command on many servers | AWS SSM Run Command, Ansible ad-hoc |
| Know what is installed and patched | SSM Inventory and Patch Manager |
| Spot servers that drifted | Terraform plan, AWS Config, Ansible check mode |
| Safe access without SSH | SSM Session Manager |
| Metrics and logs for every server | CloudWatch, Prometheus, Grafana, Loki |
Containers: images at scale too
Container images follow the same ideas:
- Keep a small set of approved base images.
- Rebuild automatically when the base gets security fixes.
- Scan them in CI, and sign them (with Cosign) before deployment.
A security hole in the base image is fixed once, then rolled out to every service just by rebuilding.
Real-world scenario: patching 600 servers for a critical security bug
A critical OpenSSH vulnerability is announced.
- The old way: SSH loops and
apt upgradeon 600 servers, mixed results, and a lost weekend. - The golden image way: start the image pipeline. In 20 minutes it builds a patched image, scans and tests it, and promotes it. Instance refresh replaces every Auto Scaling group within hours, with health checks guarding each batch. Compliance reports show 100% on the new image.
Common mistakes
- Baking secrets into images.
- Long-lived "snowflake" servers patched by hand, so no two match.
- Huge cloud-init scripts doing work the image should already contain. This makes boots slow and fragile.
- No clean-up of old images, leaving hundreds of old, unpatched images that someone could still launch.
Practice
- Decide which layer each belongs in: CloudWatch agent, database password, app version, CIS settings, hostname.
- Write a cloud-init snippet that sets the timezone and installs
jq. - Describe the steps of a rolling image update, including how to roll back.
- Explain why secrets must not go in user data.
Cheat sheet
| Command | What it does |
|---|---|
packer init / validate / build . | Build a golden image |
cloud-init status --long | First-boot status |
/var/log/cloud-init-output.log | First-boot output |
cloud-init schema --system | Check the user data is valid |
aws autoscaling start-instance-refresh | Replace an ASG's servers with the new launch template |
Key takeaways
- At scale, servers are "cattle": built from code, and replaced instead of repaired.
- Packer builds a golden image with everything that rarely changes.
- cloud-init does the small, per-server setup on first boot.
- Fetch secrets when the server starts. Never bake them into the image or user data.
- To update servers, build a new image and replace the old servers in batches.