HashiCorp Packer is a great tool for building virtual machine images for a variety of platforms including Google Cloud. Normally Packer starts an GCE instance, builds the machine image on it and terminates the instance on completion. However sometimes the process is aborted and the instance is left running, racking up useless cloud spend. In this blog I present a utility to get delete lingering Packer instances.
The gcp-hashicorp-packer-reaper utility stops or terminates all Packer Builder GCE instances. You use it from the command line utility or install it as Cloud Run service so that old instances are automatically reaped.
install the reaper
to install the Packer reaper, type:
$ pip install gcp-hashicorp-packer-reaper
The utility allows you to list, stop or terminate any GCE instance which name starts with
packer- and the description is
New instance created by Packer There is no other validation that the instance is indeed a machine started by packer. The name is pretty distinctive, but if you start instances with the same name prefix and description, they will be selected too.
show packer instances
To show running packer instances:
$ gcp-hashicorp-packer-reaper list
packer-5e99d4f2-b5a5-e9e0-b763-cd5102ae7e73 launched 7 minutes ago in your-project - europe-west4-c - RUNNING
INFO: 1 packer builder instances found
stop packer instances
To stop running packer instances older than 2 hours:
$ gcp-hashicorp-packer-reaper stop --older-than 2h
INFO: stopping packer-5e99d4f2-b5a5-e9e0-b763-cd5102ae7e73 in your-project created 3 hours ago
INFO: total of 1 running instances stopped
delete packer instances
To terminate stopped and running packer instances older than 24 hours:
gcp-hashicorp-packer-reaper --verbose delete --older-than 24h
INFO: deleting packer-5e99d4f2-b5a5-e9e0-b763-cd5102ae7e73 in your-project created 2 days ago
INFO: total of 1 instances deleted
deploy the packer reaper
To deploy the packer reaper as a service in your project, type:
git clone https://github.com/binxio/gcp-hashicorp-packer-reaper.git
PROJECT=$(gcloud config get-value project)
make USERNAME=$PROJECT snapshot
terraform apply -var project=$PROJECT -auto-approve
This will install the packer reaper in your GCP project and run every 10 minutes, and delete instances older than 2 hours. You can change the schedule and the action to meet your requirements.
The gcp-hashicorp-packer-reaper utility stops or deletes all virtual machines started by Packer. I recommend to deploy the Cloud Run service in your GCP account, so that you will never pay for lingering packer instances.
Do you have this problem on AWS? Read my blog on How to terminate lingering packer instances on AWS