Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
google cloud - account.json file for packer

The problem

The document quality of Google Cloud is far away if compare with AWS. Here is a sample.

If you met below error, when running packer or other SDK,

$ cat packer.json
{
  "builders": [
    {
      "type": "googlecompute",
      "account_file": "account.json",
      "project_id": "qwiklabs-gcp-03-be761527bd29",
      "source_image": "debian-7-wheezy-v20150127",
      "ssh_username": "packer",
      "zone": "us-central1-a"
    }
  ]
}

Without account.json, you will get some errors.

error: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

or

Build 'googlecompute' errored: google: could not find default credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

As a new user of Google cloud, I have no idea about this account.json. So if you try to get help from the url provided above (https://developers.google.com/accounts/docs/application-default-credentials), you will totally lost.

Solution

gcloud auth application-default login

above command will generate a json file with full path

Credentials saved to file: [/Users/xxxx/.config/gcloud/application_default_credentials.json]

copy it to local, such as account.json which need be used by packer. Below is a sample of it

{
  "client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
  "client_secret": "d-FL95Q19q7MQmFpd7hHD0Xy",
  "refresh_token": "1//0g7OKxcamC964CgYIARAAGBASNwF-L9IrUWmjvmCGAiLIPveIbTHuli3sEVI8oKIUzVE7rZL1kB4hswsAiMDdSTrJWUC2G_9Q8aA",
  "type": "authorized_user"
}

You can run the packer command as normal, everything is fine now.

$ packer build packer.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: debian-7-wheezy-v20150127
==> googlecompute: Creating instance...
    googlecompute: Loading zone: us-central1-a
    googlecompute: Loading machine type: n1-standard-1
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
    googlecompute: Instance has been created!
==> googlecompute: Waiting for the instance to become running...
    googlecompute: IP: 104.197.21.145
==> googlecompute: Using ssh communicator to connect: 104.197.21.145
==> googlecompute: Waiting for SSH to become available...
==> googlecompute: Connected to SSH!
==> googlecompute: Deleting instance...
^@^@    googlecompute: Instance has been deleted!
==> googlecompute: Creating image...
==> googlecompute: Deleting disk...
    googlecompute: Disk has been deleted!
Build 'googlecompute' finished.

==> Builds finished. The artifacts of successful builds are:
--> googlecompute: A disk image was created: packer-1587448303

Reference

https://www.packer.io/docs/builders/googlecompute.html

https://stackoverflow.com/questions/41507904/could-not-find-default-credentials

The wrong direction

Why I can't get help from google search, because it always directs you with wrong document/wiki.

If you search key words google cloud packer, you first get this url, which makes you lost that why I need cloudbuild (google's cicd) to build a image

https://cloud.google.com/cloud-build/docs/building/build-vm-images-with-packer

The second url (https://cloud.google.com/community/tutorials/create-cloud-build-image-factory-using-packer) also makes the simple job too complex, and I don't know what it tries to teach.

Then I am back to packer homepage (packer.io), I am surprise, hashicorp packer doesn't give any explain for this file account.json as well.

https://www.packer.io/docs/builders/googlecompute.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment