HiveBrain v1.2.0
Get Started
← Back to all entries
snippetterraformMinor

How to enable Google Maps and generate an API key using Terraform?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
enablegooglemapsgenerateusinghowandapikeyterraform

Problem

I've been trying to spin up a project from scratch over the Christmas break to automate the creation of my home network, one of those components is the Ubiquiti Controller which requires a Google Maps API key to plot devices onto maps.

I've been able to automate the creation of the project using Terraform:

resource "google_project" "slaterfamily_network" {
  depends_on      = ["data.google_organization.org"]
  name            = "Slater Family Network"
  project_id      = "slaterfamily-network"
  org_id          = "${data.google_organization.org.id}"
  billing_account = "${var.billing_account}"
}

resource "google_project_iam_binding" "account_owner" {
  project = "${google_project.slaterfamily_network.project_id}"

  # It's important to specify ALL members, as this replaces them:
  #   https://twitter.com/devopsreact/status/840233052194320384
  members = [
    "user:richard@slaterfamily.name",
    "serviceAccount:${data.google_service_account.terraform_service_account.email}",
  ]

  role = "roles/owner"
}


This works fine for configuring Google Cloud DNS, however, I can't seem to find a way of enabling Google Maps and generating an API key, the following statements enable the Service Usage API, however it fails enabling the Maps API:

resource "google_project_service" "service_usage" {
  project = "${var.project_id}"
  service = "serviceusage.googleapis.com"
}

resource "google_project_service" "maps" {
  depends_on = ["google_project_service.service_usage"]
  project = "${var.project_id}"
  service = "maps.googleapis.com"
}


With this error:

google_project_service.maps: Error enabling service: failed to issue request: googleapi: Error 403: The caller does not have permission, forbidden


Which after tracing the HTTP calls gives me the following, formatted as cURL for ease of reproduction:

```
curl -H 'Host: serviceusage.googleapis.com' -H 'content-type: application/json' -H 'authorization: Bearer {{ BEARER_TOKEN }}' -H 'user-agent: googl

Solution

I guess the service ID you're looking for is maps-backend.googleapis.com or static-maps-backend.googleapis.com if you want to generate static maps.

➜  ~ gcloud services list --available |grep maps
maps-android-backend.googleapis.com                                                                  Maps SDK for Android
maps-backend.googleapis.com                                                                          Maps JavaScript API
maps-embed-backend.googleapis.com                                                                    Maps Embed API
maps-ios-backend.googleapis.com                                                                      Maps SDK for iOS
static-maps-backend.googleapis.com                                                                   Maps Static API


I've got these result with Google Cloud SDK version 360.0.0, also there is apikeys.googleapis.com service available with alpha component which makes me think it could be achievable by using gcloud module and gcloud alpha services api-keys create command.

Code Snippets

➜  ~ gcloud services list --available |grep maps
maps-android-backend.googleapis.com                                                                  Maps SDK for Android
maps-backend.googleapis.com                                                                          Maps JavaScript API
maps-embed-backend.googleapis.com                                                                    Maps Embed API
maps-ios-backend.googleapis.com                                                                      Maps SDK for iOS
static-maps-backend.googleapis.com                                                                   Maps Static API

Context

StackExchange DevOps Q#5840, answer score: 2

Revisions (0)

No revisions yet.