Automation with HashiCorp Packer

·

4 min read

Introduction:-

Generally, when you want to launch an instance you will be using AWS ec2 launch instance console and use the default AMI

Instead of using these standard, default AMIs, In an organization, you would need an AMI that has certain configurations done based on application needs. Such an image which we use for testing, QA, dev and production we call it Golden Image

So we need to ensure Golden Image with the latest configurations and certain standard, security standards to be followed is available to build our applications on top of it

How to Build AMI

There will be many environments and many projects, hence many AMI's. So we need to have many AMIs with the latest and updated configuration which is why there's a need to automate this. Hence we have a tool called PACKER which can automate all these steps for you

Packer:-

Packer is an open-source tool for creating identical machine images for multiple platforms from a single source configuration*.*

Packer is an open-source DevOps tool made by Hashicorp to create identical machine images for multiple platforms from a single JSON config file

you can write the Instructions to the packer as JSON files they are called as Templates in Packer

Packer is lightweight, runs on every major operating system, and is highly performant, creating machine images for multiple platforms in parallel.

It can create Images for multiple platforms like Docker, AWS, Oracle Virtual Box, Digital Ocean, OpenStack, Linode, Azure etc.

Simply put, the Machine Images we create using Packer can be used by Terraform to build the infrastructures.

NOTE:-

AWS alternative to Packer:-

EC2 Image Builder is a service AWS offer that allows you to automate the process of building custom Amazon Machine Images (AMIs). Unlike Packer, it also handles the full pipeline for creating, testing and even distributing the machine images.

Why Packer:-

How Packer works:-

Variables:

There is a dedicated section for variables and we will start from there. Variables - This Section would contain the list of variables you need to use or need across other sections on the Packer Template JSON file.

It is always a Good practice to use variables and referring the variable name across the builders and provisioners as using a variable would make your Packer Template easy to understand and to modify/update in future for new requirements.

Builders:-

Here we define what image we are going to create and for which technology/platform we are going to create an image for like AWS, DOCKER, VirtualBox, OpenStack etc.

Based on the Builders the configuration elements we use in this section would vary. Packer supports various builders like EC2, Vmware, Virtual box etc.. and it can be expanded to various other new products and technologies.

Provisioners:-

These are the list of built-in or external configuration on management tools like Shell, Ansible, Chef, PowerShell etc..

Here we define what must the image be build with, like a software product, Certain Usernames, Directories, Patch Version, Kernal Version, Environmental specific configuration etc.

Provisioners let you design your image and make it more meaningful by adding a necessary software/programs to it, Without it, the image we create is a simple OS kernel base image otherwise Some Use Cases of Provisioners

  • Installing Packages/Softwares

  • Creating a username and Configuration

  • Run Some Custom Scripts to make Environment required changes

  • Configure File System / NFS / Storage etc

  • Security Setup / Configuration

  • downloading necessary code files etc.

  • patching the kernel

  • Configuring the Packages/Softwares the way you want ( Apache/NGINX/Tomcat/mysql) etc

Basically, provisioners are doing the main job here and making the image as ready to use

Post Processors:-

Post Processors (Optional): Post-processors comes into the picture after the image is Successfully built or created by the builder and provisioned by the provisioner(s).

Post-processors are optional, and they can be used to upload artefacts, re-package, create tags, Publish to clouds like hub.docker.com and vagrant cloud etc Some of the Popular Post processors are

  • Docker Push

  • Docker Tag

  • Vagrant

  • Vagrant cloud

  • Vsphere Template

  • Amazon Import

  • DigitalOcean import etc

Setup Packer on Ubuntu server :-

Packer Commands:-

Using Packer:-

These are the steps we are going to perform to create a packer image that install apache httpd web server using a shell provisioner and prepare the image.

Once the image is ready we are going to use Terraform to create an instance based on the AMI or the image we have created using Packer.

setup.sh

echo "Hello Sarav, How are you doing"

echo "Installing Apache2"

apt-get update

apt-get -y install apache2

A sample template file for Packer:

{
    "variables": {
        "aws_access_key": "{{env `AWS_ACCESS_KEY_ID`}}",
        "aws_secret_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
        "region":         "us-east-1"
    },
    "builders": [
        {
            "access_key": "{{user `aws_access_key`}}",
            "ami_name": "packer-linux-aws-demo-{{timestamp}}",
            "instance_type": "t3.micro",
            "region": "ap-south-1",
            "secret_key": "{{user `aws_secret_key`}}",
            "source_ami_filter": {
              "filters": {
              "virtualization-type": "hvm",
              "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
              "root-device-type": "ebs"
              },
              "owners": ["099720109477"],
              "most_recent": true
            },
            "ssh_username": "ubuntu",
            "type": "amazon-ebs"
        }
    ],
    "provisioners": [
        {
            "type": "file",
            "source": "./welcomefile",
            "destination": "/home/ubuntu/"
        },
        {
            "type": "shell",
            "inline":[
                "ls -al /home/ubuntu",
                "cat /home/ubuntu/welcomefile"
            ]
        },
        {
            "type": "shell",
            "script": "./setup.sh"
        }
    ]
}

packer validate filename.json

Build an Image using Packer from the Template JSON file

packer build filename.json

The build is completed and AMI is generated