简体   繁体   中英

Terraform ignore nested block inside a resource with terraform conditional value

I need to ignore blob properties if the account kind is file storage, I have tried using for each with dynamic but it keeps giving error.

resource "azurerm_storage_account" "sa" {

  name                      = var.name
  location                  = var.location
  resource_group_name       = var.resource_group_name
  account_kind              = var.account_kind
  account_tier              = var.account_tier
  account_replication_type  = var.replication_type
  min_tls_version           = "TLS1_2"
  enable_https_traffic_only = true


  blob_properties {
    dynamic "ignore_filestorage" {
      for_each = var.account_kind == FileStorage ? 0 : 1
      delete_retention_policy {
        days = var.blob_retention_days
      }
      container_delete_retention_policy {
        days = var.container_retention_days
      }
    }
  }

error - Blocks of type "ignore_filestorage" are not expected here. error- A reference to a resource type must be followed by at least one attribute access, specifying the resource name.

If "account_kind" is specified as "filestorage" in var.tf then blob peroperties needs to ignored.

I tried using for each with dynamic but Keeps getting error and can't use count either inside a nested block.

for_each takes a set, not a number. You can't pass it the values 0 or 1. You need to pass it either an empty list, or a list of the size you want to create.

Please take the time to look at the documentation and understand the differences between count and for_each .

For exmaple:

for_each = var.account_kind == FileStorage ? [] : [1]

That would create 0 dynamic blocks if var.account_kind == FileStorage , and one dynamic block if var.account_kind != FileStorage .

Note that the value in [1] is just a placeholder to make the list of size 1 . The value could be anything, like ["stackoverflow"] .

Based on the comments and the resource documentation, you probably want something like this:

resource "azurerm_storage_account" "sa" {

  name                      = var.name
  location                  = var.location
  resource_group_name       = var.resource_group_name
  account_kind              = var.account_kind
  account_tier              = var.account_tier
  account_replication_type  = var.replication_type
  min_tls_version           = "TLS1_2"
  enable_https_traffic_only = true


  dynamic "blob_properties" {
    for_each = var.account_kind == "FileStorage" ? [] : [1]
    content {
      delete_retention_policy {
         days = var.blob_retention_days
      }
      container_delete_retention_policy {
        days = var.container_retention_days
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM