Google CloudGoogle API Gatewaygoogle_api_gateway_apiPro

How to Import an Google API Gateway API into Terraform

To import an existing Google API Gateway API into Terraform, scan it with Terraback and run terraback gcp import --method bulk. Terraback writes the matching google_api_gateway_api resource block and the Terraform 1.5+ import block for you, so you do not have to run terraform import by hand. The import ID is the full path projects/{project}/locations/global/apis/{api_id} (the short forms {project}/{api_id} and {api_id} are also accepted).

Import Google API Gateway API with Terraback (recommended)

Terraback reverse-engineers your live infrastructure: it reads the Google API Gateway resource with read-only credentials, generates the HCL, and produces the exact import block. Two commands take you from a live Google API Gateway API to managed Terraform.

1

Scan your Google Cloud account

terraback scan all gcp --project my-gcp-project
2

Generate import blocks and import into state

terraback gcp import --method bulk

The Terraform import block

Terraback emits a Terraform 1.5+ import block like the one below. Because the block lives in your configuration, the import is reviewable in a pull request and repeatable across environments.

import {
  to = google_api_gateway_api.orders
  id = "projects/my-project/locations/global/apis/orders-api"
}

Example google_api_gateway_api configuration

Here is a realistic Google API Gateway API block. Terraback generates a fuller version from your actual resource attributes; this is a minimal, valid starting point.

resource "google_api_gateway_api" "orders" {
  provider     = google-beta
  api_id       = "orders-api"
  display_name = "Orders API"
}

Gotchas when importing a Google API Gateway API

  • The API is always global; the location segment in the import path is the literal string global, not a region.
  • This is a beta resource and requires provider = google-beta on the api and every child api_config and gateway.
  • Creating the api provisions a backing managed_service; that service name is read-only and is what api_configs and gateways attach to.
  • api_id is immutable; changing it forces Terraform to destroy and recreate the API.

Doing it manually with terraform import

The native approach is to write the google_api_gateway_api block by hand, then run terraform import google_api_gateway_api.example <import-id> for every resource, one at a time. That works for a handful of resources, but it does not scale: you author all the HCL yourself and repeat the command for each item. Terraback generates the HCL and the import blocks for your whole account in one pass.

Import your whole Google Cloud account in minutes

Terraback scans 80+ Google Cloud resource types and emits clean Terraform plus import blocks, running locally with read-only credentials. $499 once, no SaaS.