You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform配置:引用google_cloudfunctions2_function的url属性报错求助

问题

尝试使用Terraform搭建Google Cloud Functions Gen2,并将google_cloudfunctions2_function资源的URL属性用于google_pubsub_subscription资源的推送配置中,但运行时报错提示该资源没有url属性。相同配置在Gen1云函数中可正常运行。

cloudfunction.tf代码:

resource "google_cloudfunctions2_function" "task_gateway" {
  name        = "task-gateway"
  description = "Accepts tasks created from dispatcher and sends them to the tasks API"
  project     = google_project.gather.project_id
  location    = local.region
  labels      = var.labels

  service_config {
    service_account_email = google_service_account.task_gateway.email
    available_cpu         = 0.583
    available_memory      = "512Mi"
    timeout_seconds       = 300
  }

  build_config {
    runtime     = "nodejs20"
    entry_point = "helloGET"
    source {
      storage_source {
        bucket = google_storage_bucket.gather_staging.name
        object = google_storage_bucket_object.hello_world_zip.name
      }
    }
  }

  lifecycle {
    ignore_changes = all
  }
}

pubsub.tf代码:

resource "google_pubsub_subscription" "tasks_gateway_handler" {
  project                 = google_project.gather.project_id
  name                    = "tasks-gateway-handler"
  topic                   = google_pubsub_topic.tasks_gateway.name
  labels                  = var.labels
  ack_deadline_seconds    = 300
  enable_message_ordering = false
  expiration_policy {
    # ttl empty means the subscription will never expire
    ttl = ""
  }

  push_config {
    push_endpoint = google_cloudfunctions2_function.task_gateway.url
    oidc_token {
      service_account_email = google_service_account.task_gateway.email
      audience              = google_cloudfunctions2_function.task_gateway.url
    }
  }

  retry_policy {
    minimum_backoff = "10s"
    maximum_backoff = "600s"
  }
}

报错信息:

Error: Unsupported attribute

  on .terraform/<path>/pubsub.tf line 97, in resource "google_pubsub_subscription" "tasks_gateway_handler":
  97:     push_endpoint = google_cloudfunctions2_function.task_gateway.url

This object has no argument, nested block, or exported attribute named "url".
解决方案

1. 修正属性引用路径

Gen2云函数的Terraform资源google_cloudfunctions2_function没有直接的url导出属性,对应的访问地址嵌套在service_config块中,属性名为uri。由于service_config是列表类型,需要通过索引访问第一个元素:

google_cloudfunctions2_function.task_gateway.service_config[0].uri

2. 调整lifecycle的ignore_changes配置

你当前设置了ignore_changes = all,这会让Terraform忽略该资源所有属性的状态同步,包括导出的uri属性,导致状态文件中无法获取到该值供其他资源引用。如果确实需要忽略部分属性的变更,应该明确指定具体属性,而非使用all:

lifecycle {
  ignore_changes = [labels] # 仅忽略labels的变更,根据实际需求调整
}

修改后的pubsub.tf推送配置部分

push_config {
  push_endpoint = google_cloudfunctions2_function.task_gateway.service_config[0].uri
  oidc_token {
    service_account_email = google_service_account.task_gateway.email
    audience              = google_cloudfunctions2_function.task_gateway.service_config[0].uri
  }
}

内容的提问来源于stack exchange,提问作者AnandShiva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:16:10