云功能模块 Terraform

Cloud Function Module Terraform

我对 Terraform 比较陌生,正在尝试创建一个可以同时启动多个云功能的工作模块。对我来说抛出错误的部分是我动态调用事件触发器的地方。我在下面写了一个粗略的代码。 有人可以建议我做错了什么吗?

Main.tf

resource "google_cloudfunctions_function" "event-function" {
  for_each = var.cloudfunctions
  project              = local.test_project
  region               = lookup(local.regions,"use1")
  name                 = format("clf-%s-%s-use1-%s-%s", var.domain, var.env, var.use_case, each.key)
  description          = format("clf-%s-%s-use1-%s-%s", var.domain, var.env, var.use_case, each.key)
  #source_directory     = "${path.module}/${each.value}}
  #bucket_force_destroy = var.bucket_force_destroy
  entry_point          = each.value.entry_point
  runtime              = each.value.runtime
  #vpc_connector        = "projects/${var.host_project}/locations/${var.region}/connectors/${var.vpc_connector_prefix}-${var.environment}-test"
 
 dynamic event_trigger {
    for_each = [ for i in each.value.event_trigger : lookup(local.event_trigger,i.event_name,i.resource) ]
    content {
        event_type          = event_trigger.value.event_type
        resource            = event_trigger.value.resource
      }
  }

Variables.tf

variable "cloudfunctions" {
  type = map(object({
  runtime       = string
  event_trigger = list(object({
    event_type = string
    resource = string

   }))
  }))
  default = {}
}

Locals.tf

42.event_trigger = flatten ([
43.     for i,n in var.cloudfunctions :[
44.     for event in n.event_trigger :{
45.        event_type = event_type
46.     resource   = resource

       }
     ]
  ])

}

错误

 on locals.tf line 44, in locals:
│   44:         event_type = event_type
│ 
│ A reference to a resource type must be followed by at least one attribute
│ access, specifying the resource name.
╵
╷
│ Error: Invalid reference
│ 
│   on locals.tf line 45, in locals:
│   45:         resource   = resource
│ 
│ The "resource" object must be followed by two attribute names: the resource
│ type and the resource name.

您的 event_triggern。因此,您的 event_trigger 应该是:

  event_trigger = flatten ([
     for i,n in var.cloudfunctions :[
       for event in n.event_trigger: {
        event_type = event.event_type
        resource   = event.resource
       }
     ]
  ])