Creating endpoint in cloud run with Terraform and Google Cloud Platform

[亡魂溺海] 提交于 2020-08-06 05:44:30

问题


I'm research for a way to use Terraform with GCP provider to create cloud run endpoint. For starter I'm creating testing data a simple hello world. I have resource cloud run service configured and cloud endpoints resource configured with cloud endpoints depends_on cloud run. However, I'm trying to pass in the cloud run url as a service name to the cloud endpoints. File are constructed with best practice, with module > cloud run and cloud endpoints resource. However, the Terraform interpolation for passing the output of service_name = "${google_cloud_run_service.default.status[0].url}" Terraform throughs an Error: Invalid character. I've also tried module.folder.output.url. I have the openapi_config.yml hardcoded in the TF config within I'm wondering if it's possible to have to work. I research many post and some forum are outdated.

 #Cloud Run
 resource "google_cloud_run_service" "default" {
 name     = var.name
 location = var.location

 template {
   spec {
     containers {
       image = "gcr.io/cloudrun/hello"
     }
   }




   metadata {
     annotations = {
       "autoscaling.knative.dev/maxScale" = "1000"
       "run.googleapis.com/cloudstorage"  = "project_name:us-central1:${google_storage_bucket.storage-run.name}"
       "run.googleapis.com/client-name"   = "terraform"
     }
   }
 }
 traffic {
   percent         = 100
   latest_revision = true
 }
 autogenerate_revision_name = true
}
output "url" {
 value = "${google_cloud_run_service.default.status[0].url}"
}

data "google_iam_policy" "noauth" {
 binding {
   role = "roles/run.invoker"
   members = [
     "allUsers",
   ]
 }
}

resource "google_cloud_run_service_iam_policy" "noauth" {
 location = google_cloud_run_service.default.location
 project  = google_cloud_run_service.default.project
 service  = google_cloud_run_service.default.name

 policy_data = data.google_iam_policy.noauth.policy_data
}

#CLOUD STORAGE
resource "google_storage_bucket" "storage-run" {
 name          = var.name
 location      = var.location
 force_destroy = true

 bucket_policy_only = true


}


data "template_file" "openapi_spec" {
 template = file("${path.module}/openapi_spec.yml")
}

#CLOUD ENDPOINT SERVICE 
resource "google_endpoints_service" "api-service" {
 service_name   = "api_name.endpoints.project_name.cloud.goog"
 project        = var.project
 openapi_config    = data.template_file.openapi_spec.rendered
}


ERROR: googleapi: Error 400: Service name 'CLOUD_RUN_ESP_NAME' provided in the config files doesn't match the service name 'api_name.endpoints.project_name.cloud.goog' provided in the request., badRequest

So I later discovered, that the service name must match the same as the host/cloud run esp service url without https:// in order for the cloud endpoint services to provisioner. Terraform docs states otherwise in the form of " $apiname.endpoints.$projectid.cloud.goog " terraform_cloud_endpoints and in GCP docs states that the cloud run ESP service must be the url without https:// > gateway-12345-uc.a.run.app Getting Started with Endpoints for Cloud Run

来源:https://stackoverflow.com/questions/62461956/creating-endpoint-in-cloud-run-with-terraform-and-google-cloud-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!