Images and Provisioning
Immutable OS images and boot-time automation
Machine images (AMI, Azure image, Compute Engine image) define what boots. Pipeline-built images plus user-data/cloud-init turn VM launching into a repeatable, scriptable operation.
What you will learn
- Explain image vs live-config provisioning
- Build an image with Packer or a provider builder
- Use cloud-init and user-data to configure at boot
An image is a frozen bootable disk (OS + packages + config). Provisioning with images means boot identical, pre-baked machines, then hand runtime drift-bearing config to cloud-init at first boot.
What is this?
AWS AMIs are regional images captured from a source or built with Packer; Azure uses image definitions in a shared image gallery (SIG); GCP uses compute images and image families. Boot automation: user-data scripts, cloud-init (write_files, packages, users, runcmd), and instance templating (Launch Templates, VM image + config, instance templates). Images are immutable — never patch a running one, build a new version.
Why do you need it?
- Baking packages into images cuts boot from 15 min to < 2 min.
- Reproducible images kill the 'works on that one box' class of bugs.
- Golden images are the substrate for autoscaling: scale-out boots identical workers.
- Provisioning logs (cloud-init) turn boot mysteries into debuggable steps.
Mental model
A frozen dinner vs cooking from scratch each time
Images vs live provisioning
Bake the meal (image) ahead with all ingredients standardized; at service time pop it in the oven (boot) with the table setting configured by the host (user-data).
How it works
- Packer builds an image: launch a base, run provisioners, snapshot the disk, register the AMI/image.
- Image families let you always reference the latest: ami-family, image family 'ubuntu-2204-lts'.
- At boot, the metadata service injects user-data; cloud-init runs it (write files, install packages, run commands).
- Launch templates/templates pin image, size, disks, SG, key, and user-data in one reusable definition.
- New image versions are promoted through environments, then the ASG/MIG rolls instances gradually.
Image build to fleet roll.
Examples
Boot-time configuration with cloud-init
#user-data#cloud-configpackages: - nginxwrite_files: - path: /etc/site/index.html content: "hello from boot config"runcmd: - systemctl enable --now nginx - curl -sf http://169.254.169.254/latest/meta-data/tags > /tmp/tags.txtcloud-init applies packages, files and commands on first boot, so a bare AMI becomes a configured server in seconds.
In production
- A payment platform builds a hardened golden AMI with CIS-hardened OS, agents and app binary each release.
- Azure shared image gallery distributes standard images across subscriptions and regions with replication.
- GCP MIGs roll image updates from a versioned template with canary and rolling replacement.
Best practices
- Keep images minimal and patch a day-old base to trim window for fresh CVEs.
- Version images semantically; tag the AMI id in the release manifest.
- Store user-data in Version Control, not in the console, and log cloud-init output.
- Scan images (ECR/Artifact Registry/SIG scanning) before they reach production fleets.
Common mistakes
- Patching running instances forever instead of rebuilding images.
- Committing OS drift: image from last year, packages updated haphazardly.
- Forgetting cloud-init only runs once on first boot — pre-bake the config.
- Regional image mismatch: an AMI copied to another region with the old ID breaks launch.
Common issues
App missing after bootPackage installed by cloud-init failed or never ran
Inspect cloud-init-output.log; ensure the package manager cache is reachable at boot.
Image copy across regions missingRegion-scoped images not replicated
Copy AMI or add Azure SIG replicas / GCP cross-region snapshots.
Debugging tips
- Tail /var/log/cloud-init-output.log for first-boot failures.
- Check immutability: after the first boot, config changes should come from a new image.
- If an instance 'loses' its config, verify user-data was attached and cloud-init completed.
- Compare MD5 of launched image vs your golden image; a mismatch means the wrong image launched.
Interview checkpoint
Quick quiz
cloud-init runs on first boot of a fresh instance only.