Mastering Azure cost optimisation: A Complete Guide

Yazan Kassam, DevOps Engineer

10 min read

.

July 23, 2024

DevOps

Microsoft Azure logo with text 'cloud cost optimisation

Cost optimisation in the cloud is crucial to efficiently manage resources and control spending. Within the Azure cloud platform, services such as Azure Function, Web App, Azure Container Registry (ACR), and Azure Bastion are key components for resource management.

However, the costs associated with these services can quickly escalate, impacting overall project budgets and resource allocation.

This article will delve into Azure cost optimisation strategies to help you manage and reduce your Azure expenses. It explores the general and specific strategic approaches for optimising costs and enhancing deployment efficiency within Azure environments.

Importance of Cloud Cost Optimisation

Cost optimisation is vital for successful software development in the cloud, especially when using premium-tiered server-based services.

By enhancing cost management in the cloud, businesses can achieve the following benefits:

Better resource utilisation

Cloud services enable businesses to dynamically scale resources as per demand, resulting in improved resource utilisation and significant cost savings.

Enhanced Performance

Cost optimisation ensures efficient resource allocation, boosting performance and productivity. This is accomplished through strategic scaling, right-sizing, and prioritising critical workloads.

Improved financial outcomes

Effective cost optimisation helps companies reduce expenses, lower overall cloud service spending, and boost their Return on Investment (ROI).

Understanding Azure Cost Management: Key Strategies for Savings

Effective cost management is crucial for optimising your Azure cloud expenditure. Azure offers several strategies to help you manage and reduce costs while maximising the value of your investments. Here’s a closer look at key strategies for cost management:

Reserved plan

Azure’s Reserved Plan is an excellent way to achieve substantial savings. By committing to a longer-term reservation (1 to 3 years), you can save up to 30% on resource costs. This approach is particularly beneficial for predictable workloads and can significantly reduce overall spending on computing resources.

Saved plan

If you require more flexibility while still seeking cost savings, Saved Plan might be the right choice. This option provides a 15% discount on long-term reservations and allows you to adjust your resources as needed. It is ideal for environments where resource requirements may evolve but you still want to take advantage of lower costs.

Hybrid benefits

Leverage your existing licences with Azure’s Hybrid Benefits to further reduce costs. If you have Windows or SQL Server licences, you can apply them to your Azure Virtual Machines or Azure SQL Databases. This can lead to significant savings on licensing fees, making it a valuable option for enterprises with existing Microsoft software investments.

Credit-based subscription

For development and testing purposes, Azure offers credit-based subscriptions such as the Dev/Test subscription for Visual Studio Enterprise licence holders or Sponsorship Subscriptions. These options provide a set amount of credits to use, which can help reduce costs for non-production environments. However, note that these subscriptions do not come with SLAs, so they may not be suitable for all scenarios.

Optimising Azure Function Cost: Best Practices

Azure Functions logo – a serverless compute service enabling event-driven code execution without server management

Azure Functions, a serverless computing service, allows you to run code in response to events without managing servers. To maximise cost efficiency with Azure Functions, consider the following best practices:

Consumption plan

Azure Functions offers various pricing plans. At AlphaApps, we switched from a Premium plan to a Consumption plan to save costs. This required refactoring our application from a dockerized format to a zipped app. Despite the Consumption plan’s lower memory (1.5 GB vs. 3.5 - 14 GB in the Premium plan), thorough QA confirmed it meets our needs effectively, leading to significant cost savings.

Shared storage accounts

Azure Functions often need to store temporary data or maintain state. Utilising an existing Azure Storage Account, specifically tailored for your serverless applications, can help minimise expenses. By sharing a storage account across multiple functions, you can optimise costs associated with data storage and management.

Shared function app

