Terraform 代码未在 Azure 存储生命周期中创建多个规则

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

我尝试通过 IAC terraform 创建 Azure 存储生命周期。但面临以下错误。

预计为存储生命周期创建多个规则,但只创建了一个规则。如果我们重新运行 terraform apply ,它会与旧规则重叠,但仍然创建 1 条规则。我正在使用变量“rules”的默认值。

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.72.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_storage_account" "storage_account" {
  name                     = var.storage_account_name
  resource_group_name      = var.resource_group_name
  location                 = var.location
  account_kind             = var.account_kind
  account_tier             = var.account_tier
  account_replication_type = "LRS"
  access_tier              = var.access_tier
}

resource "azurerm_storage_management_policy" "storage_management_policy" {
  storage_account_id = azurerm_storage_account.storage_account.id

  for_each = var.rules

  rule {
    name    = each.value.name
    enabled = true
    
    filters {
      prefix_match = each.value.prefix_match
      blob_types   = ["blockBlob"]
    }

    actions {
      base_blob {
        delete_after_days_since_creation_greater_than= each.value.base_blob.delete_after_days_since_creation_greater_than
      }

    }
  }
}

Var.tf:

variable "storage_account_name" {
  type        = string
  description = "The name of the Storage Account."
  default     = "sakinaka"
}

variable "resource_group_name" {
  type        = string
  description = "The name of the Resource Group where the Storage Account is located."
  default     = "bombay"
}

variable "location" {
  type        = string
  description = "The location where the Storage Account is created."
  default     = "East US"
}

variable "account_kind" {
  type        = string
  description = "The kind of the Storage Account."
  default     = "StorageV2"
}

variable "account_tier" {
  type        = string
  description = "The tier of the Storage Account."
  default     = "Standard"
}

variable "access_tier" {
  type        = string
  description = "The access tier of the Storage Account."
  default     = "Hot"
}

variable "rules" {
  type = map(object({
    name            = string
    prefix_match    = list(string)
    base_blob       = object({
      delete_after_days_since_creation_greater_than  = number
    })
  }))
  default = {
    rule3 = {
      name          = "samplerule3"
      prefix_match  = []
      base_blob     = {
        delete_after_days_since_creation_greater_than  = 365
      }
    },
    rule4 = {
      name          = "samplerule4"
      type          = "Lifecycle"
      prefix_match  = ["arch/"]
      base_blob     = {
        delete_after_days_since_creation_greater_than  = 90
      }
    }
  }
}

请看一下这个

azure azure-devops terraform terraform-provider-azure terragrunt
1个回答
0
投票

我认为在这种情况下,您想要将

[dynamic][1]
块与
for_each
一起使用,类似于:

resource "azurerm_storage_management_policy" "storage_management_policy" {
  storage_account_id = azurerm_storage_account.storage_account.id

  dynamic "rule" {
    for_each = var.rules
    content {
      enabled = false
      name    = rule.value.name

      actions {
        base_blob {
          delete_after_days_since_creation_greater_than = rule.value.base_blob.delete_after_days_since_creation_greater_than
        }
      }
      
      filters {
        blob_types   = [ "blockBlob" ]
        prefix_match = [ rule.value.prefix_match ]
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.