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.

How to Automate AMI Creation using Packer

Packer is a very popular Open Source Tool which supports multiple platforms from a single source of Configuration. The Packer tool is used to automate the creation of machine images for on-premise and Cloud. If you are looking for an Image creation process then Packer is the 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 a packer on on-premise or on a cloud machine. So here we will show you how to do it in AWS. Take access to AWS using access keys.

  1. You need to download the package from www.packer.io/downloads.html
  2. Unzip the downloaded package and also you need to set the path in ~/.bashrc
$ vi ~/.bashrc

Export PATH=$PATH:/path/to/packer
  1. Exit/close the terminal or reconnect to AWS CLI or you can refresh the terminal with below command
$ source ~/.bashrc
  1. Verify packer
$ packer version

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:

{
  "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-west-1",
    "source_ami": "ami-uasb416s",
    "instance_type": "t2.micro",
    "ssh_username": "ec2-user",
    "ami_name": "packer-demo {{timestamp}}"
  }],


  "provisioners": [
  {
    "type": "shell",
    "script": "sysadmin-sysadmin-demo-script.sh"
  }

  ]
}

In this above sample template configuration, we using AWS access keys and secret keys as variables. As per best practice, we should not passkeys 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 a packer template you need to follow build command with the JSON template.

$ packer build tomcat.json

Variables in Templates

Let’s see one example for variables in templates

"variables": {
"instance_type": "t2.micro",
"region": "us-west-1"
}

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

"instance_type": "{{user `instance_type`}}",
"region": "{{user `region`}}"

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

"variables": {
"script_path": "{{env `SCRIPT_PATH`}}",
}

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

"provisioners": [
{
  "type": "shell",
  "script": "{{user `script_path` }}/sysadmin-demo-script.sh"
}
]

How to use Command Line Variables

Declare the name of the variable in the variable section.

"app_name": "{{app_name_cmd_var}}"

For example:

$ packer build -var 'app_name_cmd_var=apache' tomcat.json

How to use a JSON File

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

$ packer build -var-file=variables.json tomcat.json

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

$ mkdir packer

Step 2. Create a script file named sysadmin-sysadmin-demo-script.sh and copy the following text to it.

$ vi sysadmin-demo-script.sh

#!/bin/bash

sudo yum -y update

sudo yum install -y httpd

This script “sysadmin-demo-script.sh” update and install httpd.

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

{

"variables": {

  "ami_id": "ami-2e34vbg0g",

  "app_name": "httpd"

},


"builders": [{

  "type": "amazon-ebs",

  "region": "eu-west-1",

  "source_ami": "{{user `ami_id`}}",

  "instance_type": "t2.micro",

  "ssh_username": "ec2-user",

  "ami_name": "PACKER-DEMO-{{user `app_name` }}",

  "tags": {

      "Name": "PACKER-DEMO-{{user `app_name` }}",

      "Env": "DEMO"

    }
}],


"provisioners": [

  {

    "type": "shell",

    "script": "sysadmin-demo-script.sh"

  }
]
}

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

$ packer validate httpd.json

$ packer inspect httpd.json
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

$ packer build httpd.json

This will build new AMI

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

$ packer build httpd.json 2>&1 | sudo tee output.txt

This is the end of the packer tutorial in which we explain How to Automate AMI Creation using Packer tool.

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


Read other articles:

When you can plan for VPC Peering

What is AWS Virtual Private Cloud

What are Amazon EC2 Container Service and its features

Install AWS CLI and Configure

Submit your article