How to Create Custom AWS AMIs with Packer – Step-by-Step Tutorial

*Originally published on [DevTutsPro]
1. Why Create Custom AMIs?
Building your own Amazon Machine Images (AMIs) offers several advantages:
🚀 Faster EC2 launches – pre-configured images save boot time.
🔄 Eliminate repetitive setup – no more manual installation for each instance.
📦 Consistent environments – identical setups across production, staging, and development.
In this guide, you’ll discover how to:
Create a custom AWS AMI using Packer.
Automatically install essential tools like Git and Docker.
Learn why Packer outperforms manual AMI creation.
Properly test and clean up 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
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
Check Version:
packer --version
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" ] } }
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
5. Build the AMI with Packer
packer init . packer validate ami.pkr.hcl packer build ami.pkr.hcl
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:
Open the EC2 Dashboard
In the left sidebar, click AMIs
Filter by Owned by Me
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:
Go to EC2 → AMIs
Select the custom AMI (e.g.,
ami-version-1.0.1-<timestamp>
)Click Actions → Deregister AMI
Delete Associated Snapshots:
Go to EC2 → Snapshots
Find the snapshot linked to the AMI (check description)
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 AWS AMIs provides:
⚡ Faster EC2 instance launches – reduce startup time with pre-baked configurations.
🔄 Consistent, automated builds – eliminate human errors and ensure identical environments.
🚀 Seamless CI/CD integration – easily fit into modern DevOps workflows.
Whether you’re deploying to production, staging, or testing, Packer makes your infrastructure repeatable, reliable, and cost-efficient.
📝 Read the full original post here: https://www.devtutspro.in/2025/08/creating-custom-amis-with-packer-step.html
Subscribe to my newsletter
Read articles from Venkat Reddy directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
