'issue iteration over "aws_wafv2_regex_pattern_set" terraform wafv2

I am facing issue in terraform wafv2. Currently i am stuck in while creating the aws_wafv2_regex_pattern_set resource. The aim is regular expression with loop iteration.

resource "aws_wafv2_regex_pattern_set" "example" {
#for_each = var.parameters 
name        = "example"
description = "Example regex pattern set"
scope       = "REGIONAL"
regular_expression {
for_each = var.parameters 
regex_string = each.value
}
#regular_expression {
#  regex_string = "/api"
#}
tags = {
Tag1 = "Value1"
Tag2 = "Value2" 
}
} 

Secondly i check terraform documentation.

terraform error

But i am getting this

terraform wafv2 aws_wafv2_regex_pattern_set

Not know how to iterate over this resource on block



Solution 1:[1]

Yes, you can iterate and dynamically create regular_expression, using dynamic blocks. For example:

variable "expression_list" {
  default = ["exp1", "exp2"]
}

resource "aws_wafv2_regex_pattern_set" "example" {
  name        = "example"
  description = "Example regex pattern set"
  scope       = "REGIONAL"

  dynamic "regular_expression" {
    for_each = var.expression_list
    content {
       regex_string = regular expression.value
    }
  }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1