Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท CI/CD Patterns

Rebuild Schedules and Image Lifecycle

Keep golden images fresh: rebuild on a schedule and on triggers, track image age, retire old AMIs and snapshots with retention rules, and plan cost, drift and rollout waves.

An image is a snapshot in time. The day it is built it contains the latest patches; thirty days later it is missing a month of security fixes. Teams that build an image once and use it for years end up with servers running outdated, vulnerable software. The remedy is a lifecycle: rebuild regularly, roll out gradually, and delete what is no longer needed.

Packer and cloud APIs cannot run on this site. The cron and policy snippets are read-only; the JavaScript examples are stand-ins that model age rules and retention so you can adapt them to your own tooling.

Why images go stale

  • OS packages receive security updates continuously.
  • Base images (the vendor's official AMI or container image) are rebuilt by the vendor.
  • Bundled agents (monitoring, security, runtime) release new versions.
  • Compliance baselines change.
  • Your own application dependencies and configuration evolve.

Rebuild triggers

Combine time-based and event-based triggers:

Trigger Example Why
Schedule Weekly cron in CI Pick up routine patches
New parent image Vendor publishes a new base AMI (event or daily check) Stay on a supported base
Template change Merge to main touching images/ New software or config
Security advisory Critical CVE in an installed package Out-of-band rebuild
Dependent image changed A parent image in your own chain was rebuilt Children inherit the fix

A weekly schedule is a good default; critical fixes use a manual or webhook-triggered run of the same pipeline (see CI/CD with GitHub Actions).

Image chains

Large organizations build in layers: an os-base image (hardened OS), then runtime-java or runtime-node on top, then application images. Each layer starts from the previous layer's artifact. Rebuilding the base should cascade to the children, and a registry with ancestry tracking (versioning lesson) shows which children are built on an outdated parent.

flowchart TD
  OS["os-base (weekly)"] --> J["runtime-java"]
  OS --> N["runtime-node"]
  J --> A1["app-orders"]
  N --> A2["app-frontend"]

A new os-base triggers rebuilds of everything beneath it.

Rolling out a fresh image

Rebuilding is not the same as deploying. Existing servers keep running the old image until replaced. With immutable infrastructure you roll out by replacing instances:

  1. Promote the new version to staging, run tests.
  2. Update the auto scaling group or instance template to the new image.
  3. Replace instances gradually (rolling or blue/green), watching health checks.
  4. Move the prod channel only after a canary succeeds; keep the old version for rollback.

Track image age across the fleet: the number of running instances whose image is older than your limit (for example 30 days). That is a simple, powerful security metric.

Retention and cleanup

Every AMI has a snapshot, and every snapshot costs money; cloud accounts have limits on the number of images, too. Define a policy and automate it:

  • Keep the last N versions per image family (for example 5), plus anything currently referenced by a channel, launch template or running instance.
  • Delete images older than a maximum age (for example 90 days) when not referenced.
  • Deregister the AMI and delete its snapshots; deregistering alone leaves billable snapshots.
  • Delete failed-build leftovers: untagged images, stray builder instances and security groups, found by tag.
  • Run cleanup as a scheduled pipeline job with a dry-run mode that prints what it would delete, reviewed before enabling deletion.
  • Protect with a tag such as Retain=true for audit or legal holds.

Cloud-native tools can help: lifecycle policies in Amazon Data Lifecycle Manager, EC2 Image Builder lifecycle policies, Azure Compute Gallery version end_of_life_date and exclude_from_latest, GCE image deprecation states (DEPRECATED, OBSOLETE, DELETED).

Cost and time

Builds are short and cheap per run, but multiply by regions, architectures and image families. Reduce the bill by using smaller builder instance types for light provisioning, copying to regions only where needed, sharing instead of copying across accounts when possible, and deleting old snapshots. Reduce build time by layering and by baking slow-changing software into a base image.

Try it: age and retention policy

JS Image retention planner (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: fleet image age

JS Fleet age report (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why rebuild an image that has not changed?

    An image's contents age even if your template did not change.
  2. 2. What must you delete in addition to the AMI itself?

    Deregistering leaves billable snapshots behind unless you delete them.
  3. 3. What does rebuilding the image do to already running servers?

    Immutable infrastructure rolls out by replacing instances.
  4. 4. Which metric is a simple fleet security indicator?

    Image age shows how many servers miss recent patches.

Summary

  • Images age; rebuild weekly and on events such as new parent images or critical advisories.
  • Layered chains should cascade rebuilds; track ancestry.
  • Roll out by replacing instances gradually and keep the previous version for rollback.
  • Automate retention: keep the last N and referenced images, delete the rest including snapshots, dry-run first.

Next: Project: a golden web server image