Search code examples
google-cloud-platformgoogle-compute-enginegcloudpackergoogle-iam

Cannot create image with packer in Google Cloud due to service account error


  • I have created a service account in Gcloud.
  • Installed Gcloud on my mac.

When ever I run my packer template, it complains about this account which I have no idea where it is coming from.

Packer template:

{
  "builders": [
    {
      "type": "googlecompute",
      "account_file": "/Users/Joe/Downloads/account.json",
      "project_id": "rare-truck-123456",
      "source_image": "centos-7-v20180129",
      "zone": "us-west1-a",
      "ssh_username": "centos"
    }
  ]
}

Error:

➜  packer git:(master) ✗ packer build release_google_image.json
googlecompute output will be in this color.

==> googlecompute: Checking image does not exist...
==> googlecompute: Creating temporary SSH key for instance...
==> googlecompute: Using image: centos-7-v20180129
==> googlecompute: Creating instance...
    googlecompute: Loading zone: us-west1-a
    googlecompute: Loading machine type: n1-standard-1
    googlecompute: Loading network: default
    googlecompute: Requesting instance creation...
    googlecompute: Waiting for creation operation to complete...
==> googlecompute: Error creating instance: 1 error(s) occurred:
==> googlecompute: 
==> googlecompute: * The resource '123412341234-compute@developer.gserviceaccount.com' of type 'serviceAccount' was not found.
Build 'googlecompute' errored: Error creating instance: 1 error(s) occurred:

* The resource '123412341234-compute@developer.gserviceaccount.com' of type 'serviceAccount' was not found.

==> Some builds didn't complete successfully and had errors:
--> googlecompute: Error creating instance: 1 error(s) occurred:

* The resource '123412341234-compute@developer.gserviceaccount.com' of type 'serviceAccount' was not found.

==> Builds finished but no artifacts were created.

Why is it trying to use 123412341234-compute@developer.gserviceaccount.com?

I created a service account with compute admin v1 permissions under my project in google cloud and I downloaded my json file and renamed it to accounts.json. The name of that service account is different (release-builder@rare-truck-123456.iam.gserviceaccount.com), but packer seems to ignore it and go after some strange account.

Even my cli command gcloud info gives back the right service account:

Google Cloud SDK [188.0.1]

Platform: [Mac OS X, x86_64] ('Darwin', 'Alexs-MacBook-Pro.local', '16.7.0', 'Darwin Kernel Version 16.7.0: Thu Jan 11 22:59:40 PST 2018; root:xnu-3789.73.8~1/RELEASE_X86_64', 'x86_64', 'i386')
Python Version: [2.7.14 (default, Sep 25 2017, 09:53:22)  [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]]
Python Location: [/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python]
Site Packages: [Disabled]

Installation Root: [/Users/Joe/Downloads/google-cloud-sdk]
Installed Components:
  core: [2018.02.08]
  gsutil: [4.28]
  bq: [2.0.28]
System PATH: [/Users/Joe/Downloads/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]
Python PATH: [/Users/Joe/Downloads/google-cloud-sdk/lib/third_party:/Users/Joe/Downloads/google-cloud-sdk/lib:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python27.zip:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old:/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload]
Cloud SDK on PATH: [True]
Kubectl on PATH: [False]

Installation Properties: [/Users/Joe/Downloads/google-cloud-sdk/properties]
User Config Directory: [/Users/Joe/.config/gcloud]
Active Configuration Name: [default]
Active Configuration Path: [/Users/Joe/.config/gcloud/configurations/config_default]

Account: [release-builder@rare-truck-123456.iam.gserviceaccount.com]
Project: [rare-truck-123456]

Current Properties:
  [core]
    project: [rare-truck-123456]
    account: [release-builder@rare-truck-123456.iam.gserviceaccount.com]
    disable_usage_reporting: [True]
  [compute]
    region: [us-west1]
    zone: [us-west1-a]

Logs Directory: [/Users/Joe/.config/gcloud/logs]
Last Log File: [/Users/Joe/.config/gcloud/logs/2018.02.09/15.51.18.911677.log]

git: [git version 2.14.3 (Apple Git-98)]
ssh: [OpenSSH_7.4p1, LibreSSL 2.5.0]

Solution

  • Google Compute Engine instances use default services account to have a better integration with Google Platform.

    As you can read on the documentation [1]: "(...)If the user will be managing virtual machine instances that are configured to run as a service account, you must also grant the roles/iam.serviceAccountActor role."

    You need to add the role "Service Account User" to your service account in order to be able to create Google Compute Engine instances.

    [1] https://cloud.google.com/iam/docs/understanding-roles#compute_name_short_roles