Build A GCE (Google Compute Engine) VM image using Packer

- By Manisha Sahasrabudhe on June 21, 2018

This tutorial explains how to manually create a GCE VM image using Packer. Before you start, you should be familiar with the following concepts:

The best way to get started is to install Packer and run scripts manually on your local machine to create a VM image. Once you understand the mechanics of it, you should consider automating your workflow by following our documentation on Automated creation of GCE image using Packer.

 

Step-by-step instructions

Follow the steps below in order to build the VM image starting from a vanilla Ubuntu 16.04 image.

 

Step 1: Prep your machine

 

Step 1: Prep Packer template

  • Create a Packer template for your image. Your template will look something like this,

Packer Template

{
  "variables": {
    "source_image_family": "var",
    "machine_type": "var",
    "region": "var",
    "zone": "var",
    "project_id": "var"
  },
  "builders": [
    {
      "type": "googlecompute",
      "project_id": "",
      "machine_type": "",
      "source_image_family": "",
      "region": "",
      "zone": "",
      "image_description": "Demo Ubuntu Machine Image",
      "image_name": "u16demo-",
      "disk_size": 16,
      "disk_type": "pd-ssd",
      "ssh_username": "root"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 10",
        "uname -a",
        "uptime"
      ]
    },
    {
      "type": "shell",
      "script": "init.sh",
      "execute_command": "echo 'packer' | sudo -S sh -c ' '"
    }
  ]
}

  • Execute the following command to validate the template. Be sure to replace the project_id with your project
packer validate -var region="us-west1" -var source_image_family="ubuntu-1604-lts" -var machine_type="n1-standard-1" -var zone="us-west1-b" -var project_id=$PROJECT_ID packer.json` 

 

Step 3: Run Packer template

  • Execute the following command to build the image. Be sure to replace the project_id with your project
packer build -var region="us-west1" -var source_image_family="ubuntu-1604-lts" -var machine_type="n1-standard-1" -var zone="us-west1-b" -var project_id=$PROJECT_ID packer.json` 

 

Challenges with manually executing Packer templates

If you build machine images fairly often, you'll face a few challenges with manual execution of Packer templates:

  • Unless you want to create a Packer JSON for every single type of image you want to build, you will likely choose a template approach with one Packer template where wildcards can be replaced to build the right images. However, making sure the wildcards are correctly replaced can be error prone. Automation is key in ensuring that the wildcards are contextually replaced at runtime.
  • Once you create a build image, you likely want to provision some instances on GCP using that image. Sometimes there is an entire dependency tree of workflows that need to be triggered if the image changes. Manually triggering these dependent workflows and keeping stakeholders aware of changes can get challenging.
  • The machine has to be prepped with the right version of the CLI. If multiple teams are creating images and they have a need to use different versions of the CLI, you will need different deployment machines for each team.

 

Automated AMI builds with Packer and Shippable

To show you how to automate the workflow described above, we have designed a step by step tutorial in our documentation:

Automated Workflow To Create GCP Images with Packer

 

Schedule a demo

If you want a live demo of the Shippable platform and watch this scenario in action, schedule a demo with us:

Schedule a demo

Topics: Google Cloud Platform, packer


Add comments below...