Bicep & Storage Account Lifecycle Management
Tags: bicep, azure, iac
Published: 2022-10-26 14:13:15

In this post, I would like to show how you can configure the Lifecycle Management rules in an Azure Storage Account using Bicep.
Before reading this post, I suggest you to read [this article](https://learn.microsoft.com/en-us/azure/storage/blobs/lifecycle-management-policy-configure?tabs=azure-portal) to understand what lifecycle management rules are and how you can use them to control costs in a storage account.

To set a lifecycle management rule, you can open the Storage Account blade in Azure Portal and select **"Lifecycle management"** blade, and then **"Add Rule"** command:


![The lifecycle management blade](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jnp5oe870us0zlbba8n6.png)

The wizard for a rule definition is composed by three steps. In the first step, you define the rule name, what kind of blobs you want to manage (block or append or both), and if the rule will be applied only to the base blob or also to the versions and/or the snapshots.


![The rule details form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2vpfmhbnhuslq5ygn78.png)

You can also set a filter that will be used to select only particular blobs in the storage account. With this property, you can, for example, limit the rule to a particular container or to a specific type of blob (e.g. JPG, GIF, and so on).

The next step allows you to configure how the rule will "move" the blobs between the different access tiers. Remember that the rule moves blobs from a more expensive tier to a less expensive tier. Furthermore, the "move" is a virtual move, the rule does not physically move the blob but simply changes its tier.

You can define when a blob will move to cool, to archive or will be deleted based on the creation datetime or the update datetime as shown in the following figure:

![The rule definition step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n4bajmkvgfscgwmel08p.png)

In the previous sample, a blob will be moved from hot tier to cool tier if it is not changed for more than 10 days, then will be moved to the archive tier if it is not changed for more that 100 days, and, finally, will be deleted if it is not changed for more that 300 days.

You can define more than one rules, and, behind the scene, all the rules are described in a JSON. For example, the JSON for the previous rule is the following:

```json
{
  "rules": [
    {
      "enabled": true,
      "name": "Rule Name",
      "type": "Lifecycle",
      "definition": {
        "actions": {
          "baseBlob": {
            "tierToCool": {
              "daysAfterModificationGreaterThan": 10
            },
            "tierToArchive": {
              "daysAfterLastTierChangeGreaterThan": 7,
              "daysAfterModificationGreaterThan": 100
            },
            "delete": {
              "daysAfterModificationGreaterThan": 300
            }
          }
        },
        "filters": {
          "blobTypes": [
            "blockBlob"
          ]
        }
      }
    }
  ]
}
```

If you want to create a rule using a Bicep template, you must define a resource of type `Microsoft.Storage/storageAccounts/managementPolicies` with the storage account as parent.

```
resource analysisResultRule 'Microsoft.Storage/storageAccounts/managementPolicies@2021-09-01' = {
  name: 'default'
  parent: storageAccount
  properties: {
    policy: {
      rules: [
        {
          enabled: true
          name: 'Rule Name'
          type: 'Lifecycle'
          definition: {
            actions: {
              baseBlob: {
                tierToCool: {
                  daysAfterModificationGreaterThan: 10
                }
                tierToArchive: {
                  daysAfterLastTierChangeGreaterThan: 7
                  daysAfterModificationGreaterThan: 100
                },
                delete: {
                  daysAfterModificationGreaterThan: 300
                }
              }
            }
            filters: {
              blobTypes: [
                'blockBlob'
              ]
            }
          }
        }
      ]
    }
  }
}
```

You just put the JSON you find in the portal inside the `policy` node of the BICEP.
The policy definition is a child of the storage account resource definition (the `parent` property contains the symbolic name of the storage resource). You can define it in different ways, you can read [this article](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/child-resource-name-type) to find all the possible ways to declare.