Googlecompute Creating Instance timeout

I have inherited a project with no documentation that I’m trying to get working. Unfortunately I know nothing about any of the technologies involved – Packer, Ansible, or even much about GCP – so I’m trying to piece things together. The project was written in the older json template style so I have things converted and I’m trying to run it.

I’m stuck on an instance creation timeout and I’m not sure how to get any info to track down why this is happening. The log is

googlecompute.autogenerated_1: output will be in this color.

==> googlecompute.autogenerated_1: Checking image does not exist...
==> googlecompute.autogenerated_1: Creating temporary RSA SSH key for instance...
==> googlecompute.autogenerated_1: Using image: debian-10-buster-v20201112
==> googlecompute.autogenerated_1: Creating instance...
    googlecompute.autogenerated_1: Loading zone: us-east1-d
    googlecompute.autogenerated_1: Loading machine type: n1-standard-2
    googlecompute.autogenerated_1: Requesting instance creation...
    googlecompute.autogenerated_1: Waiting for creation operation to complete...
==> googlecompute.autogenerated_1: Error creating instance: time out while waiting for instance to create
Build 'googlecompute.autogenerated_1' errored after 5 minutes 3 seconds: Error creating instance: time out while waiting for instance to create

If it’s helpful, the file I’m running is

# This file was autogenerated by the 'packer hcl2_upgrade' command. We
# recommend double checking that everything is correct before going forward. We
# also recommend treating this file as disposable. The HCL2 blocks in this
# file can be moved to other files. For example, the variable blocks could be
# moved to their own 'variables.pkr.hcl' file, etc. Those files need to be
# suffixed with '.pkr.hcl' to be visible to Packer. To use multiple files at
# once they also need to be in the same folder. 'packer inspect folder/'
# will describe to you what is in that folder.

# Avoid mixing go templating calls ( for example ```{{ upper(`string`) }}``` )
# and HCL2 calls (for example '${ var.string_value_example }' ). They won't be
# executed together and the outcome will be unknown.

# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }

# source blocks are generated from your builders; a source can be referenced in
# build blocks. A build block runs provisioner and post-processors on a
# source. Read the documentation for source blocks here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/source
source "googlecompute" "autogenerated_1" {
  disk_size       = "20"
  disk_type       = "pd-ssd"
  image_name      = "linux-build-agent-${local.timestamp}"
  machine_type    = "n1-standard-2"
  network         = "{network}"
  project_id      = "{project_id}"
  source_image    = "debian-10-buster-v20201112"
  ssh_username    = "packer"
  subnetwork      = "us-east1-universal"
  tags            = ["tmp-buildkite-linux"]
  use_internal_ip = false
  zone            = "us-east1-d"
}

# a build block invokes sources and runs provisioning steps on them. The
# documentation for build blocks can be found here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/build
build {
  sources = ["source.googlecompute.autogenerated_1"]

  provisioner "ansible" {
    extra_arguments = ["--vault-password-file=scripts/vault-password.sh"]
    playbook_file   = "ansible/linux-build-agent.yml"
    use_proxy       = false
  }

}

The only thing I found that looked similar was

If anyone has any suggestions on how to track down why the timeout is occurring, thanks much.

OK, if anyone else hits this, I found the solution here: google cloud platform - googlecompute Error creating instance: time out while waiting for instance to create - Stack Overflow

Which amounts to adding the permissions for the Compute Engine Instance Admin (v1) and Service Account User roles to your service account.