<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

Packer 0.12.1

Nikhil Garge Jan 23, 2017

Packer

What is Packer?

  • Packer is an open source, lightweight tool created by Hashicorp that runs on most of the major operating systems
  • It is used to create machine images for multiple platforms. It has a high performance and creates machine images for different platforms in parallel
  • Packer is easy to use. It holds modern configuration management by encouraging use of tools like Puppet, Chef to install and configure various software packages in machine images

What is a Machine Image?

  • Machine image is a static unit that contains state of a machine at a certain instance of time which comprises of operating system and software(s) installed in the machine along with their configurations
  • Machine images can be used to create a new machine quickly in running state
  • Examples: AMI (Amazon Machine Image) in AWS EC2, VMDK for VMware, etc.

Advantages of using Packer:

  • Fast: Once templates for creating machine images are ready, one can provision machines for complete development team in seconds, as spawning a machine from an image file is a quick process
  • Supports multiple platforms: Supports building images for multiple platforms like AWS, Openstack, VMware, Virtualbox, DigitalOcean, Docker, Cloudstack and many more in parallel
  • Easy to Test: Once an image is built, one can test functionality of underlying application by launching instance in the required platform and later replace machine in production with the machine created from built image if test succeeds

Use Case:

  • Packer can be used in the process of Continuous Delivery to build an image, once there is a new change received
  • This can help to quickly test and deploy the changes and revert to the old state in less time

Installing Packer:

cd /opt && wget https://releases.hashicorp.com/packer/0.12.1/packer_0.12.1_linux_amd64.zip

unzip packer_* 

mv packer /usr/sbin/packer
packer version
  • The final command should print the version number of packer tool

Building an Image:

  • We’ll be building an image on AWS. For this we need to have an AWS account which can be obtained by registering here. Once you complete the sign up process, you can login into your account.
    Note: Following process though uses a free tier instance type t2.micro, you may be charged a few cents
  • The complete configuration which defines the image is in JSON format. We’ll create a JSON file which we’ll use to build the machine image
 cd /opt && vim example.json
  • Below is a JSON extract for reference that creates an AMI (Amazon Machine Image) under AWS EC2 service
{
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "",
"secret_key": "",
"region": "us-east-1",
"source_ami": "ami-fce3c696",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example "
}]
}
  • In the above template:

aws_access_key and aws_secret_key are user variables which will be passed as parameters while building the image from the template

Within JSON, builders contains an array of elements that are responsible for creation of a machine and then packing that machine into an AMI (Amazon Machine Image)

We provide basic values like the ssh_username, source_ami, region and instance_type which are used to create an EC2 instance in AWS environment

Lastly we also provide ami_name which will be used as a value for key name to store final machine image under the AMI section of AWS EC2

  • Validating JSON template:
    Once the template is ready, we can validate it using below command:
 packer validate example.json

 On successful validation, we get messasge as: Template validated successfully

  • Once validated, we are now ready to build our image using JSON file we had created. Use below command to build an AMI:
 packer build
  • You can monitor the console to check process being executed. On successful completion of process, you get ami id of AWS AMI created
  • The AMI is ready to use. This was a basic example of how packer works. We can additionally pass provisioners to JSON file we created above, to explore real power of packer

Provisioners:

  • Provisioning helps us to install and configure additional software’s into the image while its being created
  • We’ll use provisioners to install apache webserver along with php in our AMI
  • Below is the modified template including provisioners section:
 {
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "",
"secret_key": "",
"region": "us-east-1",
"source_ami": "ami-fce3c696",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example "
}],
"provisioners": [{
"type": "shell",
"inline": [
"sleep 50",
"sudo apt-get update",
"sudo apt-get install -y apache2 php"
]
}]
}
  • Below command uses the updated JSON to validate and then create a new machine image:
packer validate example.json && packer build
  • We asked the packer to instantiate provisioner of type shell and install, then pull updates. Later we passed the command to install the apache2 and php packages using the apt-get
  • This process spawns an EC2 instance using specification specified in the builders section as we had already covered in previous section. Along with it, after instance is up, it installs the packages passed from provisioners section. Later, it packs the image with installed software packages and final image is stored in the AMI section of AWS EC2
  • In same way, we can use this technique to install desired software, configure them as per requirement. Also, we can download the application in the desired directory and later create an image of it which can later be deployed

Conclusion:

  • With the help of packer we were able to create machine images within seconds at a basic level which could later be used to create a running machine on its desired platform
  • We also learnt to install additional software’s using command line of OS installed over the machine and later create its static machine image with installed software’s. Later, this helps us as we can have a machine with preconfigured tools
  • This can help us in building up machines for developers in short time, creating images of machines with latest changes to be deployed as part of continuous delivery system

References:

https://www.packer.io

Similar Blog

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.