When building custom images for Google Cloud Platform using Hashicorp Packer, you either specify a fixed, or latest for the Google source image version. When specifying a fixed version, you run the risk of building with a deprecated image. If you use latest, your build may introduce changes unknowingly. As we adhere to the principle to keep everything under version control, we created a utility which ensures that your packer template is always referring to the latest, explicit version of an image.
When creating a Google cloud platform virtual machine image, you either specify the exact source_image
or you specify the source_image_family
. The source_image
pins your custom image to a specific version, whereas the source_image_family
refers to the latest image version.
This utility will update the source_image
in the Packer template to match the latest version of the source_image_family
. In this way, the build process becomes reliable. It will specify the exact version the image is built on and will update it when a new version is available.
How does it work?
It is pretty simple. You just need a packer template as show below:
{
"builders": [
{
"project_id": "my-project",
"source_image_family": "ubuntu-1804-lts",
"type": "googlecompute"
}
]
}
To update it to the latest version, type:
$ gcp-hashicorp-packer-updater -filename tests/source-image-family.json
2021/04/11 11:31:23 updating image from '' to 'ubuntu-1804-bionic-v20210325'
2021/04/11 11:31:23 setting source image project to 'ubuntu-os-cloud'
The resulting Packer template looks as follows:
{
"builders": [
{
"project_id": "my-project",
"source_image": "ubuntu-1804-bionic-v20210325",
"source_image_family": "ubuntu-1804-lts",
"source_image_project_id": "ubuntu-os-cloud",
"type": "googlecompute"
}
]
}
As you can see, the version is updated and both the source image family and source image project are also added to the packer template. If your Packer template only has a source_image
it works in the same way.
installation
To install the utility, either type:
$ go get github.com/binxio/gcp-hashicorp-packer-updater
Or download a pre-compiled binary from github.com/binxio.
conclusion
With this simple utility, you can keep the google source image version in your Packer template tied to a specific version and you can keep it up-to-date. The utility only works with JSON files, as these can be read and updated by a program. Once there is a HCL2 read/write library is available, I will update the utility to support that too.
Image by Vinson Tan ( 楊 祖 武 ) from Pixabay