HashiCorp Packer is a powerful open-source tool that enables automated building of machine images, including Virtual Machine (VM) images for Compute Engine. Cloud Build is a hosted service by Google Cloud for continuous integration. In this exercise, you will set up a Cloud Build pipeline to bake VM images using Packer.

What you'll learn

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Screenshot from 2016-02-10 12:45:26.png

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

Start Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab you will be using Google Cloud Shell, a command line environment running in the Cloud.

From the GCP Console click the Cloud Shell icon on the top right toolbar:

It should only take a few moments to provision and connect to the environment. When it is finished, you should see something like this:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on Google Cloud, greatly enhancing network performance and authentication. All of your work in this lab can be done with simply a browser.

This tutorial will use a number of Google Cloud services. Before proceeding, you need to activate them. This only needs to be done once per project to make the API accessible.

$ gcloud services enable compute.googleapis.com \
    sourcerepo.googleapis.com \
    cloudbuild.googleapis.com \

As Cloud Build is triggered by git commit pushes, create a Cloud Source Repository to store the code for this codelab.

$ gcloud source repos create packer-image-build

Then clone the repository out into Cloud Shell and enter it.

$ gcloud source repos clone packer-image-build && cd packer-image-build

To use Packer from Google Cloud Build, you need to build an image with Packer installed. Thankfully, one already exists for you to use - you just need to add it to your project.

Clone the cloud-builders-community repo:

$ git clone https://github.com/GoogleCloudPlatform/cloud-builders-community

Go to the directory that has the source code for the packer Docker image:

$ cd cloud-builders-community/packer

Build the Docker image via Cloud Build:

$ gcloud builds submit --config cloudbuild.yaml .

Remove this temporary directory:

$ cd ../.. && rm -rf cloud-builders-community

To run Packer builds, you need a packer.json file which defines the image properties and provisioning steps to take.

Download the sample Packer configuration from GitHub:

$ curl -sSfO https://raw.githubusercontent.com/morgante/google-cloud-examples/codelab-packer/codelabs/packer-cloud-build/packer.json

This file specifies to build a Debian image in the us-central1 region and writes the Operating System info to a sysinfo.txt file.

Packer Configuration

  "variables": {
    "project_id": "{{env `PROJECT_ID`}}",
    "source_image_family": "debian-9",
    "machine_type": "n1-standard-1",
    "region": "us-central1",
    "zone": "us-central1-a"
  "builders": [
      "type": "googlecompute",
      "project_id": "{{user `project_id`}}",
      "machine_type": "{{user `machine_type`}}",
      "source_image_family": "{{user `source_image_family`}}",
      "image_description": "Sample Debian Image",
      "image_name": "debian-demo-",
      "disk_size": 16,
      "disk_type": "pd-ssd",
      "ssh_username": "root",
      "region": "{{user `region`}}",
      "zone": "{{user `zone`}}"
  "provisioners": [
      "type": "shell",
      "inline": [
        "uname -a >> ~/sysinfo.txt"

Now that you have a Packer image and configuration, you need to configure Google Cloud Build to run Packer.

Create Build Configuration YAML

Cloud Build bases its build steps on a cloudbuild.yaml file in your repository. Download the sample yaml from GitHub:

$ curl -sSfO https://raw.githubusercontent.com/morgante/google-cloud-examples/codelab-packer/codelabs/packer-cloud-build/cloudbuild.yaml

Cloud Build Configuration

- name: 'gcr.io/$PROJECT_ID/packer'
  - build
  - packer.json

Give Cloud Build Editor Access

In order to build images, Cloud Build needs permission to access the Compute Engine API in your project.

Retrieve the Cloud Build Service Account's email:

$ CLOUD_BUILD_SA=$(gcloud projects get-iam-policy $GOOGLE_CLOUD_PROJECT --flatten=bindings --filter="bindings.role:roles/cloudbuild.builds.builder" --format="value(bindings.members[])")

Grant it access to your project:

$ gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member $CLOUD_BUILD_SA \
    --role roles/editor

Set up Cloud Build Triggers

You now need to configure Cloud Build to automatically trigger builds every time your repository is pushed to.

  1. Click on the Build Triggers page under Cloud Build menu in the Google Cloud Console
  2. Click "Create Trigger"
  3. Under "Repository," select "packer-image-build" and click "Continue"
  4. Enter a name for this trigger (for example: "packer-image-build")
  5. Set the build configuration to "Cloud Build configuration file (yaml or json)"
  6. Click "Create trigger"

Now that you have configured Cloud Build and Packer, you just need to push your code and trigger your first build.

Before you can push, you need to configure Git. You can use your real information or fake information in this step:

$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"

Next, push the changes:

$ git add -A
$ git commit -a -m "Initial commit"
$ git push

This will trigger a Packer build, which you can view from the Build history page of the Cloud Console. Once the build is complete, you can see the image is now available in your project.

You learned how to use a module for HashiCorp Terraform to create a network on Google Cloud.

Clean up

If you are done exploring, please consider deleting your project.

Learn More


This work is licensed under a Creative Commons Attribution 2.0 Generic License.