Immutable infrastructure is a way of running servers where you never change a server once it is live. When something needs to change, you build a new server from an updated image and swap it in for the old one. This article is for teams who maintain servers by logging in and patching them, and who are tired of machines that nobody can rebuild exactly. Over months, small manual fixes add up to drift, the gap between what a server should be and what it really is, and drift is what makes rebuilds and incidents hard. The fix is a build pipeline that produces a tested image, plus a rollout that replaces old servers with new ones. Below you’ll find how that pipeline works, how the rollout goes, and the cases where patching in place is still the right call.
How to verify
For a fleet that claims to run immutably, three signals tell you whether it actually does:
# What image (or AMI) is this instance running?
curl -fsS http://169.254.169.254/latest/meta-data/ami-id # AWS
# When was that image built? (label or tag in your image metadata)
# When does the autoscaling group last cycle instances?
aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[*].[AutoScalingGroupName,InstanceCount]'
# Has anyone SSHed in recently?
sudo last | head -10
ls -la /etc/nginx/conf.d/ # for files newer than the AMI build date
last showing zero interactive logins for weeks, file mtimes matching the image build, and the ASG cycling instances on a regular cadence are the operator-visible properties of an immutable fleet.
What’s happening
The mental shift: a server is not a destination, it is an instantiation. The artifact is the image (an AMI, a container image, a VM template); the running instance is a transient projection of that artifact. Changes happen by rebuilding the image and rolling instances. There is no apt upgrade, no vi /etc/nginx/..., no pip install against a running host. If any of those need to happen, the change goes into the next image and the fleet rolls.
The deployment model: a CI pipeline takes the application code plus a base image plus a Packer (or kaniko, or Bazel) recipe and produces a versioned image. The deploy step is to update the autoscaling group’s launch template to reference the new image and trigger an instance refresh (or for containers, update the deployment spec). Old instances drain and terminate; new instances come up from the new image. Rollback is the same operation pointing at the previous image — no special path.
The benefits start with reproducibility (the image is the spec) and no drift at the OS layer (you cannot drift from your image if you replace whenever a change is needed). You also get faster recovery (a known-good image is one launch away), and a smaller incident-response surface (the questions “what changed?” and “when?” have unambiguous answers).
The procedure
-
Pick the image build tool. Packer for VM images (AMIs, GCE images, Azure managed images),
docker buildorkanikofor container images,nixfor pure functional builds. Whichever, version the recipe in Git next to the application. -
Write a thin Packer recipe. Start from an LTS base image, add the application and its dependencies, harden, and bake.
# packer/web.pkr.hcl packer { required_plugins { amazon = { source = "github.com/hashicorp/amazon", version = "~> 1.3" } } } source "amazon-ebs" "web" { ami_name = "acme-web-${formatdate("YYYYMMDD-hhmm", timestamp())}" instance_type = "t3.small" region = "us-east-1" source_ami_filter { filters = { name = "ubuntu/images/hvm-ssd/ubuntu-24.04-amd64-server-*" } owners = ["099720109477"] most_recent = true } ssh_username = "ubuntu" } build { sources = ["source.amazon-ebs.web"] provisioner "shell" { script = "scripts/install.sh" } provisioner "ansible" { playbook_file = "../ansible/playbooks/web.yml" } } -
Bake the image in CI. A PR merge to
mainruns Packer, tags the new image with a build ID, and writes the AMI ID into Terraform variables. -
Reference the image in the launch template. Terraform reads the latest AMI by tag.
data "aws_ami" "web" { most_recent = true owners = ["self"] filter { name = "name" values = ["acme-web-*"] } } resource "aws_launch_template" "web" { name_prefix = "web-" image_id = data.aws_ami.web.id instance_type = "t3.small" } -
Roll the fleet. AWS instance refresh, GCP rolling-update, or Kubernetes Deployment with
RollingUpdatestrategy all do the same thing — drain N instances, terminate, launch new from the latest image.aws autoscaling start-instance-refresh \ --auto-scaling-group-name acme-web \ --preferences MinHealthyPercentage=80,InstanceWarmup=120 -
Pair with config that is also versioned. App configuration that changes per environment lives outside the image — in SSM Parameter Store, in a Kubernetes ConfigMap, in environment variables from a launch template’s
user_data. The image carries no environment-specific config. -
Keep an exception path for stateful workloads. Databases, queues, and the like are not candidates for immutable cycling — their state would die with the instance. Either accept they remain mutable, or use a managed service (RDS, Aurora, Cloud SQL) whose state is decoupled from the host.
Common pitfalls
- An “immutable” fleet where engineers still SSH in to “just check something” is not immutable — disable interactive login on the AMI (no SSH key in cloud-init, console-only access via SSM) and force changes through the image.
- Images that take 45 minutes to build slow the entire iteration loop — keep the recipe tight and use layer caching where possible.
- A long-lived image (more than 30 days) accumulates CVEs you have not patched; bake nightly so the base is always fresh even when the app code did not change.
- Persistent local state (logs, sessions, caches) does not survive instance replacement — ship logs externally, terminate sessions on cycle, treat local disk as cache only.
- An image with secrets baked in is a secret leak waiting for an EBS snapshot to leave the account — secrets come from the secret store at instance boot.
In the engagements we run, immutable is the default for stateless services and the explicit non-default for stateful ones — Packer-baked AMIs, Terraform-driven instance refresh, no inbound SSH on application instances. The exceptions (databases, message brokers) are handled separately and intentionally. See /en/services/managed-operations/ and /en/solutions/aws-management/.