How to Install Packer for Creating Identical Machine Images

May 21, 2015 | By
| Reply More

Hi everyone, today we'll learn about installing Packer and how we can create identical Images using it. Packer is an open source tool developed and maintained by Hashicorp for creating identical machine images for multiple platforms from a single source configuration. Packer is a lightweight software that can run on every major operating system with great performance. It is easy to use and automates the creation of any type of machine image. It doesn't replaces framework such as Chef and Puppet but it can be used to install and configure the software within the Packer-made images.Packer has the ability to create machine images for any platform but currently packer ships with support for a set of platforms. You can extend it through plugins to support any platform. Currently, Packer is shipped with the support for Amazon EC2 (AMI), DigitalOcean, Docker, Google Compute Engine, OpenStack, Parallels (PVM), QEMU, VirtualBox (OVF), VMware (VMX).

Here are the steps below how we can install Packer in our Linux Operating System.

1. Downloading Packer

We can easily download Packer from its Official Download Page. To download the zipped packer 0.7.5 using wget, simply run the following command in a shell or a terminal.

# mkdir ~/packer
# cd ~/packer

Creating Packer Directory

# wget https://dl.bintray.com/mitchellh/packer/packer_0.7.5_linux_amd64.zip

Downloading Packer Tool

2. Extracting Packer

Now, we'll extract the zipped packer we have just downloaded using unzip command as shown bellow.

If you don't have unzip installed, you can install it using your package manager. Here, we are running Ubuntu Server so, we'll use apt manager to install it.

# apt-get install unzip

Installing Unzip

# unzip packer_0.7.5_linux_amd64.zip

Extracting Packer Tool

3. Setting PATH

Now, after extracting Packer, we'll want to install it in our system. Inside the package, there are the binary files like packer, packer-builder-docker, packer-builder-digitalocean, etc available. So, to make them usable, we'll need to set PATH variables. We'll need to add the following lines inside ~/.profile if we want the packer be available for the current user as shown below.

# nano ~/.profile

Now, we'll need to add the below lines into the file.

export PATH=$PATH:~/packer/

Editiing Profile

Now, we'll need to re-login to apply changes to the system.

If we want Packer to be available throughout the system, we'll want to run the below commands.

sudo cp -r ~/packer/* /usr/bin/

4. Verifying the Installation

If everything went fine accordingly, we'll get the result as shown below if we enter command packer inside a shell or a terminal.

# packer

5. Creating Templates

After we have successfully installed Packer into our Linux Operating System, we would definitely give it a try and wanna build an image with it. First before we start, we'll need to know how to create a Template for Packer. So, templates are the configuration files which defines where and what images we wanna build. These configuration files is a simple easy to configure JSON format templates.

For example, here are some simple packer template file based on Amazon EC2 (AMI) and DigitalOcean. We can use our favorite text editor to create the template. Here, this example of template for Amazon EC2 is named as example-ami.json and for Digital Ocean is named as example-digitalocean-json .

For Amazon EC2 (AMI):

# nano example-ami.json

{
"variables": {
"aws_access_key": "",
"aws_secret_key": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-9eaa1cf6",
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example {{timestamp}}"
}]
}

For Digital Ocean:

# nano example-digitalocean.json

{

"variables": {
"do_api_token": ""
},
"builders": [
{
"type": "digitalocean",
"api_token": "{{user `do_api_token`}}"
}]
}

Note: The above is just a simple template where you are required to fill the contents to make it workable. When building, you'll pass in the aws_access_key, aws_secret_key, digitalocean_client_id and digitalocean_api_key as a user variable, keeping your secret keys out of the template. You'll need to create the security credential from the respective platform account.

6. Building the Images

After we successfully create the Packer Template, we'll go for validating the template and building the image defined by the template. So, to validate the template where its correctly configured or not, we'll use the command packer validate. To do so, we'll need to run the following command in the terminal or shell.

# packer validate example.json

Now, after we get our template validated. We'll build the image using that template.

For Amazon EC2, we'll need to replace YOU_ACCESS_KEY and YOUR_SECRET_KEY with the access key and secret key generated in Amazon EC2 Account.

# packer build \
-var 'aws_access_key=YOUR_ACCESS_KEY' \
-var 'aws_secret_key=YOUR_SECRET_KEY' \
example-ami.json

For Digital Ocean, we'll need to replace YOU_ACCESS_KEY and YOUR_SECRET_KEY with the access key and secret key generated in Digital Ocean Account.

# packer build \
-var 'do_api_token=YOUR_API_KEY' \
example-digitalocean.json

7. Configuring Provisioners

We have already learned above how we can create template and use that template to build images out of existing base images in the respective platforms. Now, we'll go for the real use of Packer ie it can be used for installing and configuring software packages into the images as well. To do so, we'll need to add some more configurations into the JSON Packer template.

Here's an example of template with provisioners which will install the Apache Web Server.

{
"variables": ["..."],
"builders": ["..."],

"provisioners": [{
"type": "shell",
"inline": [
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y apache2"
]
}]
}

8. Building Parallely

We can even build images in multiple platforms with the single template using Packer. Packer can automatically build images and provision it according to our requirements. We can configure many platforms in parallel way. Here's a simple example of template to build images in both Digital Ocean and Amazon EC2 platforms and installs Apache Web Server.

{
"variables": {
"aws_access_key": "",
"aws_secret_key": "",
"do_api_token": "",
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami": "ami-de0d9eb7",
"instance_type": "t1.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example {{timestamp}}"
},
{
"type": "digitalocean",
"api_token": "{{user `do_api_token`}}",
"region": "sgp1"
}],
"provisioners": [{
"type": "shell",
"inline": [
"sleep 30",
"sudo apt-get update",
"sudo apt-get install -y apache2"
]
}]
}

Conclusion

Finally, we have successfully learned how to install and create images using a tool name Packer developed and maintained by Hashicorp. Packer is an amazing tool which uses the APIs of the respective Platforms. It is pretty easy to work with Packer. It is useful if you need to create multiple images in different platforms at once. Please note that packer is not built for managing or controlling the images. If you want to remote build and store the output of builds with Packer, you'll need to use Atlas by Hashicorp . So, if you have any questions, comments, feedback please do write on the comment box below and let us know what stuffs needs to be added or improved. Thank You! Enjoy Packer Tool by Hashicorp :-)

Filed Under : LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.