Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Images for Cloud and VMs

Building AMIs on AWS with amazon-ebs

Build Amazon Machine Images with the Packer amazon-ebs builder: source AMI filters, instance types, tags, encryption, sharing and cleanup, with a runnable naming checker.

The most common first real-world Packer job is an Amazon Machine Image (AMI). An AMI is the template AWS uses to launch EC2 instances. If your team bakes the operating system, agents and application runtime into an AMI, every new instance boots ready to work instead of spending minutes installing packages.

The Packer plugin for AWS ships several builders. The most used is amazon-ebs: it launches a temporary EC2 instance from a source AMI, runs your provisioners, stops the instance, snapshots its root EBS volume into a new AMI and then terminates the temporary instance. This lesson walks through that flow and the settings that matter in production.

This site cannot run Packer or reach AWS. The HCL below is read-only, and the runnable examples are small JavaScript stand-ins that teach the same logic (naming, filtering, tag rules). Nothing here shows real Packer output.

The lifecycle of an amazon-ebs build

What amazon-ebs does

Find source AMI Filters pick the base image
Launch builder Temporary instance
Provision Shell, Ansible, files
Snapshot New AMI is created

The builder instance is always terminated at the end

1 / 2

A complete template

HCL
packer {
  required_plugins {
    amazon = {
      source  = "github.com/hashicorp/amazon"
      version = ">= 1.3.0"
    }
  }
}

variable "region" {
  type    = string
  default = "eu-central-1"
}

locals {
  timestamp = formatdate("YYYYMMDD-hhmm", timestamp())
}

source "amazon-ebs" "base" {
  region        = var.region
  instance_type = "t3.small"
  ssh_username  = "ubuntu"
  ami_name      = "web-base-${local.timestamp}"

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd-gp3/ubuntu-*-24.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    owners      = ["099720109477"]
    most_recent = true
  }

  tags = {
    Name      = "web-base"
    BuiltBy   = "packer"
    Release   = local.timestamp
  }
}

build {
  sources = ["source.amazon-ebs.base"]

  provisioner "shell" {
    inline = ["sudo apt-get update", "sudo apt-get -y upgrade"]
  }
}

Run it with packer init ., packer validate . and packer build .. Packer needs AWS credentials from the usual places: environment variables, a shared credentials file, an SSO profile or an instance role. Never put keys in the template.

Choosing the source AMI

You can hard-code source_ami = "ami-...", but AMI ids differ per region and go stale. A source_ami_filter is better: it describes the image by name pattern and owner, and most_recent = true picks the newest match. Always set owners to a trusted account id (for example the publisher's official account) or the alias amazon. A filter without an owner could match a malicious public image with a similar name.

Settings that matter

Setting Why it matters
instance_type The builder size. A bigger type finishes provisioning faster; it is only billed while building.
ssh_username Depends on the source image (ubuntu, ec2-user, admin). Wrong value gives a long SSH timeout.
ami_name Must be unique per region. Add a timestamp or a version.
tags / snapshot_tags Tag the AMI and its snapshot for cost and ownership. run_tags tags the builder instance.
encrypt_boot + kms_key_id Encrypt the resulting AMI's volumes.
ami_regions Copy the finished AMI to more regions.
ami_users Share with other AWS account ids (launch permission).
subnet_id, vpc_id, associate_public_ip_address Control where the builder runs, for example a private subnet with a bastion.
launch_block_device_mappings Resize the root volume, change volume type.
imds_support = "v2.0" Require IMDSv2 on instances launched from the image.
force_deregister, force_delete_snapshot Overwrite an AMI of the same name (use carefully).

Cleaning up and failure handling

If a build fails, Packer terminates the builder instance and deletes temporary key pairs and security groups it created. With -on-error=ask you can keep the instance to debug over SSH, but you must then clean it up yourself, otherwise it keeps costing money. Old AMIs and their snapshots are not removed automatically: plan a retention policy (see image rebuild schedules and lifecycle).

Try it: validate AMI names and tags

AMI names allow 3 to 128 characters of letters, digits and a few symbols (( ) [ ] . / - ' @ _). This stand-in checks a name and a tag set against rules a team might enforce.

JS AMI name and tag checker (stand-in)

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

No output.

Try it: pick the newest source image

most_recent = true sorts matching images by creation date. This stand-in applies a name pattern and an owner filter and then picks the newest.

JS Source AMI filter (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. What does the amazon-ebs builder do after provisioning finishes?

    The temporary instance is only a workbench. The result is the new AMI and its snapshot.
  2. 2. Why should a source_ami_filter always set owners?

    Name patterns are not unique across accounts. The owner pins the image to a trusted publisher.
  3. 3. Which setting copies the finished AMI to additional regions?

    ami_users shares with accounts, while ami_regions copies the image to other regions.
  4. 4. Where should AWS credentials for Packer come from?

    Use the standard AWS credential chain so secrets never enter the template or Git.

Summary

  • amazon-ebs launches a temporary instance, provisions it, snapshots it into an AMI and cleans up.
  • Use source_ami_filter with owners and most_recent instead of hard-coded AMI ids.
  • Give every AMI a unique name and consistent tags; encrypt and require IMDSv2.
  • Use ami_regions and ami_users to distribute an image; plan to delete old AMIs and snapshots.

Next: Azure and Google Cloud images