Intro: Hashicorp `Packer`
Tags: automation, hashicorp, packer, devops
Published: 2019-07-30 23:28:34

---
title: Intro: Hashicorp `Packer`
published: true
description: An introduction to Hashicorps' Packer and what I learned in one day.
tags: automation, hashicorp, packer, devops
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2019/07/hashicorp_packer-930x620.png
---

<!-- wp:paragraph -->
<p><strong>The problem space...</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In today's modern application development process an applications has many homes. Local development, (docker) containers, on premise Linux servers, maybe even Unix mainframes for production. Often an application is developed using a container then deployed to an cloud compute instance that is 'build like' the container. Then someone somewhere has to move the application to production and ensure it still works correctly. Boiling this problems down to the core we see it is a problem of creating machine images that match desired state. Docker container? machine image. On premise Linux integration server? machine image. Production cloud host? again, machine image. So exactly how are we suppose to create matching machine images over such a wide range of under lying systems?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Hashi-who pack-what-?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://www.hashicorp.com/">Hashicorp</a> is a development and management tool publisher. Most famous there Infrastructure as Code (IaC) tool <a href="https://www.hashicorp.com/products/terraform/">Terraform</a>. <a href="https://www.packer.io/docs/index.html">Packer</a> is one of the tools available from them. Created specifically to ease the creation of machine images, Packer is super easy to learn and has a very low bearer of entry. Container, Linux Machines and even Virtual Box and VMWare images can all be created with Packer. In one solid day and you can publishing machine images like a pro!</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1761,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/07/brett-jordan-MFLNpz5FZRk-unsplash-1920x1440.jpg" alt="" class="wp-image-1761"/><figcaption>Image from Brett Jordan @ Unsplash: https://unsplash.com/@brett_jordan</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Learning Curve</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>As stated above the learning curve for <a href="https://www.packer.io/">Packer</a> takes one solid day. The <code>packer help</code> CLI command returns 6 options. 6! <code>build</code>, <code>console</code>, <code>fix</code>, <code>inspect</code>, <code>validate</code>, and <code>version</code>. Pretty self explanatory right?! Try that with any other CLI application and scrolling up and down becomes a way of life. Moving on to the configuration files, they are plan text JSON, not even HCL, just plain JSON. Inside the configuration files are what I like to call three 'top level concepts':</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://www.packer.io/docs/builders/index.html">Builders</a>: Who is building the machine image? Think of this similar to the <code>build</code> stage in a CI/CD pipeline; but much more versatile. AWS, Docker, GCP, 1&amp;1, OpenStack, Oracle, VMWare, and even custom builds are available. Checkout the <a href="https://www.packer.io/docs/builders/index.html">complete list to see over on the docs</a>.</li><li><a href="https://www.packer.io/docs/provisioners/index.html">Provisioners</a>: This part of the configuration determines what is used to install dependencies, update the core OS, create users, set file permissions, and other configuration processes INSIDE the image.</li><li><a href="https://www.packer.io/docs/post-processors/index.html">Post-Processors</a> (optional): When these are run after the machine image is created additional commands can be executed. Upload the image for storage to an artifact repository, re-package a a VM from VMWare to VirtualBox, run build time reports, and other post event actions.</li></ul>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>That is it; three main concepts, and one of them is optional!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Examples</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Build a local Docker image and push to image repository</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
```json
{
    "builders": [{
        "commit": true,
        "image": "ubuntu:16.04",
        "type": "docker"
    }],
    "provisioners": [
        {
            "type": "shell",
            "inline": [
                "apt-get update -y &amp;&amp; apt-get install -y python python-dev"
            ]
        }
    ],
    "post-processors": [
        {
            "repository": "example-ubuntu-16.04-updated",
            "tag": "latest",
            "type": "docker-tag"
        }
    ]
}
```
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In the above Packer configuration Packer pulls the Ubuntu 16.04 Docker image from Docker Hub via the <code>builders</code> section. Followed by the <code>shell</code> provisioner to update the system and install the Python; now it is ready for you Flask or Django app! Finally, the last part, the <code>post-processor</code> adds a tag to the image and pushes to your image repository.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1762,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/07/chuttersnap-xewrfLD8emE-unsplash.jpg" alt="" class="wp-image-1762"/><figcaption>Photo by <a href="https://unsplash.com/@chuttersnap?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">chuttersnap</a> on <a href="https://unsplash.com/search/photos/containers?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>B<em>uilding an AWS AMI, Updating the OS, and saving the image as an AMI.</em></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
```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_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*",
          "root-device-type": "ebs"
        },
        "owners": ["099720109477"],
        "most_recent": true
      },
      "instance_type": "t2.micro",
      "ssh_username": "ubuntu",
      "ami_name": "packer-aws-ami-{{timestamp}}"
    }],
    "provisioners": [{
        "type": "shell",
        "inline": [
            "sleep 30",
            "sudo apt-get update",
            "apt-get install mysql-server libmysqlclient-dev"
        ]
    }]
  }
```
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In the configuration above we use local AWS credentials to build an image based on Ubuntu 16.04. In the improvisers section the OS is updated and MySQL-server is installed using the <code>shell</code> provisioner. That is all. You now have a EC2 based MySQL server image.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is just two simple examples of what Packer can do. Imagine it as part of your CI/CD pipeline! It is even possible to build different images for different targets with the same provisioner execution on each, at the same time! <a href="https://www.packer.io/intro/getting-started/parallel-builds.html">Parallel builds</a> are an amazing advanced feature to look into as you dig into the full feature set.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Conclusion</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All told Packer fits a nice niche in the build process: creating the underlying machine image. From there a provisioner like Shell scripts, Ansible, or PowerShell pick up and execute custom application specific commands. A fast, easy to understand, amazingly simple (and REPEATABLE) way to configure those sweet sweet <code>golden images</code>. Now there is no reason for base images to be out of date an unpatched.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>So what do you think? Can we see what Packer fits into your daily build cycle, or even simplify an effort intensive process of creating images? Let me know your thoughts in the comments below.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Further Reading</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><a href="https://www.packer.io/">https://www.packer.io/</a></li><li><a href="https://www.packer.io/intro/index.html">https://www.packer.io/intro/index.html</a></li><li><a href="https://devopscube.com/packer-tutorial-for-beginners/">https://devopscube.com/packer-tutorial-for-beginners/</a></li><li><a href="https://blog.codeship.com/packer-vagrant-tutorial/">https://blog.codeship.com/packer-vagrant-tutorial/</a></li><li><a href="https://semaphoreci.com/community/tutorials/continuous-deployment-of-golden-images-with-packer-and-semaphore"><a href="https://semaphoreci.com/community/tutorials/continuous-deployment-of-golden-images-with-packer-and-semaphore">https://semaphoreci.com/community/tutorials/continuous-deployment-of-golden-images-with-packer-and-semaphore</a></a></li></ul>
<!-- /wp:list -->