A Function App provides shared infrastructure for multiple serverless Azure functions. To optimise costs, deploy functions with similar runtimes and versions within the same Function App. Note that a single Function app cannot be used for functions with different languages or runtimes (e.g., C# and Python) simultaneously. Proper planning and organisation can help you make the most of the shared infrastructure, reducing overall costs.

Optimising Azure Web App Cost: Best Practices

Azure Web App logo – a fully managed platform for building, deploying, and scaling web applications quickly and efficiently without managing infrastructure

When managing a web app, it is important to consider the costs associated with the Web App Plan rather than just the Web App itself. The Web App Plan is a critical resource that determines the CPU, Memory, and Network capabilities available.

To maximise the benefits of this service at the most cost-effective rate, it is helpful to keep the following points in mind:

Shared Web App Plan

This feature can be helpful when you have multiple applications that need to run on the same infrastructure. This can reduce costs because the Web App Plan serves multiple Web Apps instead of just one.

Automatic Scale Out Feature

Instead of manual scaling, automatic scale out makes more sense as it provides high availability and stability for your app. This feature is rule-based on metrics that determine when the app is consuming too many resources.

In our case, the metric that the Scale Out feature relies on is the number of Active Messages in the Service Bus Queue. Based on this metric, the feature will increase or decrease the count of instances as needed to ensure optimal performance with cost reduction efficiently.

Optimising Azure Container Registry Cost: Best Practices

Azure Container Registry logo – a managed Docker container registry service for storing and managing container images

Consider these steps to optimise ACR for your workloads. Here's how they can contribute to cost efficiency:

Consolidating registries

By consolidating multiple ACR instances into fewer registries, you can reduce costs. It reduces management overhead and administrative costs in maintaining multiple registries.

Tagging strategy with prefixes

Implementing a clear tagging strategy (e.g., dev-, prod-) helps in the efficient management and deployment of versions across different environments.

Optimising Dockerfiles and image size

Using lightweight base images and multi-build stages in Dockerfiles reduces the size of Docker images stored in ACR. Smaller image sizes not only improve deployment speed but also reduce storage costs within ACR, as Azure charges for the storage space used by container images.

ACR tasks for automated image cleanup

Setting up automated tasks to clean up old and unused images helps optimise storage usage in ACR. By removing outdated versions or unnecessary images according to a defined schedule, you can prevent unnecessary storage costs from accumulating over time.

Optimising Azure Bastion Service for better cost management

Azure Bastion logo – a managed service providing secure and seamless RDP and SSH access to virtual machines without exposure to the public internet

Optimising Azure Bastion service is essential for managing costs while ensuring secure VM access. Here are some key methods to achieve cost efficiency:

Number of Bastion instances

Utilise Peer VNets to connect different virtual networks together. By using this feature, you can avoid the need to create a separate Bastion for each VNet, reducing unnecessary expenses.

Optimise Uptime for attached VMs

Deallocated VMs help avoid non-essential costs. For instance, when a Jumpbox VM is no longer needed to access DB servers in Production, its costs can be managed by stopping it.

Automate Lifecycle of Bastion Management:

Automating Bastion service management is crucial for cost reduction. Here's how you can achieve this:

  1. Build ARM Template: Deploy ARM templates to automate the initial setup of the Azure Bastion Service, as manual creation can be time-consuming.
  2. Implement Scheduled Pipeline Triggers: Utilise scheduled pipeline triggers (e.g. GitLab CI) to automate the initialisation and deletion processes during working hours.

In conclusion, optimising cloud costs is a critical action, especially when dealing with sensitive environments such as Staging and Production. The key lies in balancing cost optimisation while achieving high performance and secure principles. In Azure Cloud, general strategies, such as reserved plans, saving plans, hybrid benefits, and credit-based subscriptions can help.

Additionally, consolidating Azure Container Registry (ACR) registries, automating Bastion creation with an IaC ARM template, using the Consumption plan for Azure Functions, and leveraging the Scale Out feature in Web Apps contribute to improved cost management and security.

FAQS

1. How can we identify services that need cost enhancement and can make a significant impact?

Azure’s Cost Analysis dashboard helps monitor and identify high-cost services across subscriptions and resources. It highlights areas for savings, forecasts expenses, and offers optimisation recommendations through Azure Advisor. Using these tools, you can pinpoint which services need cost enhancement and measure the impact of cost-saving measures.

2. What is the best way to determine the appropriate min/max values for a scale out rule in a Web App?

You can select the appropriate min/max scale values based on your logic app and the plan that you choose. In this context, conducting a load test is an essential step to gather useful insights and determine the proper values.

3. What is the reason for not continuing to use Docker with Azure Functions in a consumption plan?

Azure Functions does not support running dockerized applications in Consumption plans to save costs. Containerised function apps can only be deployed in Elastic Premium or Dedicated plans. Therefore, we switched to zip deployment to utilise the Consumption plan, which offers pay-as-you-go pricing.

4. How can I ensure that tasks in ACR purge old images based on the environment for each service without overlapping deletions?

To prevent overlapping deletions, ACR should have specific purge tasks for each service and environment within the registry to remove old images.

Let's say your service name is "ocr" and the environment is "dev." It needs to clear its old images every day while keeping the latest 4 images. The deleted images must be at least 3 days old. The command should be as follows:

5. What are the benefits of optimising ACR using Task instead of retention policy?

Retention policy feature is applicable for the upper-tier Premium that comes with a higher cost. Above all when a retention policy is enabled, ONLY untagged manifests in the registry are automatically deleted after a set number of days, but tagged images will not be deleted even if they are too old.