Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Build a GCE VM Image Using Packer

DZone's Guide to

Build a GCE VM Image Using Packer

Here's a quick and easy guide to building your own Google Cloud VM image with a helping hand from Packer.

· Cloud Zone ·
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

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 ' '"
    }
  ]
}
  • If you do not have your own Packer templates, please feel free to clone our sample repository here: https://github.com/devops-recipes/gcp_image_with_packer, which contains a packer.json. You can replace all the variables with values or supply it during runtime.
  • Authenticate the newly installed gcloud to GCP. You do this with the following command gcloud auth login. [Refer to the GCP docs]
  • 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.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
cloud ,gce ,packer ,vm image ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}