Managing Storage Lifecycle in Azure
Requirements
The Nautilus DevOps team needs to optimize data retention costs by automating the deletion of old blobs. They plan to implement Blob Lifecycle Management for a specific container in Azure Storage.
Task
- Create a Storage Account:
- Name the storage account
devopsstor14097. - Set the region to East US.
- Use Locally-redundant storage (LRS) as the redundancy option.
- Create a Blob Container:
- Name the container
devops-container14097.
- Upload a File to the Container:
- Upload the file named
tempfile.txtto the container. The file is present under/rootof the client host.
- Configure Blob Lifecycle Management:
- Apply a Lifecycle Management rule named
devops-del-ruleto the containerdevops-container14097to delete blobs after7days of last modification.
- Validation:
- Verify that the Lifecycle Management rule named
devops-del-ruleis correctly applied.
Note
This lab uses Azure CLI to create storage resources and apply a lifecycle policy in a single, reproducible flow.
Prerequisites
- Azure CLI installed and logged in (
az login). - Access to the target subscription and resource group.
- File
/root/tempfile.txtpresent on the client host.
Steps
RG=$(az group list --query "[?contains(name, 'kml')].name" --output tsv)
STORAGE_ACCOUNT="devopsstor14097"
CONTAINER_NAME="devops-container14097"
LOCATION="eastus"
RULE_NAME="devops-del-rule"
FILE_PATH="/root/tempfile.txt"
az storage account create \
--name "$STORAGE_ACCOUNT" \
--resource-group "$RG" \
--location "$LOCATION" \
--sku Standard_LRS \
--kind StorageV2
az storage container create \
--name "$CONTAINER_NAME" \
--account-name "$STORAGE_ACCOUNT" \
--auth-mode login
az storage blob upload \
--account-name "$STORAGE_ACCOUNT" \
--container-name "$CONTAINER_NAME" \
--name tempfile.txt \
--file "$FILE_PATH" \
--auth-mode login
cat > lifecycle.json <<EOF
{
"rules": [
{
"enabled": true,
"name": "${RULE_NAME}",
"type": "Lifecycle",
"definition": {
"actions": {
"baseBlob": {
"delete": {
"daysAfterModificationGreaterThan": 7
}
}
},
"filters": {
"blobTypes": ["blockBlob"],
"prefixMatch": ["${CONTAINER_NAME}/"]
}
}
}
]
}
EOF
az storage account management-policy create \
--account-name "$STORAGE_ACCOUNT" \
--resource-group "$RG" \
--policy @lifecycle.json
Verification
- Confirm container exists:
az storage container show --name "$CONTAINER_NAME" --account-name "$STORAGE_ACCOUNT" --auth-mode login --query name -o tsv
- Confirm blob upload:
az storage blob show --account-name "$STORAGE_ACCOUNT" --container-name "$CONTAINER_NAME" --name tempfile.txt --auth-mode login --query name -o tsv
- Confirm lifecycle rule:
az storage account management-policy show --account-name "$STORAGE_ACCOUNT" --resource-group "$RG" --query "policy.rules[0].name" -o tsv