DEV Community

Cover image for How to Create Your Own AWS AMIs Using Packer
DevTutsPro
DevTutsPro

Posted on • Edited on • Originally published at devtutspro.in

How to Create Your Own AWS AMIs Using Packer

**Originally published on [DevTutsPro]*


1. Why Custom AMIs Matter
Creating your own custom Amazon Machine Images (AMIs) can:

  • Speed up EC2 instance launches ๐Ÿš€
  • Reduce repetitive manual setup tasks
  • Ensure environment consistency across production, staging, and development

In this blog, youโ€™ll learn how to:

  • Build a custom AWS AMI using Packer
  • Install essential tools like Git and Docker automatically
  • Understand why Packer beats manual AMI creation
  • Clean up and test AWS resources after the build

2. Initial Setup: EC2 Instance + Packer

  • Launch a Base EC2 Instance (t2.micro)
  • AMI: Amazon Linux 2
  • Instance Type: t2.micro
  • Key Pair: Ensure SSH access (port 22)

Installing Packer on EC2

For Amazon Linux / RHEL:

sudo yum update -y
sudo yum install -y yum-utils unzip
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum install packer -y
Enter fullscreen mode Exit fullscreen mode

For Ubuntu/Debian:

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packer -y
Enter fullscreen mode Exit fullscreen mode

Check Version:

packer --version
Enter fullscreen mode Exit fullscreen mode

3. IAM Role Creation

Create an IAM role with the following settings:

  • Trusted Entity: EC2
  • Attach Policies:

  • AmazonEC2FullAccess

  • AmazonSSMFullAccess

  • IAMInstanceProfileRole

  • (Optional) AmazonS3FullAccess

Attach this role to your EC2 instance that will run Packer.


4. Create Your Packer Template

ami.pkr.hcl File:

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

source "amazon-ebs" "amazon-linux" {
  region          = "ap-southeast-2"
  ami_name        = "ami-version-1.0.1-{{timestamp}}"
  instance_type   = "t2.micro"
  source_ami      = "ami-0d6294dcaac5546e4"
  ssh_username    = "ec2-user"
  ami_regions     = ["ap-southeast-2"]
}

build {
  name = "hq-packer"
  sources = ["source.amazon-ebs.amazon-linux"]

  provisioner "file" {
    source      = "provisioner.sh"
    destination = "/tmp/provisioner.sh"
  }

  provisioner "shell" {
    inline = [
      "chmod a+x /tmp/provisioner.sh",
      "ls -la /tmp",
      "pwd",
      "cat /tmp/provisioner.sh",
      "/bin/bash -x /tmp/provisioner.sh"
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

provisioner.sh File:

#!/usr/bin/env bash

# Update packages
sudo yum -y update

# Install Git
sudo yum install git -y

# Install Docker
sudo yum install docker -y
sudo systemctl start docker
Enter fullscreen mode Exit fullscreen mode

5. Build the AMI with Packer

packer init .
packer validate ami.pkr.hcl
packer build ami.pkr.hcl
Enter fullscreen mode Exit fullscreen mode

This will:

  • Launch a temporary EC2 instance
  • Execute the provisioning script
  • Create a new AMI
  • Terminate the temporary instance

6. Check the Created Resources

View AMI in AWS Console:

  1. Open the EC2 Dashboard
  2. In the left sidebar, click AMIs
  3. Filter by Owned by Me
  4. Look for an AMI named similar to: ami-version-1.0.1-<timestamp>

7. Clean Up Unused Resources

Why Cleanup Is Important

Leaving unused AMIs and snapshots increases costs and clutter. Always delete temporary resources when no longer needed.

Steps to Clean Up Resources

Delete the Custom AMI:

  1. Go to EC2 โ†’ AMIs
  2. Select the custom AMI (e.g., ami-version-1.0.1-<timestamp>)
  3. Click Actions โ†’ Deregister AMI

Delete Associated Snapshots:

  1. Go to EC2 โ†’ Snapshots
  2. Find the snapshot linked to the AMI (check description)
  3. Select it โ†’ Click Actions โ†’ Delete Snapshot

Confirm Temporary EC2 Termination:

  • Go to EC2 โ†’ Instances
  • Filter by name or recently created instances
  • Confirm that no temporary instance from the build is running
  • If you find one still running, terminate it manually

8. Conclusion

Using Packer to create custom AMIs delivers:

  • Faster instance launches
  • Consistent, automated builds
  • Seamless CI/CD integration

Whether for production, staging, or test environments, Packer ensures your infrastructure is repeatable and reliable.

๐Ÿ“ Read the full original post here:
https://www.devtutspro.in/2025/08/creating-custom-amis-with-packer-step.html

Top comments (0)