將 Cloud Run 服務對應至自訂網域

這個範例說明如何將 Cloud Run 服務對應至您建立的自訂網域。將 verified-domain.com 換成您已驗證的自訂網域,例如 example.com 或 subdomain.example.com

程式碼範例

Terraform

如要瞭解如何套用或移除 Terraform 設定,請參閱「基本 Terraform 指令」。 詳情請參閱 Terraform供應商參考說明文件

resource "google_cloud_run_v2_service" "default" {
  name     = "custom-domain" # Replace with your service name
  location = "us-central1"

  deletion_protection = false # set to true to prevent destruction of the resource

  template {
    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello" # Replace with your container image
    }
  }
}
data "google_project" "project" {}

resource "google_cloud_run_domain_mapping" "default" {
  name     = "verified-domain.com"
  location = google_cloud_run_v2_service.default.location
  metadata {
    namespace = data.google_project.project.project_id
  }
  spec {
    route_name = google_cloud_run_v2_service.default.name
  }
}

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器