HashiCorp Packer for Machine Images
Last updated
Was this helpful?
Last updated
Was this helpful?
Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel. Packer does not replace configuration management like Chef or Puppet.
In fact, when building images, Packer is able to use tools like Chef or Puppet to install software onto the image.A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines. Machine image formats change for each platform. Some examples include AMIs for EC2, VMDK/VMX files for VMware, OVF exports for VirtualBox, etc.
If you’ve ever used Vagrant you may be thinking, what is different between Vagrant and Packer? Put simple Vagrant pulls down a pre built virtual machine from a repository or a location of your choosing and sets it up while Packer creates the virtual machine fresh from an ISO each time.
Packer can easily be used to create your own Vagrant images.
Packer scripts are written in JSON format as below given example:-
In each script there are four major blocks
VARIABLES:- This lets you parameterize you packer template and stores all the import variables such as region , AWS secrets , etc.
BUILDERS:- Within the template the builders section contains an array of all the builders that Packer should use to generate machine images for the template. Builders are responsible for creating machines and generating images from them for various platforms.
PROVISIONERS:- Within the template, the provisioners section contains an array of all the provisioners that Packer should use to install and configure software within running machines prior to turning them into machine images. Provisioners are optional. If no provisioners are defined within a template, then no software other than the defaults will be installed within the resulting machine images .
POST-PROCESSORS:- The post-processor section within a template configures any post-processing that will be done to images built by the builders. Examples of post-processing would be compressing files, uploading artifacts, etc. Post-processors are optional. If no post-processors are defined within a template, then no post-processing will be done to the image. The resulting artifact of a build is just the image outputted by the builder.
Create a JSON file as per the infrastructure create your own builder block as below given example and name it as quickstart.json
Now in provisioners block pass the provisioner type and the bootstrap script for the required software installation.
Now in order to validate the script to check whether the syntax and the variables used are correct or not Packer has a functionality of validating the script before build
This command will check for the syntactical and builders error in the script before build .
And then you can go on with the build.
NOTE:- PACKER CREATES ITS OWN SECURITY GROUP , AUTHENTICATION KEY AND INSTANCE IN AWS ENVIRONMENT , MAKE SURE THAT THE IAM ROLE IS CONFIGURED IN SUCH A WAY THAT IT DOES NOT CREATE ANY PROBLEM WITH PACKER OPERATIONS.
For more detailed usage visit