Conditional block or allow variable for wafv2 resource when using override_action or default_action

I have following terraform resource:

resource "aws_wafv2_web_acl" "main" {
  name  = var.name_prefix
  scope = "REGIONAL"

  default_action {
    block {}
  }
}

Question: how can I make default_action block so that it can be passed as a variable? Is there some solution using dynamic block that I am not aware of?

I have tried:

  dynamic "default_action" {
    for_each = var.default_action
    content {
      default_action.value
    }
  }

but this is simply failing with an error: An argument or block definition is required here. To set an argument, use the equals sign “=” to introduce the argument value.

Same problem applies to rule block which can contain:

   override_action {
      count {}
  }

Please advise :slight_smile:

Try below approach -

default_action {

 dynamic "allow" {
   for_each = var.default_action == "allow" ? [""] : []
   content {
   }
 }

 dynamic "block" {
   for_each = var.default_action == "block" ? [""] : []
   content {
   }
 }

}
It worked for me :slight_smile:

1 Like

In stead of doing [""]; I used ["a"] and ["b"] for allow and block respectively, which essentially does the same job but just looks a bit nicer to me :slight_smile: