Packer is a tool for build a golden images also packer is lightwight. As of here, I have used to provision the image using a packer script.
- AWS IAM account needed for the AMI build using the packer
- Basic knowledge about AWS services especially AMI, EC2 and userdata.
Reference: packer download
wget https://releases.hashicorp.com/packer/1.7.3/packer_1.7.3_linux_amd64.zip
unzip packer_1.7.3_linux_amd64.zip
mv packer /usr/bin/
# -----------------------------------------------
# Timestamp variable declaration on running time
# -----------------------------------------------
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }
#--------------------------------------------------
# AMI (Golden image) Creation
#--------------------------------------------------
source "amazon-ebs" "apache" {
source_ami_filter {
filters = {
virtualization-type = "hvm"
name = "amzn2-ami-hvm-2.0.*.1-x86_64-ebs"
root-device-type = "ebs"
}
owners = ["amazon"]
most_recent = true
}
ami_name = "PACKER-TEST ${local.timestamp}"
region = "ap-south-1"
instance_type = "t2.micro"
ssh_username = "ec2-user"
###### IAM USER calling##################
access_key = "${var.access-key}"
secret_key = "${var.secret-key}"
tags = {
Env = "PACKER-TEST"
Name = "PACKER-TEST-${var.web_name}"
}
}
### TO BUILD
build {
sources = ["source.amazon-ebs.apache"]
provisioner "file" {
source = "siteinstallation.sh"
destination = "~/siteinstallation.sh"
}
provisioner "shell" {
inline = ["sudo bash ~/siteinstallation.sh"]
}
}
#!/bin/bash
yum install httpd unzip -y
## Below is downloading a html site template from tooplate site
wget https://www.tooplate.com/download/2126_antique_cafe
mv 2126_antique_cafe 2126_antique_cafe.zip
unzip 2126_antique_cafe.zip
sudo cp -rvf 2126_antique_cafe/* /var/www/html/
sudo chown apache. /var/www/html/ -R
sudo systemctl restart httpd
sudo systemctl enable httpd
################################################################
# Create a IAM programmatic user for the execution
###########################################################
variable "access-key" {
default = "<access-key-from-aws-console-when-user-created>"
}
variable "secret-key" {
default = "<secret-key-from-aws-console-when-user-created>"
}
variable "web_name" {
default = "site-html"
}
Check packer version using
packer version
packer init .
packer fmt .
packer validate .
packer build .