Google Cloud Platform の Google Cloud API を terraform から有効化してみた

2023.07.26

西田@CX事業本部です。

今回は Google Cloud Platform の Google Cloud API を Terraform から有効化してみました。terraformでリソースを作成するときに、APIを有効化してないと以下のようなエラーになり、リソースの作成ができません

 [
   {
     "@type": "type.googleapis.com/google.rpc.Help",
     "links": [
       {
         "description": "Google developers console API activation",
         "url": "https://console.developers.google.com/apis/api/artifactregistry.googleapis.com/overview?project=xxx"
       }
     ]
   },
   {
     "@type": "type.googleapis.com/google.rpc.ErrorInfo",
     "domain": "googleapis.com",
     "metadata": {
       "consumer": "projects/xxx",
       "service": "artifactregistry.googleapis.com"
     },
     "reason": "SERVICE_DISABLED"
   }
]

このエラーはコンソールから API を有効化すれば解消します。

簡単に解決できますが、できればTerraformでAPI有効化までしておきたいケースもあると思います

このような場合に Google Cloud Project Factory Terraform Module が使えます

Google Cloud Project Factory Terraform Module について

Google Cloud Project Factory Terraform Module は Google Cloud Platform の Project の設定が行える、 Terraform の module です。この module を使うことで主に以下の操作ができます

  • VPC
  • IAM
  • サービスアカウント
  • APIの有効、無効

今回はこの APIの有効もしくは無効にする機能を使って、 Terraform から、そのプロジェクトで使用するGoogle Cloud APIの有効化を行います

Terraform のコード

Terraform のファイルを以下の3つの役割に分けて紹介します

  • apiを有効化 (apis.tf)
  • 変数を定義 (variables.tf)
  • サービスのリソースを作成 (artifact.tf)

apis.tf

Google Cloud Project Factory Terraform Moduleが提供する google_project_services module を使ってapiを有効化しています

このモジュールを使う場合は、運用中に誤って api を disabled にしてしまう可能性に気をつける必要があります

module "project_services" {
  source                      = "terraform-google-modules/project-factory/google//modules/project_services"
  version                     = "14.2.1"
  disable_services_on_destroy = false
  project_id                  = var.project_id
  enable_apis                 = var.enable_apis
  activate_apis = [
    "artifactregistry.googleapis.com"
  ]
}

google_project_services で、今回使用した Input は以下です

Inputs 説明
project_id プロジェクトID
disable_services_on_destroy リソースが削除された時にサービスを disabled にするかどうか
activate_apis 有効にするサービスAPIを羅列する
enable_apis APIを実際に有効にするかどうか。もし false にした場合、このモジュールは何もしない

activate_apis に利用したい Service を指定します。 有効なサービスは以下のコマンドで取得できます

> gcloud services list

NAME                              TITLE
artifactregistry.googleapis.com   Artifact Registry API
bigquery.googleapis.com           BigQuery API
bigquerystorage.googleapis.com    BigQuery Storage API
cloudapis.googleapis.com          Google Cloud APIs
cloudbuild.googleapis.com         Cloud Build API
clouddebugger.googleapis.com      Cloud Debugger API (Deprecated)
cloudtrace.googleapis.com         Cloud Trace API
containerregistry.googleapis.com  Container Registry API
datastore.googleapis.com          Cloud Datastore API
logging.googleapis.com            Cloud Logging API
monitoring.googleapis.com         Cloud Monitoring API
pubsub.googleapis.com             Cloud Pub/Sub API
run.googleapis.com                Cloud Run Admin API
secretmanager.googleapis.com      Secret Manager API
servicemanagement.googleapis.com  Service Management API
serviceusage.googleapis.com       Service Usage API
sql-component.googleapis.com      Cloud SQL
sqladmin.googleapis.com           Cloud SQL Admin API
storage-api.googleapis.com        Google Cloud Storage JSON API
storage-component.googleapis.com  Cloud Storage

variables.tf

Terraform の変数を定義しています。利用者によって可変である Project Id と Region、APIの有効化、無効化を利用するかどうかのフラグを上書きできるようにしてあります 

variable "project_id" {
  type        = string
  description = "Google Cloud Project ID"
}

variable "region" {
  type        = string
  description = "GCP Zone for provisioning zonal resources."
  default     = "asia-northeast1"
}

variable "enable_apis" {
  type    = bool
  default = true
}

artifact.tf

APIの有効化が必要なサービスのリソースを作成しています。今回は例として Artifact Registory のリポジトリを作成します。 depends_on に APIを有効化するモジュールを指定して、APIが有効されてからリソースが作成されるようにしています

resource "google_artifact_registry_repository" "api" {
  provider = google

  project       = var.project_id
  location      = var.region
  repository_id = local.api_name
  description   = "Container image registry for API"
  format        = "Docker"

  depends_on = [module.project_services]
}

参考

https://github.com/terraform-google-modules/terraform-google-project-factory

https://github.com/GoogleCloudPlatform/terraform-dynamic-python-webapp