INF-43Recommended
Replace servers instead of patching them
Build a new machine image for every change, start new servers from it and stop the old ones. Never change a running server by hand.
How to do it
Build the image in CI with a tool such as Packer, or ship a container image. Start the new servers, wait for their health checks, then stop the old ones. A server changed by hand drifts away from the code, and nobody can rebuild it the same way. Replace each server at least every 30 days. Also roll out a new image within the time limit set by Install security updates by themselves requires. This rule does not fit one long-lived server. Use configuration management for that instead. Build each image the same way every time, as Make builds repeatable says.
Examples
Terraform example
resource "aws_launch_template" "web" {
image_id = data.aws_ami.web_latest.id
instance_type = "t4g.small"
}
resource "aws_autoscaling_group" "web" {
min_size = 2
max_size = 6
launch_template {
id = aws_launch_template.web.id
version = aws_launch_template.web.latest_version
}
instance_refresh {
strategy = "Rolling"
preferences { min_healthy_percentage = 100 }
}
}
How to check
No running server is older than 30 days. Every server runs the image ID of the latest build.
Sources
- AWS Well-Architected REL08-BP04 Deploy using immutable infrastructure
- OWASP Infrastructure as Code Security Cheat Sheet
Related rules
- INF-46 Install security updates by themselves in Infrastructure and environments
- INF-44 Configure long-lived servers from code in Infrastructure and environments
- ENG-21 Pin every version so builds repeat in Engineering
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/servers/immutable-servers">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.