Terraform で google_api_gateway_api_config に api_config_id を設定すると再作成時に罠がある

まとめ Terraform で google_api_gateway_api_config に api_config_id は設定しない 以下詳細 以下のように api_config_id を設定すると、再作成時にエラーが発生する resource "google_api_gateway_api_config" "default" { project = var.project_id provider = google-beta api = google_api_gateway_api.default.api_id api_config_id = "${var.env}-${var.service}-api-config" openapi_documents { document { path = "openapi.yaml" contents = base64encode(templatefile("${path.module}/openapi.yaml", { title = "${var.env} ${var.service} API" description = "This is the API for ${var.env} ${var.service}" version = "1.0.0" managed_service = google_api_gateway_api.default.managed_service func_url = google_cloud_run_service.default.status[0].url })) } } lifecycle { create_before_destroy = true } } │ Error: Error creating ApiConfig: googleapi: Error 409: Resource 'projects/{project_id}/locations/global/apis/{api_id}/configs/{api_config_name}' already exists │ Details: │ [ │ { │ "@type": "type.googleapis.com/google.rpc.ResourceInfo", │ "resourceName": "projects/{project_id}/locations/global/apis/{api_id}/configs/{api_config_name}" │ } │ ] 一見、create_before_destroy = true が指定されているので、新しいリソースを作成してから古いリソースを削除するように見えるが、 実際には api_config_id が変更されていないため、新しいリソースを作成することができない 省略した場合はタイムスタンプベースでIDが振られている… terraform-YYYYMMDDHHMMSS000000000001-xxxxxxxxxxxxx

2024-11-15 ·  2024-11-15 · 1 分 · 108 文字

GCP ApiGateway + CloudRun で CORS を設定する

構成 GCP ApiGateway -> CloudRun ApiGateway で OpenAPI yaml を定義している 1. OpenAPI yaml で x-google-endpoints に allowCors: True を設定して、バックエンドでCORSリクエストを処理する x-google-endpoints: - name: ${managed_service} allowCors: True # バックエンドでCORSリクエストを処理する 2. CloudRun で OPTIONS メソッドを設定する // 以下のように cors ミドルウェアを挟む http.HandleFunc("/example", cors(user.Handler)) // 定義はこんな感じ func cors(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { slog.Info("==> Reply OPTIONS Request", "status", http.StatusNoContent) w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization") w.Header().Set("Access-Control-Expose-Headers", "Content-Length,Content-Range") w.Header().Set("Access-Control-Max-Age", "1728000") // 20 days: reduce preflight requests w.WriteHeader(http.StatusNoContent) // 204 return } next.ServeHTTP(w, r) } } Refs Endpoints の CORS サポートを有効にする | Cloud Endpoints with OpenAPI | Google Cloud

2024-11-15 ·  2024-11-15 · 1 分 · 105 文字