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.
CodePudding user response:
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"]
.
CodePudding user response:
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
}
}
}
}