Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: Cloud Endpoints on a Cloud Run service?

Is there a way to run Cloud Endpoints on a Cloud Run service?

Let's say I have the following main.tf and I want to use Cloud Run's URL when I am defining my Cloud Endpoints services. That URL is supposedly stored under google_cloud_run_service.cloud-run.status.url. The configuration below throws an error.

Output from terraform plan:

Error: Unsupported attribute

  on main.tf line 411, in resource "google_endpoints_service" "cloud-run":
 411:   service_name = "${google_cloud_run_service.cloud-run.status.url}"

This value does not have any attributes.

main.tf:

[...]
#############
# Cloud Run #
#############
resource "google_cloud_run_service" "cloud-run" {
  name     = "cloud-run"
  provider = "google-beta"
  location = "europe-west1"
  metadata {
    namespace = "${var.gcp_project[var.env]}"
  }
  spec {
    containers {
      image = "gcr.io/endpoints-release/endpoints-runtime-serverless@sha256:a12b14dd6d31a88637ca7c9e63724ad542226d9509421ba08ed4452a91ce751e"
    }
    container_concurrency = var.env != "dev" ? 0 : 1
  }
}

###################
# Cloud Endpoints #
###################
resource "google_endpoints_service" "pre-pairing-api" {
  # The service name, AFAIK, should be Cloud Run's URL:
  service_name = "${google_cloud_run_service.cloud-run.status.url}" #  <--------
  openapi_config = <<EOF
swagger: '2.0'
info:
  title: Pre-pairing
  description: API on Cloud Endpoints with a Google Cloud Functions backend...
  version: 1.0.0
# Same applies to the host. It should be, AFAIK, Cloud Run's URL.
host: "${google_cloud_run_service.cloud-run.status.url}" # <--------
[...]

Am I missing or misunderstanding something? Thanks in advance!

like image 669
Oleg Korol Avatar asked Oct 28 '19 18:10

Oleg Korol


2 Answers

I found a solution for this:

# main.tf
[...]
#############
# Cloud Run #
#############
resource "google_cloud_run_service" "cloud-run" {
  [...]
}


# The URL was located under `status[0].url` instead of `status.url`.
# I have created a local variable to store its value.
locals {
  cloud_run_url = google_cloud_run_service.cloud-run.status[0].url
}

###################
# Cloud Endpoints #
###################
resource "google_endpoints_service" "some-api" {
  service_name = "${replace(local.cloud_run_url, "https://", "")}" # <--------
  openapi_config = <<EOF
swagger: '2.0'
info:
  title: Some-API
  description: API on Cloud Endpoints with a Google Cloud Functions backend...
  version: 1.0.0
host: "${replace(local.cloud_run_url, "https://", "")}" # <--------
[...]
EOF

depends_on = ["google_cloud_run_service.cloud-run"]

I am not yet 100% sure if this would work for the very first run though. Nonetheless, I would expect depends_on (see above) to take care of this dependency and wait for Cloud Run to be created before proceeding to create the Cloud Endpoints service.

like image 134
Oleg Korol Avatar answered Oct 19 '22 07:10

Oleg Korol


You can set up Cloud Endpoints for Cloud Run by following this documentation.

Your main.tf file does not wait for the Cloud Run service to be ready in order to proceed with the next steps of deploying the Extensible Service Proxy (ESP) container to Cloud Run.

The example usage here shows how to use a local variable to wait for the Cloud Run service to be ready.

like image 45
Daniel Ocando Avatar answered Oct 19 '22 06:10

Daniel Ocando