📦 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",
    ]
  }
}
  1. Build the image:
packer init .
packer build ubuntu.pkr.hcl

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
  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! 😊