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)