Deploying Packer in GCP to build Windows Images using Cloud Scheduler and Cloud Build with Terraform

When recently working on a customer environment I was looking at ways to improve the startup time of their Windows Application servers. Their sophisticated Powershell startup script was taking about 15–20 minutes to execute fully and involved installing IIS, Azure DevOps & Stackdriver agents and the specialist application software. It was fairly clear to me that time could be saved by packing at least some of these elements into a base image. This then led me to consider deploying Packer on GCP. In this blog post I hope to discuss some of the steps taken to deploy this and how it hangs together.

Step 1: Deploy Packer Builder into GCR

I felt the obvious choice was to run Packer within Cloud Build using a Trigger defined in the Terraform. To make life even easier some kind people in the Cloud Builders Community have already packaged a Packer builder so the first step is simply to push this to the project GCR so Cloud Build can use it. After cloning down that repo change into the packer directory, then simply run:

Note: If you have any specific requirements for a certain GCR location, like this customer did, modify the cloudbuild.yaml file for the builder. The subsequent code references eu.gcr.io for this reason.

Step 2: Writing the Cloud Build Packer job YAML File

So the next step is to define the Cloud Build Packer job using YAML, there is an example provided in the Cloud Builders repo for inspiration but here is an anonymized copy of my resultant file:

In the above example I specified the timeout as 30 minutes as the GCP default (10 minutes) isn’t enough time for this job. The ${_ENV} value is a substitution that is passed into the Cloud Build job. In the example above this is to allow the same code to be run in multiple projects. Finally the packer/app.json is the location of the JSON file that Packer itself reads.

Step 3: Writing the Packer JSON File

Whilst I appreciate that as of v1.5 Packer can read HCL2, most documentation and examples are still in JSON (including the one I pinched and modified) so I opted to keep it this way:

Note that in the JSON I am parsing the powershell script and specifying a network tag of “packer-winrm”. The network tag is then used to target a specific WinRM firewall rule during the build process. The Terraform firewall code looks like this:

Note: I can’t specify the source_ranges as Google doesn’t publish ranges for Cloud Build.

Step 4: Writing the Powershell

This step is only applies to Windows but I wanted to mention the requirement to pass a no_shutdown argument to the GCESysprep wrapper to prevent sysprep from restarting the image. Without this argument Packer fails the build as it assumes something has gone wrong.

Note: I found the GCESysprep documentation to be a little sparse so I would recommend reviewing the code in the Github Repo. This particular issue I found helped a great deal in finding a working solution.

Step 5: Writing Cloud Build Triggers

Normally with Cloud Build you are triggering off a PR or Push to a specific branch of a git repo. However in this case I don’t want to build an image every single time someone pushes to the repo. One solution to this would be to separate Packer into its own repo but in this case it was preferable to keep it in the repo with the infrastructure code. Therefore I created the trigger in a disabled state so it can only be manually triggered:

Note: You can also see above where I make the _ENV substitution based on a Terraform variable.

Step 6: Triggering Cloud Build with Cloud Scheduler

Before I begin this section I want to shout out Neil Kolban’s excellent post about Scheduling Cloud Builds which inspired this section. I won’t go over what he’s already covered but I will provide Terraform code for the Cloud Scheduler job:

Step 7: Enjoy Cloud Scheduled Packer Builds :)

Now the Cloud Scheduler job will trigger Cloud Build which will go and package a Windows image using Packer before adding it to an image family from which it can be deployed.

I am a GCP Platform Engineer based in the UK. Thoughts here are my own and don’t necessarily represent my employer.