24

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]

1 Answer 1

43

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.