Terraform:Cloud Run服务上的Cloud Endpoints?

时间:2019-10-28 18:15:21

标签: google-cloud-platform terraform google-cloud-endpoints google-cloud-run terraform-provider-gcp

是否可以在Cloud Run服务上运行Cloud Endpoint?

假设我有以下main.tf,并且在定义Cloud Endpoints服务时想使用Cloud Run的URL。 该URL可能存储在google_cloud_run_service.cloud-run.status.url下。 以下配置会引发错误。

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}" # <--------
[...]

我丢失或误解了什么吗? 预先感谢!

2 个答案:

答案 0 :(得分:0)

您可以按照以下documentation设置Cloud Run的Cloud Endpoints。

您的main.tf文件不会等待Cloud Run服务准备就绪,以便继续进行将可扩展服务代理(ESP)容器部署到Cloud Run的后续步骤。

示例用法here显示了如何使用局部变量等待Cloud Run服务准备就绪。

答案 1 :(得分:0)

我找到了解决方案:

# 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"]

我还不确定100%是否可以在第一次运行时使用。尽管如此,我希望depends_on(请参见上文)会处理这种依赖性,并等待创建Cloud Run之后再继续创建Cloud Endpoints服务。