Efficient cost management and FinOps have become one of the top priorities for public cloud customers across all industries. One of the main reasons for cloud budget overruns is lack of visibility into a volume’s lifecycle, which leads to the presence of orphaned and unused resources. These overlooked resources are contributed to a company cloud bill, and users don’t even expect that they’re paying for them.
After our blog post Reduce your AWS bill by cleaning orphaned and unused disk snapshots we’ve got many questions on how it can be applied to cloud cost optimization goals to other public clouds, such as Microsoft Azure and Alibaba Cloud?
Let’s explore how we can perform the similar cleanup operations on them.
Cleaning up MS Azure snapshots
The process of finding unused snapshots in Azure can be performed applying the following criteria to the list of snapshots:
- snapshot is older than certain date
- the source disk from which the snapshot is created doesn’t exist
Let’s use Azure Cloud Shell in Bash mode to find such snapshots.
Making a list of old snapshots (you can set date threshold according to your needs):
OLD_SNAPSHOTS=$(az snapshot list --query "[?timeCreated <= '2021-01-01T00:00:00.000000+00:00'].{DiskId:creationData.sourceResourceId,Id:id}" --output table | tail -n +3)
Listing disks:
USED_DISKS=$(az disk list --query "[].{Id:id}" --output table | tail -n +3)
Filtering out snapshots which still have source disk:
UNUSED_SNAPSHOTS=()
IS_UNUSED_SNAPSHOT=false
IS_DISK_ID=true
for RESOURCE_ID in $OLD_SNAPSHOTS
do
if $IS_DISK_ID
then
if [[ ! "$USED_DISKS[*]" =~ $RESOURCE_ID ]]
then
IS_UNUSED_SNAPSHOT=true
fi
IS_DISK_ID=false
else
if $IS_UNUSED_SNAPSHOT
then
UNUSED_SNAPSHOTS+=($RESOURCE_ID)
fi
IS_UNUSED_SNAPSHOT=false
IS_DISK_ID=true
fi
done
echo "Detected the following unused snapshots: $UNUSED_SNAPSHOTS"
Once you have them listed and verify that you really don’t need them, you can easily clean them up via a single command:
az snapshot delete --ids "${UNUSED_SNAPSHOTS[@]}"
Free cloud cost optimization. Lifetime
Cleaning up snapshots in Alibaba Cloud
In Alibaba Cloud, Snapshot properties has one more useful value which allows us to put it as criterion for filtering – field “Usage” which is empty if this Snapshot is not used as a source for some disk or image.
Having that, here is how you can detect unused Snapshots using Alibaba Cloud Shell.
Making a list of old snapshots (you can set date threshold according to your needs):
OLD_SNAPSHOTS=$(aliyun ecs DescribeSnapshots --pager --Usage none --Filter.2.Key CreationEndTime --Filter.2.Value 2021-01-01T00:00Z --output cols=SnapshotId,SourceDiskId rows=Snapshots.Snapshot[] | tail -n +3 | sed 's/ | /,/g')
Listing disks:
USED_DISKS=$(aliyun ecs DescribeDisks --pager --output cols=Disks.Disk[].DiskId | tail -n +3 | sed 's/[//' | sed 's/]//')
Filtering out snapshots which still have source disk:
for SNAP_INFO in $OLD_SNAPSHOTS
do
SNAPSHOTID=$(cut -d, -f1 <<< $SNAP_INFO)
DISKID=$(cut -d, -f2 <<< $SNAP_INFO)
if [[ ! "$USED_DISKS[*]" =~ $DISKID ]]
then
echo "Detected snapshot - $SNAPSHOTID"
# aliyun ecs DeleteSnapshot --SnapshotId=$SNAPSHOTID
fi
done
Please note the commented line in the last snippet. You’ll need to uncomment it once you verify the list of detected unused snapshots and ready to clean them up
The ways of identifying and cleaning up orphaned snapshots described above will help your company keep MS Azure and Alibaba Cloud costs under control, and implement best practices of FinOps methodology by providing your IT team with more visibility into cloud resources.
Find best practices + helpful tips on how to tag cloud resources in MS Azure → https://hystax.com/ms-azure-tagging-best-practices.