Apply these 8 FinOps Practices to Optimize Your Azure Cloud Spending

Apply these 8 FinOps Practices to Optimize Your Azure Cloud Spending

June 11, 2023
Get tips and best practices from Develeap’s experts in your inbox

Introduction

In the rapidly evolving world of IT, organizations heavily depend on cloud services like Microsoft Azure. With its comprehensive suite of services, including computing power, storage options, data analytics, and networking capabilities, Azure’s scalability, flexibility, and adaptability are key to meeting diverse business needs.

However, cost and resource optimization, critical for efficient cloud service management, are often overlooked. Without proper Financial Operations (FinOps) practices, escalating costs and resource misuse can quickly become a significant burden. Imagine large, under-managed virtual machines constantly running in Azure for application development. The cloud meter keeps running, leading to avoidable, hefty expenses over time.

In this article, we will delve into the importance of cost and resource optimization, particularly highlighting the role of FinOps. We will also share some essential practices to help you manage your Azure Cloud services more economically and effectively.

Are you ready to learn how to slash your cloud spending significantly?

Understanding FinOps

Before we dive deep into the subject, let’s briefly discuss what FinOps is. FinOps, short for Financial Operations, is a set of practices and principles aimed at bringing financial accountability to the variable spend model of cloud technology. This means improving an organization’s financial performance by optimizing its spending on cloud services. While our focus is on Azure, it’s crucial to remember that FinOps can be applied to any cloud service. In a world where cloud computing services can quickly become costly if not managed effectively, a sound understanding and effective implementation of FinOps are paramount.

By applying the FinOps methodology, we can unite an organization’s finance, operations, and engineering teams to manage cloud costs collaboratively. The goal is to optimize cloud usage without compromising the desired level of performance and availability. With proper understanding and management of FinOps, businesses can avoid unnecessary costs, inefficient resource allocation, and difficulty in financial planning, thus maintaining control over cloud expenditure.

Now, let’s look at some practical FinOps practices that can help you keep your Azure spending in check.

Cloud Costs and Resource Optimization

As we navigate the extensive world of Azure, the importance of optimizing cloud costs and resource utilization becomes apparent. This section will look at various techniques that can significantly help optimize your Azure cloud costs.

1. Set up Virtual Machines Auto-shutdown and Auto-start

Consider this: Have you ever considered how much you could save by automating VM shutdowns during off-peak hours?

With Azure Functions, you can create time-based triggers that execute scripts to start or stop VMs based on your organization’s usage patterns, leading to substantial cost savings.

Using this method, I configured the unnecessary virtual machines to run outside of working hours to shut down at 6:00 pm and restart at 7:00 the next morning, excluding weekends.

I used the following PowerShell code for the functions, inspired by Charbel Nemnon:

# Input bindings are passed in via param block.
param($Timer)


# Add all your Azure Subscription Ids below
$SubscriptionIds = @"
   ["<your-subscription1-id>", "your-subscription2-id", "your-subscription3-id"]
"@ | ConvertFrom-Json


# Convert UTC to West Europe Standard Time zone
$Date = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::Now,"<your-time-zone>")


