Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add Blob lifecycle rule to ARM template

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 !

like image 393
Joy Avatar asked May 18 '20 14:05

Joy


1 Answers

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
                                        }
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    ]
}
like image 95
ajakuszyk Avatar answered Oct 23 '22 15:10

ajakuszyk