Packer Tutorial For Beginners – Automate AMI Creation

Packer is a VM image creation tool. It lets you automate the process of image creation on various on-premise and cloud solutions. If you are setting up an immutable infrastructure model using VM’s, packer is a right fit for you.

Packer Tutorial For Beginners

In this beginner tutorial, we have covered the steps required to get started with packaging AMI’s on AWS cloud.

Installing Packer on Workstation

You can have the packer installation on your local workstation or on a cloud instance. All you need is the cli connectivity to AWS using access keys.

Note: If you are planning to setup Packer on the local workstation, make sure you have the aws access keys set on file ~/.aws/credentials. It is not mandatory to have credentials file. You can pass the AWS credentials using the packer variable declaration.

1. Download the required package from here. https://www.packer.io/downloads.html

2. Unzip the package and set the path variable in ~/.bashrc

3. Refresh terminal

4. Verify packer installation by executing the packer command.

Building Image

Packer configuration templates are written in JSON format.

A template has the following three main parts.

1. variables – Where you define custom variables.

2. builders – Where you mention all the required AMI parameters.

3. provisioners – Where you can integrate a shell script, ansible play or a chef cookbook for configuring a required application in the AMI.

An example template for packaging an AWS AMI is given below.

In the above example configuration, we are passing the AWS access keys and secret keys as variables. It is not recommended to use access keys as variables. If you have already set the credentials in ~/.aws/credentials file, you don’t need to pass access keys as variables.

Also, we have used shell provisioner which calls a demo-script.sh file.

packer supports the following provisioners.

  1. Ansible
  2. Chef
  3. Salt
  4. Shell
  5. Powershell
  6. Windows cmd
  7. File – For copying file from local directory to VM image.
READ  How to Migrate Wordpress Site to Digital Ocean Cloud Server

Building a Packer Template

To build a packer template, you just need to execute the build command with the JSON template.

For example,

Before jumping into building the template, let’s look ar some more important concepts and at last we can try out an example with shell provisioner.

Different Scenarios for Using Variables

You can make use for packer variables for dynamic configuration fo packaging AMI. Let’s discuss these scenarios one by one.

Using Variable Within Template

The variables block holds all the default variables within a template. Asn example is shown below.

The declared variables can be accessed in other parts of the template using "{{user your-variable-name}}" syntax. An example is shown below.

Using Environment Variables in Templates

Packer lets you use the system environment variables. First, you need to declare the environment variables in the variable section to use it in the other parts of the template.

Lets say, you want to use the SCRIPT_PATH environment variable that holds the path to a shell script that has to be used in the shell provisioner. You can declare that variable like shown below.

After the declaration, you can use the script_pathvariable in the provisioner like shown below.

Using Command Line Variables

First, you need to declare the variable name in the variable block as shown below.

You can pass variables during the run time using -var flag followed by the variable declaration.

You can use this variable in the template using the normal interpolation we used in the above examples.

For example,

Using a JSON File

You can pass a JSON file with variables block to the build command as shown below.

In the above example, variables.json is the variable file and apache.json is the packer template.

Packaging an Image

In this example, we will bake a t2.micro AMI using a shell provisioner. A shell script which has an update and HTTPD install instruction.

We assume that you have the AWS access keys and region set in the ~/.aws/credentials file.

Here we are going to user Oregon region and a Redhat AMI with AMI id ami-6f68cf0f

Follow the steps given below to set up the project.

READ  Jenkins Automated Build Trigger On Github Pull Request

1. Create a folder call packer

2. Create a script file named demo-script.sh and copy the following contents on to it.

The above script just updates the repository and installs httpd.

3. Create a httpd.json file with the following contents.

We have our template ready. Next step is to execute the template to for packaging the AMI with HTTPD installation.

4. Lets validate and inspect our template using the following commands.

Note: If you are using command line variables or a file as a variable, you should pass it while validating it. An example is shown below.

The above command should validate and inspect without any errors.

5. To build our new AMI, use the following command.

The above command will build a new AMI.

You can also debug the image creation. Check out this thread for packer debugging.

Few Tips

1. You can capture the output of the image build to a file using the following command.

2. Then you can extract the new AMI id to a file named ami.txt using the following command.

In this packer tutorial for beginners, we covered the basics of image creation. Let us know in the comment section if you face any issues in the setup.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.