Terraform:如何从特定于“azurerm_storage_management_policy”的局部变量中的输入变量调用字符串列表的值

问题描述 投票:0回答:1

我正在尝试使用 terraform 分配存储帐户生命周期管理策略。超过 30 个容器的列表在 input.tfvars 文件中定义为字符串列表,需要在“资源 azurerm_storage_management_policy 生命周期”的 main.tf 中调用。但它给出了错误。请指导我应该如何调用该变量。

我的git中的代码详细信息如下。

输入.tfvars -

containername = ["a", "b", "c", "d", "e", "f", "g".................................]

此列表包括 30 多个容器名称。

变量.tf ---

variable "containername" {
  type    = list(string)
  default = []
}

main.tf --

locals {
   folderlist1  = var.containername
   list1 = [ for a in local.folderlist1: a ]
 }
 output "result1" {
     value = local.folderlist1
}
resource "azurerm_storage_management_policy" "lifecycle" {
  storage_account_id = azurerm_storage_account.sa.id
  rule{
    name    = "Rule1"
    enabled = true
    filters {
      prefix_match = local.folderlist1
      blob_types   = ["blockBlob"] 
    }
    actions {
      base_blob {
        delete_after_days_since_modification_greater_than = 15
      }
      snapshot {
        delete_after_days_since_creation_greater_than = 15
      }
    }
  }
}

给出错误“prefix_match”:元素 0:需要字符串。

azure terraform terraform-provider-azure azure-storage-account terraform-variables
1个回答
0
投票

为此,您需要使用带有

dynamic
元参数的
for_each
块:

locals {
  folderlist1  = var.containername
  list1 = [ for a in local.folderlist1: a ]
}

output "result1" {
  value = local.folderlist1
}

resource "azurerm_storage_management_policy" "lifecycle" {
  storage_account_id = azurerm_storage_account.sa.id
  dynamic "rule" {
    for_each = local.folderlist1
    content {
      name    = "Rule-${rule.key}"
      enabled = true
      filters {
        prefix_match = rule.value
        blob_types   = ["blockBlob"]
      }
      actions {
        base_blob {
          delete_after_days_since_modification_greater_than = 15
        }
        snapshot {
          delete_after_days_since_creation_greater_than = 15
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.