I am using terraform modules to create GCP firewall rule . I am struggling with dynamic variables for which I need some help
My Module definition is as below
##fw.tf
resource “google_compute_firewall” “main” {
name = var.name
network = var.network
direction = var.direction
source_ranges = var.source_ranges
target_tags = var.target_tags
dynamic “allow” {
for_each = var.allow
content {
protocol = allow.value.protocol
ports = allow.value.ports
}
}
##Variables.tf
// instance.tf variables
variable “name” {
type = string
}
variable “network” {
type = string
}
variable “direction” {
type = string
default = “Ingress”
}
variable “source_ranges” {
type = list(string)
}
variable “target_tags” {
type = list(string)
}
variable “protocol” {
type = string
}
variable “ports” {
type = list(string)
}
variable “allow” {
type = map(string)
}
The calling definition is as follows
##fw.tf
module “fw1” {
source = “…/…/modules/fw”
name = var.fw_fwrule_name
network = var.fw_network
description = var.fw_desc
direction = var.fw_direction
priority = var.fw_priority
source_ranges = var.fw_ranges
target_tags = var.fw_target_tags
dynamic “allow” {
for_each = var.fw_allow
content {
protocol = allow.value.protocol
ports = allow.value.ports
}
}
##fw.tfvars
// instance varaibles
project = “infras-12345”
fw_fwrule_name = “fw-allow-httpd”
fw_network = “default”
fw_desc = “Test firewall”
fw_direction = “INGRESS”
fw_priority = 1000
fw_ranges = [
“0.0.0.0/0”,
]
fw_target_tags = [“http”,“https”]
fw_allow = {
protocol = “tcp”
ports = [“80”]
}
##variables.tf
variable “project” {}
variable “fw_fwrule_name” {}
variable “fw_network” {}
variable “fw_desc” {}
variable “fw_direction” {}
variable “fw_priority” {}
variable “fw_ranges” {}
variable “fw_target_tags” {
type = list(string)
}
variable “fw_allow” {
description = “Custom tags to set on the Instances in the ASG”
type = map(string)
default = {}
}
When I do a “terraform plan” it fails with below error
terraform plan -var-file=fw.tfvars
╷
│ Error: Argument or block definition required
│
│ on fw.tf line 22, in module “fw1”:
│ 22:
│
│ An argument or block definition is required here.
Please suggest to resolve the issue