1
votes

So I'm using terraform for azure provider in order to deploy my infrastructure. I just can't seem to be able to define the storage lifecycle. I'd like to add something like this, which i have found, but is not available as is.

So i've tried this https://github.com/terraform-providers/terraform-provider-azurerm/issues/3316, and i've look all over. I'm certain there's a way of telling azure to enable the lifecycle tiertoarchive and tiertodelete… Just can't seem to figure it out.

Thanks

What i'm looking for:

*the resource azurerm_storage_management_policy is a made up resource.

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

1
I'm not sure if you're asking for a way to do this using terraform, or a way to do this aside from terraform? Because tf doesn't have this feature right now, so you're always going to have to do it in the portal, or using powershell or such things on the side until tf does support it. After which you can import it.Marco
Ah okay, I was hoping for a work around using terraform. Is there a way of doing it with PowerShell? I could run a script for it? perhaps. I'm a little new to all of this Infrastructure as CodePicsou

1 Answers

1
votes

I see in your comment, you ask for powershell to do that. Then yes, it's possible via powershell as per this doc.

The sample code from the doc(you can modify it to meet your need) works for me, and note that you should install azure powershell az module before run the script:

#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

Please let me know if you issues when write / execute the code above.