Wednesday, October 16, 2024

Optimizing Cost Management for Azure Functions: Planning, Control, and Best Practices

Azure Functions is a serverless, fully managed compute platform that enables seamless process automation. However, its rapid feature deployment can sometimes lead to governance misalignment, which may impact organizations in several ways, including increased costs.

In this article, I will share few strategies to help you effectively govern and optimize your costs.

1. Select the right hosting plan

There are many hosting plans you can select from.






Following is a very high level analysis.

Hosting Plan

Suitable Scenarios

Type

Cost

Flex Consumption  

Event-driven workloads, rapid scaling, VNET integration

Shared

Medium

Consumption

Cost effective serverless apps, infrequent & unpredictable workloads

Shared

Low

Functions Premium

High performance, longer execution times, VNET integration

Dedicated

High

App Service

Dedicated, integration with existing app service plans, VNET integration

Dedicated

High

Container Apps Environment

Containerized workloads, VNET integration

Dedicated

High


2. Optimize your code to minimize function execution time

Two major factors influencing your Azure Functions' costs are execution time and memory usage. Inefficient code can cause delays, leading to higher-than-expected expenses.

It is always recommended to test performance in your local development environment to identify inefficiencies early. Application Insights is a powerful tool for monitoring and analyzing performance, helping to pinpoint and optimize non-performing segments.


4. Optimize network traffic

It is highly recommended to monitor and manage outbound (egress) traffic from your Azure Functions. Here are some effective strategies to reduce egress costs:

  • If possible consume resources within the same region.
  • Cache & compress external API calls
  • Aggregate & batch data processing where possible

5. Monitor & analyze cost using Azure Cost Management

Regularly monitor Azure Cost Management & Billing to track and optimize your spending. Here are some effective strategies you can implement:

  • Create budgets in Azure Cost Management
  • Setup alerts

Tuesday, October 15, 2024

How to debug issues easily with Application Insights - How to see only my code

Let's assume we have a complex solution with multiple external integrations. Identifying issues can be challenging, as error messages are often intricate and difficult to interpret.

Consider the following example, where multiple integrations are in place.




















How can I check only my code to focus on what I can control? It’s simple—just select the "Just My Code" checkbox.

















Once I do that, I can identify the specific points in my code that triggered the error.


















Now, I understand the root cause of the problem and can explore possible solutions.

Dynamically Modify API Responses Based on Subscription Tier with Azure API Management Policies

Azure API Management (APIM) provides more than just API Gateway features; it offers a comprehensive suite of capabilities that strengthen the entire API ecosystem.

Here are some key features provided by Azure API Management:
  • API Gateway
  • Developer Portal
  • Policy Management
  • Analytics and Monitoring
  • Security Features
  • Multi-Cloud and Hybrid Support
  • Versioning and Revisioning
  • Scalability
  • Integration
  • Custom Domains and Branding
  • Products & Subscriptions
We can combine multiple features from these categories based on our specific requirements.

In this article, I will illustrate how to combine Policy Management features with Product & Subscription features to implement a specific use case. 

Following is my business case

I want to offer my COVID data API in two product tiers. Users who subscribed to the premium product would see the entire response including the death count. But, the users who subscribed to the basic product would see the response without the death count

Here is the approach I used to implement the solution:

First we need to create two products within our Azure API Management instance:
  • Starter – A basic plan where customers would see only a subset of the response
  • Unlimited – A premium plan where the customers would see the full response















Then let's create two subscriptions for those products using Subscriptions section

















Let's navigate to your API and enable access for both the Starter and Unlimited products.






















Next, navigate to the Outbound Processing section of the API operation and open the Policy Editor to enforce a conditional response based on the subscribed product.



















Here is the policy snippet I used:
    <outbound>
        <base />
        <choose>
            <when condition="@(context.Response.StatusCode == 200 && context.Product?.Name != "Unlimited")">
                <set-body>@{
                        var response = context.Response.Body.As<JObject>();
                        var rawData = response["rawData"] as JArray;
                        if (rawData != null) {
                            foreach (var record in rawData) {
                                record["Deaths"]?.Parent.Remove();
                            }
                        }

                        return response.ToString();
                    }</set-body>
            </when>
        </choose>
        <cache-store duration="15" />
    </outbound>
Let's test this with the Postman client. 

Let's first try with the Starter product. We will use the subscription key associated with the Starter product. As you can see, the Deaths property is not included within the rawData collection.


























Now, let's check the same request using the Unlimited product. This time, the Deaths property is included in the response.