Yazan Kassam, DevOps Engineer
10 min read
.
July 23, 2024
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.
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:
Cloud services enable businesses to dynamically scale resources as per demand, resulting in improved resource utilisation and significant cost savings.
Cost optimisation ensures efficient resource allocation, boosting performance and productivity. This is accomplished through strategic scaling, right-sizing, and prioritising critical workloads.
Effective cost optimisation helps companies reduce expenses, lower overall cloud service spending, and boost their Return on Investment (ROI).
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:
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.
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.
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.
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.
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:
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.
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.
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.
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:
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.
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.
Consider these steps to optimise ACR for your workloads. Here's how they can contribute to cost efficiency:
By consolidating multiple ACR instances into fewer registries, you can reduce costs. It reduces management overhead and administrative costs in maintaining multiple registries.
Implementing a clear tagging strategy (e.g., dev-, prod-) helps in the efficient management and deployment of versions across different environments.
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.
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 is essential for managing costs while ensuring secure VM access. Here are some key methods to achieve cost efficiency:
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.
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.
Automating Bastion service management is crucial for cost reduction. Here's how you can achieve this:
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.
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.
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.
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.
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:
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.