DEV Community

Cover image for ๐Ÿ› ๏ธ Automate AMI Builds with Packer + EC2 Image Builder
francotel
francotel

Posted on

๐Ÿ› ๏ธ Automate AMI Builds with Packer + EC2 Image Builder

๐Ÿ“ฆ DevOps Battle for Golden Images in AWS


๐Ÿงญ 1. Why Automate AMI Builds?

Golden AMIs ensure that all your EC2 instances start with consistent, hardened, and pre-configured environments.

โŒ Manual builds = human errors, time waste, inconsistent images

โœ… Automated builds = repeatable, secure, auditable


โš”๏ธ 2. Packer vs EC2 Image Builder: The Showdown

Feature ๐Ÿงฐ Packer ๐Ÿ–ผ๏ธ EC2 Image Builder
๐Ÿ”ง Configuration Language JSON or HCL YAML or Console
๐Ÿ”„ Integration with CI/CD โœ… Very flexible โš ๏ธ Limited (manual triggers, EventBridge workaround)
๐Ÿ›ก๏ธ Security Controls โœ… Custom via hardening scripts โœ… SSM + IAM roles
๐Ÿ“ฆ Output Formats AMIs, Docker, Vagrant, etc. AMIs (only)
โ˜๏ธ AWS Native โŒ Third-party tool โœ… Fully managed
๐Ÿ“œ Logging & Visibility CLI or external โœ… CloudWatch logs, detailed history
๐Ÿงช Testing AMIs Manual or via external tools โœ… Supports testing phases (e.g., InSpec)
๐Ÿ’ฐ Cost Low (runs in your own infra) Low, but requires pipeline resources

๐Ÿ TL;DR:

  • Use Packer when you need portability and full control.
  • Use EC2 Image Builder when you prefer AWS-native, low-maintenance pipelines.

๐Ÿ”ง 3. Demo: Building a Hardened Ubuntu AMI in Both Tools

Option A: Using Packer + HCL

  1. Define ubuntu.pkr.hcl:
packer {
  required_plugins {
    amazon = {
      version = ">= 1.2.8"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "ubuntu" {
  ami_name      = "learn-packer-linux-aws"
  instance_type = "t2.micro"
  region        = "us-west-2"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }
  ssh_username = "ubuntu"
}

build {
  name    = "learn-packer"
  sources = ["source.amazon-ebs.ubuntu"]
  provisioner "shell" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y nginx",
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Build the image:
packer init .
packer build ubuntu.pkr.hcl
Enter fullscreen mode Exit fullscreen mode

packer-build

ami-ec2

Option B: Using EC2 Image Builder

Create a Recipe

  1. Example component in YAML:
name: install-nginx
description: Install Nginx
phases:
  build:
    commands:
      - apt update
      - apt install -y nginx
Enter fullscreen mode Exit fullscreen mode
  1. Create a Pipeline with:
  • Source AMI: Ubuntu 22.04
  • Build Component: install-nginx
  • Test Component (optional): e.g., check Nginx service
  • Output: AMI in target region

Trigger manually or via EventBridge for automation (e.g., weekly builds)

ec2-image-builder

๐ŸŽฏ 5. Conclusions & Recommendations

Verdict:

๐Ÿงฐ Use Packer if:

  • You want cross-cloud image creation
  • You need integration with existing pipelines
  • You're comfortable managing infrastructure

๐Ÿ–ผ๏ธ Use EC2 Image Builder if:

  • You're all-in on AWS
  • You want minimal setup with native controls
  • You want to integrate with AWS Config, IAM, and SSM easily

๐Ÿค Let's Connect!

If you find this repository useful and want to see more content like this, follow me on LinkedIn to stay updated on more projects and resources!

LinkedIn

If youโ€™d like to support my work, you can buy me a coffee. Thank you for your support!

BuyMeACoffee

Thank you for reading! ๐Ÿ˜Š

Top comments (0)