Packer
- Packer is a tool for creating machine images for multiple platforms
- In the case of AWS we call this machine images as AMI and in the case of Azure we call it as VM Image.

- Some of the other machine image formats are
- VMDK/VMX (Vmware)
- OVF
- VHD (HyperV)
Use Cases
- Continuous Delivery: For every release a machine image is created using packer which will be used by terraform to create virtual machines

- Packer is used in Demo Environmets as it consists of machine images
Becoming Effective with Packer
- Understand how to create machine image manually
- Be effective with json format
Basic Packer Terms
- Builder: Builder lets you select the virtual environment (aws, azure, vmware etc) to create machine images
- Popular Builders are:
- Amazon EC2
- Azure
- Digital Ocean
- Alicloud ECS
- Hyper-V
- Virtual Box
- VmWare
- OpenStack
- Provisioner: Are built-in and third-party software to install and configure the machine images. Popular provisioners
- Ansible
- Chef
- Salt
- puppet
- Shell
- Windows Shell
Machine image creation (Manual)
- Lets try to create a machine image for lamp stack.
- For lamp stack steps refer here
- Linux Steps
sudo apt-get update
sudo apt install apache2 -y
sudo apt install php libapache2-mod-php php-mysql php-cli -y
sudo systemctl restart apache2
sudo nano /var/www/html/info.php
sudo systemctl enable apache2
sudo systemctl restart apache2
###
<?php
phpinfo();
?>
http://<publicip>/info.php

How to create a AMI in AWS
- Create a EC2 instance
- Install/Configure your application
- Create image (AMI) from EC2 instance and then use the image (AMI) create for creating further machines
- Delete the Ec2 instance created in step 1 (Optional)

How to create a VM Image in Azure
- Create an Azure VM
- Install/Configure your application
- Create vm image from azure vm and then use the vm create for creating further machines
- While creating the VM Image, Azure deallocates (Stops) vm and generalize vm (Removing specific user configuration)
- Delete the Azure VM created in step1 as this vm becomes unusable

Installing Packer
- Download Packer from here and add it to the path variable
- If you have choco installed
choco install packer -y
Machine image creation Using Packer
AWS AMI Creation
- Lets get started with basic json
{
}
{
"builders": [
]
}
- Now since we are using aws, lets find aws builders Refer Here
{
"builders": [
{
"type": "amazon-ebs",
"access_key": "",
"secret_key": "",
"region": "us-west-2",
"ami_name": "mylampv1.0",
"source_ami": "ami-003634241a8fcdec0",
"instance_type": "t2.micro",
"ssh_username": "ubuntu"
}
]
}
{
"builders": [
{
"type": "amazon-ebs",
"access_key": "",
"secret_key": "",
"region": "us-west-2",
"ami_name": "mylampv1.0",
"source_ami": "ami-003634241a8fcdec0",
"instance_type": "t2.micro",
"ssh_username": "ubuntu"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"sudo apt-get update",
"sudo apt install apache2 -y",
"sudo apt install php libapache2-mod-php php-mysql php-cli -y",
"sudo systemctl restart apache2",
"echo '<?php phpinfo(); ?>'/var/www/html/info.php ",
"sudo systemctl enable apache2",
"sudo systemctl restart apache2"
]
}
]
}
- Now execute the following commands
packer --help
packer validate --help
packer validate aws.json
packer inspect aws.json

- Now lets create amazon ami by executing
packer build aws.json and if you want to execute step by step packer build -debug aws.json

- Now lets add variables refer here
{
"variables": {
"aws_secret_key": "",
"aws_access_key": "",
"source_ami": "ami-003634241a8fcdec0"
},
"builders": [
{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-west-2",
"ami_name": "mylamp {{timestamp}}",
"source_ami": "{{user `source_ami`}}",
"instance_type": "t2.micro",
"ssh_username": "ubuntu"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"sudo apt-get update",
"sudo apt install apache2 -y",
"sudo apt install php libapache2-mod-php php-mysql php-cli -y",
"sudo systemctl restart apache2",
"echo '<?php phpinfo(); ?>'/var/www/html/info.php ",
"sudo systemctl enable apache2",
"sudo systemctl restart apache2"
]
}
]
}
- Now execute packer
packer build -var "aws_secret_key=xxxxx" -var "aws_access_key=xxxx" aws.json
- To the packer script we can add functions Refer Here
Creating VM image in Azure using Packer
Exercise:
- Write a Packer template to create nop Commerce image on aws or azure and then use that image in terraform
