Reputation: 177
Hello Experts I am trying to build a terraform module for cloud function in which i can create multiple cloud functions at once. I am dynamically creating the event trigger but not sure if my approach is right. The dynamic event trigger is the part where i am getting stuck. Can someone validate my approach. I have included the code below :
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
}
]
])
}
Upvotes: 0
Views: 418
Reputation: 177
To use the dynamic event trigger block you need :
dynamic event_trigger {
for_each = each.value.event_trigger
content {
event_type = event_trigger.value.event_type
resource = event_trigger.value.resource
}
}
This worked fine for me.
Upvotes: 1