Edgar.A
Edgar.A

Reputation: 1383

Optional block based on variable

I'm writing sort of wrapper module for azurerm_storage_account.

azurerm_storage_account has optional block

static_website {
  index_document = string
  error_404_document = string
}

I want to set it based on variable and I'm not really sure how can I do that? Conditional operators don't really work for blocks (e.g. static_website = var.disable ? null : { .. } )

Or do blocks work in such a way that if I'd set index_document and error_404_document to null it'd be the same as not setting static_website block altogether?

[email protected]

[email protected]

Upvotes: 23

Views: 18278

Answers (1)

Marcin
Marcin

Reputation: 238081

I think you can use dynamic block for that. Basically, when the disable is true, no static_website will be created. Otherwise, one static_website block is going to be constructed.

For example, the modified code could be:

  dynamic "static_website" {

    for_each = var.disable == true ? toset([]) : toset([1])

    content {
        index_document = string
        error_404_document = string
    }
  } 

You could also try with splat to check if disable has value or is null:

  dynamic "static_website" {

    for_each = var.disable[*]

    content {
        index_document = string
        error_404_document = string
    }
  } 

In the above examples, you may need to adjust conditions based on what values var.disable can actually have.

Upvotes: 42

Related Questions