Creating custom AMIs helps speed up EC2 instance launches, reduce manual work, and improve consistency across all environments. In this blog, you’ll learn how to build a custom AMI using Packer, install tools like Git and Docker, understand why Packer is better than manual AMIs, and how to clean up and test the resources that are created.
1. What is Packer?
Packer is an open-source tool from HashiCorp used to automate the creation of AMIs (Amazon Machine Images). It supports multiple platforms, including AWS.
Why Use Packer for Custom AMIs?
Feature | Manual AMI Setup | Custom AMI with Packer |
---|---|---|
Time to Launch EC2 | Slower | Faster |
Repeatable Builds | No | Yes |
Automation Ready | Minimal | Fully automated |
Errors and Variance | High | Low |
Integration with CI/CD | Difficult | Seamless |
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 AMIs is a powerful and automated solution for deploying consistent infrastructure. With faster instance start times, baked-in configurations, and repeatable builds, it’s ideal for production, staging, and CI/CD pipelines.
Always remember to clean up unused AMIs and snapshots to save costs and keep your AWS account organized.