foreach ($SubscriptionId in $SubscriptionIds) {


   # Selecting Azure Subscription
   Set-AzContext -SubscriptionId $SubscriptionId | Out-Null


   $CurrentSub = (Get-AzContext).Subscription.Id
   if ($CurrentSub -ne $SubscriptionId) {
       Throw "Could not switch to SubscriptionID: $SubscriptionId "
       Exit 1
   } else {
     Write-Host "Setting Azure context to subscription: $($SubscriptionId)`n"
   }


   # Getting all VMs that are tagged with
   # "AutoStart", "AutoShutdown", and "Weekend"
   $Vms = Get-AzVM -Status | Where-Object {
     ($_.tags.AutoShutdown -ne $null) `
     -and ($_.tags.AutoStart -ne $null) `
     -and ($_.tags.Weekend -ne $null)
   }
   $Now = $Date


   # Managing VMs according to the tags assigned to them
   foreach ($Vm in $Vms) {


       if (($Vm.PowerState -eq 'VM running') `
       -and ($Now-gt $(get-date $($Vm.tags.AutoShutdown))) ) {
           Stop-AzVM -Name $Vm.Name -ResourceGroupName $Vm.ResourceGroupName `
           -Confirm:$false -NoWait -Force
           Write-Warning "Stop VM - $($Vm.Name)"
       }
       elseif (($Vm.PowerState -ne 'VM running') `
       -and ($Vm.tags.Weekend -eq 'Off') `
       -and ($Date.dayofweek.value__ -in 1..5 ) `
       -and ($Now -gt $(get-date $($Vm.tags.AutoStart))) `
       -and ($Now -lt $(get-date $($Vm.tags.AutoShutdown))) ) {
           Start-AzVM -Name $Vm.Name `
           -ResourceGroupName $Vm.ResourceGroupName -NoWait
           Write-Warning "Start VM - $($Vm.Name)"
       }
       elseif (($Vm.PowerState -ne 'VM running') `
       -and ($Vm.tags.Weekend -eq 'On') `
       -and ($Now -gt $(get-date $($Vm.tags.AutoStart))) `
       -and ($Now -lt $(get-date $($Vm.tags.AutoShutdown))) ) {
           Start-AzVM -Name $Vm.Name `
           -ResourceGroupName $Vm.ResourceGroupName -NoWait
           Write-Warning "Start VM - $($Vm.Name)"
       }


   }
  
}


Exit 0

You just need to update the “$SubscriptionIds” variable with your subscription(s) ID(s), update “$Date” variable with your timezone, as well as apply “AutoStart”, “AutoShutdown”, and “Weekend” tags on your desired VMs (with the values “HH:MM” for AutoStart and AutoShutdown, and “On” or “Off” for Weekend).

As in the following screenshot:

“Tags” section under Virtual Machine pane in Azure Portal; an example of how the tags for the Azure Function should look like

2. Delete Old Snapshots in an Automated Way

Did you know that not only does storing outdated snapshots consume valuable storage space, but it also incurs additional costs?

By automating the deletion of snapshots older than a specific duration (e.g., 90 days), you can manage your storage costs efficiently. For example, I have created a timer-based Azure Function for deleting old snapshots, similar to VM Start/Stop function mentioned above. It is triggered every day at midnight UTC. The Function App may be written in PowerShell as below:

# Input bindings are passed in via param block.
param($Timer)


# Variables section
$MaxAge = 90
$CurrentDate = (Get-Date).ToUniversalTime()
$SubscriptionIds = @"
 ["<your-subscription1-id>", "your-subscription2-id", "your-subscription3-id"]
"@ | ConvertFrom-Json


foreach ($SubscriptionId in $SubscriptionIds) {


 # Selecting Azure Subscription
 Set-AzContext -SubscriptionId $SubscriptionId | Out-Null


 $CurrentSub = (Get-AzContext).Subscription.Id
 if ($CurrentSub -ne $SubscriptionId) {
     Throw "Could not switch to SubscriptionID: $SubscriptionId"
     Exit 1
 } else {
   Write-Host "Setting Azure context to subscription: $($SubscriptionId)`n"
 }


 # Getting all the subscription's snapshots
 $Snapshots = Get-AzSnapshot | Where-Object {
   ($CurrentDate - $_.TimeCreated).TotalDays -gt $MaxAge
 }


 # Deleting all the snapshots older than the
 # number of days defined in "$MaxAge" variable
 foreach ($Snapshot in $Snapshots) {


   Write-Host "Deleting snapshot: $($Snapshot.Name), `
   created at $($Snapshot.TimeCreated)`n"
   Remove-AzSnapshot -ResourceGroupName $Snapshot.ResourceGroupName `
   -SnapshotName $Snapshot.Name -Force
   Write-Host "Snapshot: $($Snapshot.Name) has now been deleted."


 }


 Write-Host "All snapshots older than $($MaxAge) days `
 in subscription $($SubscriptionId) have been deleted."


}


Exit 0

In this example, the script will evaluate all Azure Subscription IDs provided in “$SubscriptionIds” list variable, searching for all Disk Snapshots older than the number of days provided in “$MaxAge” variable (in this case 90 days), and remove these snapshots.

3. Optimize Your Virtual Machines and Disks Size

Paying attention to VM and disk sizes is crucial. Do you know how much CPU and Memory your Virtual Machine actually needs and how much is being wasted due to mere miscalculation?

The same goes for disk performance. Opt for standard SSD or HDD storage options instead of Premium SSD so you can reduce costs without compromising performance.

In short, adjust the VM or disk size to meet your specific requirements. By regularly evaluating these parameters, you ensure that you only pay for the resources you actually use.

4. Buy out Reservations for Your Computing Instances

Are you aware that reservation pricing can lead to substantial savings for predictable workloads? If you pre-purchase reserved instances of SQL databases or VMs, you can benefit from discounted rates compared to pay-as-you-go pricing.

Make an internal resource audit and assess your organization’s workload patterns; then, reserve the heavily used types of resources for 1 or 3 years and reap the fruit of your optimized costs over the long term.

5. Scale Your Kubernetes Cluster Properly

Did you know you can reduce cluster costs in half? All you need to do is apply cluster autoscaling in your Kubernetes cluster(s).For example, you can use spot instances as cluster nodes to take advantage of unused computing capacity at lower prices. You can also just buy out a similar ready-to-go service from Cast.ai whose automated scaling solutions will help you optimize your resource usage and costs.

6. Clean up and Prune Resources

Regularly review your subscription(s) and prune unnecessary resources, thereby getting rid of extra costs.

Create a resource inventory, implement a tracking system, and identify unused or underutilized resources. Make sure you make informed decisions about resource allocation and cost optimization.

7. Manipulate Log Analytics and Application Insights Log Levels

Reduce the costs of collecting and retaining logs by keeping log levels for Log Analytics or Application Insights fine-tuned.

While you manage your logs properly, you ensure that only the essential logs are stored, thereby minimizing storage and processing expenses.

8. Track Your Costs in a Visually Readable Manner

Are you finding it hard to identify cost centers and responsible parties? Enforcing a policy that requires key tags on all resource groups in your subscription(s) can make cost management and reporting a breeze.

Other than that, take a while to build a set of readable and varied dashboards that will empower you to see the areas in which your spending is specifically located.

The above will facilitate your decisions to be driven by actual data.

Conclusion

Embracing good FinOps practices is key to optimizing your Azure costs and building a healthier, more efficient organization.

Here are the main takeaways:

  1. Use VM Auto-shutdown and Auto-start to avoid paying for idle resources;
  2. Regularly delete old snapshots to manage storage costs efficiently;
  3. Evaluate your VM and disk sizes once in a while to ensure you only pay for resources you actually use;
  4. Utilize reservation pricing for predictable workloads to access discounted rates;
  5. Apply cluster autoscaling in your Kubernetes cluster(s) to optimize resource usage;
  6. Prune unnecessary resources on a timely basis to manage costs effectively;
  7. Fine-tune your Log Analytics and Application Insights log levels to manage storage and processing expenses;
  8. Use key tags on all resource groups to simplify cost management and reporting.

But remember, these are just the starting point. The journey to cost optimization and better resource management is a continuous one. I encourage you to start implementing these strategies and experience the benefits they bring.

By applying the strategies provided in this article, you can unlock the potential for sustainable growth, improved resource management, and better cost efficiency. When you actively pursue FinOps practices, not only will you adhere to the Continuous Improvement principle but also make a significant contribution to the overall development of your organization.

Are you ready to take the first step towards financial growth and a healthier, more efficient organization? Good luck!

We’re Hiring!
Develeap is looking for talented DevOps engineers who want to make a difference in the world.