Creating custom Amazon Machine Images (AMIs) is crucial in automating cloud infrastructure. This article walks through using Packer to build AWS AMIs and integrates it with Terraform for seamless deployment.
By the end of this guide, you’ll have a customized AWS AMI with Apache (httpd) installed and configured.
The workflow includes:
1. Creating a Packer template file.
2. Writing a provisioner script.
3. Validating and building the image.
4. Deploying infrastructure with Terraform.
5. Cleaning up AWS resources.
Step 1: Install and Configure Packer
Before we begin, ensure the following Packer is installed.
Here’s how you do it on Linux/MacOS:
curl -fsSL https://releases.hashicorp.com/packer/1.8.6/packer_1.8.6_linux_amd64.zip -o packer.zip
unzip packer.zip
sudo mv packer /usr/local/bin/
On Windows:
Download Packer from the official site and follow the installation guide.
- Verify installation
packer version
Next, Configure AWS CLI.
aws configure
Step 2: Set Up the Packer Template
Clone the project repository and navigate to the required directory:
git clone https://github.com/Here2ServeU/packer.git
cd packer/aws
Now, create the Packer template file (aws-ami-packer.json):
{
"builders": [
{
"type": "amazon-ebs",
"region": "us-east-1",
"source_ami": "ami-0c02fb55956c7d316",
"instance_type": "t2.micro",
"ssh_username": "ec2-user",
"ami_name": "t2s-demo-ami-{{timestamp}}"
}
],
"provisioners": [
{
"type": "shell",
"script": "provisioner.sh"
}
]
}
This template defines:
• AWS region: us-east-1
• Source AMI: A predefined Amazon Linux image.
• Instance type: t2.micro for cost efficiency.
• Provisioner script: provisioner.sh (next step).
Step 3: Create a Provisioner Script
A provisioner script automates package installation and system setup inside the AMI. Create provisioner.sh:
#!/bin/bash
# Wait for system updates to complete
sleep 10
# Kill any running yum processes
sudo pkill -9 yum || true
# Wait until the yum lock is released
while sudo fuser /var/run/yum.pid >/dev/null 2>&1; do
echo "Waiting for yum lock...";
sleep 5;
done
# Remove yum lock files
sudo rm -rf /var/run/yum.pid
# Stop and disable automatic yum updates
sudo systemctl stop yum-cron || true
sudo systemctl disable yum-cron || true
# Clean and update yum repositories
sudo yum clean all
sudo yum update -y
# Install Apache web server
sudo yum install -y httpd
# Enable and start Apache
sudo systemctl enable httpd
sudo systemctl start httpd
# Create a basic HTML page
echo "<html><body><h1>Welcome to T2S Web Server</h1></body></html>" | sudo tee /var/www/html/index.html
Make the script executable:
chmod +x provisioner.sh
Step 4: Validate and Build the AMI
1. Validate the Packer template
packer validate aws-ami-packer.json
2. Build the image
packer build aws-ami-packer.json
After successful execution, the new AMI will be available in the AWS EC2 AMI Dashboard.
Step 5: Deploy Infrastructure Using Terraform
Once the AMI is created, deploy an EC2 instance with Terraform.
1. Navigate to Terraform project directory (web-app-create):
cd web-app-create
2. Modify the variables.tf file to use the new AMI ID and other desired values.
3. Initialize Terraform:
terraform init
4. Preview changes:
terraform plan
5. Apply and deploy infrastructure:
terraform apply
Step 6: Verify the Deployment
1. Go to AWS EC2 Dashboard → Instances.
2. Find the newly launched instance.
3. Access the web server:
curl http://<EC2-Public-IP>
You should see:
<html><body><h1>Welcome to T2S Web Server</h1></body></html>
Step 7: Clean Up AWS Resources
To prevent unnecessary costs, clean up unused resources.
1. Delete the AMI:
aws ec2 deregister-image --image-id <ami-id>
2. Destroy Terraform-managed resources:
terraform destroy
Conclusion
By following this guide, you have:
Automated AWS AMI creation with Packer.
↳ Installed and configured Apache Web Server in the AMI.
↳ Deployed infrastructure using Terraform.
↳ Cleaned up resources to avoid unnecessary costs.
Using Packer and Terraform, you can quickly build secure, reusable, and scalable AMIs for deployment across AWS environments.
Join My Mentorship Program!
To break into Cloud and DevOps or advance your skills, check out my Beginner2DevOps 8-week mentorship program at T2S. Gain hands-on experience, expert guidance, and a clear path to success.
🔗 Enroll now at www.transformed2succeed.com, and connect with me on LinkedIn at https://www.linkedin.com/in/ready2assist/, to stay updated!