6
votes

I have an already created ARM template based on my existing resource group,

recently I added a new configuration to my blob storage in my account storage, I needed to manage its life cycle, which was fortunately available on the azure portal by adding a rule :

enter image description here

or by adding this json code:

{
  "rules": [
    {
      "name": "ruleFoo",
      "enabled": true,
      "type": "Lifecycle",
      "definition": {
        "filters": {
          "blobTypes": [ "blockBlob" ],
          "prefixMatch": [ "container1/foo" ]
        },
        "actions": {
          "baseBlob": {
            "tierToCool": { "daysAfterModificationGreaterThan": 30 },
            "tierToArchive": { "daysAfterModificationGreaterThan": 90 },
            "delete": { "daysAfterModificationGreaterThan": 2555 }
          },
          "snapshot": {
            "delete": { "daysAfterCreationGreaterThan": 90 }
          }
        }
      }
    }
  ]
}

but what's not clear to me is in which part of my blob service section

{
            "type": "Microsoft.Storage/storageAccounts/blobServices",
            "apiVersion": "[variables('storageAccount_version')]",
            "name": "[concat(variables('storageAccount_name'), '/default')]",
            "tags": {
                "displayName": "Storage Account - Blob Service"
            },
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount_name'))]"
            ],
            "properties": {
                "cors": {
                    "corsRules": []
                },
                "deleteRetentionPolicy": {
                    "enabled": false
                }
            }
        },

I would appreciate any help! thanks !

2
How does it look once you add rules you want and click on "export template"?LMG

2 Answers

6
votes

The following template creates storage account and it's blob lifecycle.

The key is to name the lifecycle resource with storage account name prefix and add the dependsOn section.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageAccountName": {
            "type": "string",
            "defaultValue": "ajstoragetest444"
        }
    },
    "resources": [
        {
            "name": "[parameters('storageAccountName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "apiVersion": "2019-06-01",
            "location": "westeurope",
            "sku": {
                "name": "Standard_LRS",
                "tier": "Standard"
            },
            "kind": "StorageV2",
            "properties": {
            }
        },
        {
            "name": "[concat(parameters('storageAccountName'), '/default')]",
            "type": "Microsoft.Storage/storageAccounts/managementPolicies",
            "apiVersion": "2019-06-01",
            "dependsOn": [
                "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
            ],
            "properties": {
                "policy": {
                    "rules": [
                        {
                            "name": "ruleFoo",
                            "enabled": true,
                            "type": "Lifecycle",
                            "definition": {
                                "filters": {
                                    "blobTypes": [
                                        "blockBlob"
                                    ],
                                    "prefixMatch": [
                                        "container1/foo"
                                    ]
                                },
                                "actions": {
                                    "baseBlob": {
                                        "tierToCool": {
                                            "daysAfterModificationGreaterThan": 30
                                        },
                                        "tierToArchive": {
                                            "daysAfterModificationGreaterThan": 90
                                        },
                                        "delete": {
                                            "daysAfterModificationGreaterThan": 2555
                                        }
                                    },
                                    "snapshot": {
                                        "delete": {
                                            "daysAfterCreationGreaterThan": 90
                                        }
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    ]
}
2
votes

Please take a look at this article, and the example as below:

{
  "name": "default",
  "type": "Microsoft.Storage/storageAccounts/managementPolicies",
  "apiVersion": "2019-04-01",
  "properties": {
    "policy": {
      "rules": [
        {
          "enabled": "boolean",
          "name": "string",
          "type": "Lifecycle",
          "definition": {
            "actions": {
              "baseBlob": {
                "tierToCool": {
                  "daysAfterModificationGreaterThan": "number"
                },
                "tierToArchive": {
                  "daysAfterModificationGreaterThan": "number"
                },
                "delete": {
                  "daysAfterModificationGreaterThan": "number"
                }
              },
              "snapshot": {
                "delete": {
                  "daysAfterCreationGreaterThan": "number"
                }
              }
            },
            "filters": {
              "prefixMatch": [
                "string"
              ],
              "blobTypes": [
                "string"
              ]
            }
          }
        }
      ]
    }
  }
}