【问题标题】:How to configure azure storage lifecycle with terraform?如何使用 terraform 配置 Azure 存储生命周期?
【发布时间】:2020-01-25 13:58:55
【问题描述】:

所以我使用 terraform for azure provider 来部署我的基础架构。我似乎无法定义存储生命周期。我想添加类似的东西,我发现了,但不能按原样使用。

所以我已经尝试过这个https://github.com/terraform-providers/terraform-provider-azurerm/issues/3316,我已经看遍了。我敢肯定,有一种方法可以告诉 azure 启用生命周期 tiertoarchive 和 tiertodelete……只是似乎无法弄清楚。

谢谢

我在找什么:

*资源 azurerm_storage_management_policy 是一个组成的资源。

resource "azurerm_storage_account" "example" {
  name                     = "myaccount"
  resource_group_name      = "myresourcegroup"
  location                 = "westeurope"
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_management_policy" "example" {
  storage_account_name ="${azurerm_storage_account.example.name}"

  rule {
    name = "rule1"
    enabled = true
    type = "Lifecycle"
    definition {
      filters {
        prefix_match = ["container1/wibble"]
        blob_types = ["blockBlob"]
      }
      actions = {
        base_blob {
          tier_to_cool {
            days_after_modification_greater_than = 30
          }
          tier_to_archive { 
            days_after_modification_greater_than = 90
          }
          delete {
              days_after_modification_greater_than = 2555
          }
        snapshot {
          delete {
            days_after_creation_greater_than = 90
          }
        }
      }
    }
  }
}

https://github.com/terraform-providers/terraform-provider-azurerm/issues/3316

【问题讨论】:

  • 我不确定您是在寻求一种使用 terraform 的方法,还是在 terraform 之外寻求一种方法?因为 tf 现在没有这个功能,所以你总是不得不在门户中做,或者在旁边使用 powershell 或类似的东西,直到 tf 支持它。之后您可以导入它。
  • 啊,好吧,我希望能解决使用 terraform 的问题。有没有办法用 PowerShell 做到这一点?我可以为它运行一个脚本吗?可能。我对所有这些基础设施即代码有点陌生

标签: azure azure-storage terraform terraform-provider-azure


【解决方案1】:

我在您的评论中看到,您要求使用 powershell 来执行此操作。那么是的,可以按照doc 的方式通过powershell。

文档中的示例代码(您可以修改它以满足您的需要)对我有用,请注意,您应该在运行脚本之前安装 azure powershell az module

#Initialize the following with your resource group and storage account names
$rgname = ""
$accountName = ""

#Create a new action object
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction Delete -daysAfterModificationGreaterThan 2555
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -BaseBlobAction TierToArchive -daysAfterModificationGreaterThan 90
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -BaseBlobAction TierToCool -daysAfterModificationGreaterThan 30
$action = Add-AzStorageAccountManagementPolicyAction -InputObject $action -SnapshotAction Delete -daysAfterCreationGreaterThan 90

# Create a new filter object
# PowerShell automatically sets BlobType as “blockblob” because it is the only available option currently
$filter = New-AzStorageAccountManagementPolicyFilter -PrefixMatch ab,cd

#Create a new rule object
#PowerShell automatically sets Type as “Lifecycle” because it is the only available option currently
$rule1 = New-AzStorageAccountManagementPolicyRule -Name Test -Action $action -Filter $filter

#Set the policy
$policy = Set-AzStorageAccountManagementPolicy -ResourceGroupName $rgname -StorageAccountName $accountName -Rule $rule1

如果您在编写/执行上述代码时出现问题,请告诉我。

【讨论】:

  • 谢谢,所以效果很好,但我不得不做一些工作。这就是问题所在。我在 Azure DevOps 中使用 YAML Pipeline 进行自动化部署。即使我为所有脚本和 Pwsh 定义了一个容器,变量也不会持续存在。我不得不将上述所有 pwsh 放在一行中。任何想法如何解决这个问题?
  • @Picsou,抱歉,我没有使用 azure devops。你最好发布一个关于这个的新问题。
猜你喜欢
  • 2021-08-14
  • 2019-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
  • 2022-01-26
  • 1970-01-01
  • 2021-04-18
  • 2014-07-18
相关资源
最近更新 更多