AWS CloudDevOps

How to Automate AMI Creation using Packer

In this tutorial, we explain how to automate AMI creation using Packer. Packer is a DevOps tool for creating machine images.

Packer is a very popular Open Source Tool which supports multiple platforms from a single source of Configuration. Packer tool is used to automate the creation of machine images for on-premise and Cloud. If you are looking for Image creation process then Packer is a best and open source tool.

How to Automate AMI Creation using Packer

In this article, we will show you how to install packer and Image creation for AWS Cloud.

Steps to Install Packer on a workstation or on Cloud

It is very easy to install packer on on-premise or on cloud machine. So here we will show you how to do in AWS. Take access to AWS using access keys.

  1. You need to download the package from
  2. Unzip the downloaded package and also you need to set the path in ~/.bashrc
  1. Exit/close the terminal or reconnect to AWS CLI or you can refresh the terminal with below command
  1. Verify packer

Steps to Build Image using Packer Tool

Packer comes with some configuration templates which are written in JSON format. Configuration templates contain follow parameters:

Variables: here you can define the custom variables

Builders: define all required AMI parameters

Provisioners: integrate a shell script, chef cookbook or ansible play for configuring required applications in the AWS AMI.

Sample template for AWS AMI:

In this above sample template configuration, we using AWS access keys and secret keys as variables. As per best practice, we should not pass keys in template better you use IAM roles to EC2 instance or configure AWS keys in ~/.aws/credentials file.

In the above template, we have used the script for provision. Packer tool supports some of the provisioners like Chef, Salt, Ansible, Powershell, Shell, Windows cmd, file… etc.

Steps to Build Packer Template

To build packer template you need to follow build command with json template.

Variables in Templates

Let’s see one example for variables in templates

As mention in the example, you can declare variables and can be accessed in other parts of the template.

Packer allows you to use the system environment variables. So that you can use it in other parts of the template.

After the declaration, you can use the script_path variable in the provisioner as shown below.

How to use Command Line Variables

Declare the name of the variable in the variable section.

For example:

How to use a JSON File

Packer allows you to use the JSON file with build option, as mention in the example.

variables.json is the variable fileHow to Automate AMI Creation using Packer

tomcaat.json is the packer template.

How to Pack an Image?

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

Let’s take an example, we will create image AWS AMI with t2.micro instance type using shell provisioner. Shell Script to update and install httpd package instruction.

Here we are going to user N.California region and a Fedora AMI with AMI id ami-2e34vbg0g

Step 1. Create a folder

Step 2. Create a script file named and copy the following text to it.

This script “” update and install httpd.

Step 3. Create an httpd.jsonfile with the following contents.

Step 4: Validate ad inspect template which is created “http.json”

Note: If you are using command variables or a file as a variable, you should pass it while validating it.

Step 5. Command to build your first new AMI using packer run command

This will build new AMI

Command to take the output of image build to a file:

This is the end of packer tutorial in which we explain how to take create an image using Packer Tool.

If you have any suggestion or query write us in below comment box.


Reference :

Install AWS CLI and Configure

Related Articles

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Adblock Detected

Please consider supporting us by disabling your ad blocker
%d bloggers like this: