How to automate turning off or suspending some Azure services to save money?

EnterpriseArchitect 5,406 Reputation points
2024-05-05T13:46:36.69+00:00

What Azure objects can we suspend or turn off outside business hours to save running costs?

App Service: The app service implements the message compose experience in the team tab and the messaging endpoint for the bot.

Service Bus: The individual messages sent by the bot to the recipients are queued on a service bus queue, to be processed by an Azure Function. This queue decouples the message composition experience from the process that delivers the message to recipients.

Azure Function: The Azure Function picks up the messages from the queues, prepares the recipients and delivers them.

Azure Key vault: The Azure Key vault stores secrets, certificates and connection strings. For more information about the data stored, see

Author Bot Registration: The Author Bot registration is used for authors' apps. The application is separated from the User application so that different teams' policies can be applied.

  1. User Bot Registration: The User Bot registration is utilized for the user's application, which users install to receive messages sent by the Company Communicator application.

The above are some examples deployed by the Company Communicator applications on Azure which can be very costly when kept running despite not being used by the users.

Azure Cost Management
Azure Cost Management
A Microsoft offering that enables tracking of cloud usage and expenditures for Azure and other cloud providers.
2,681 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,253 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,257 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
915 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,582 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 12,001 Reputation points
    2024-05-05T14:43:33.43+00:00

    Hello EnterpriseArchitect,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Problem

    Sequel to your questions, I understand that you want to figure out how to stop or shut down certain parts of the app when they're not needed, like outside regular work hours. This means looking at things like the App Service, Service Bus, Azure Function, and Azure Key Vault, and making sure they're only running when necessary. By doing this, your company can cut down on costs while still making sure the app runs smoothly when it's supposed to.

    Scenario

    The Company Communicator application utilizes various Azure resources to facilitate message composition, queuing, processing, and delivery. During non-operational hours, such as nights and weekends, these resources continue to run, resulting in unnecessary costs. To address this issue, the company seeks to implement cost-saving measures by identifying which Azure objects can be suspended or turned off outside of business hours.

    Solution

    This prescribed solution was based on the scenario given and your questions, while focusing on the problem statement. However, given it a second thought from my side, your questions were environment base, therefore. To save on running costs, you can consider suspending or turning off the following Azure objects outside of business hours:

    App Service: If the message compose experience and messaging endpoint for the bot are not required outside of business hours, you can consider scaling down or stopping the Azure App Service during non-operational hours.

    Service Bus: Since messages are queued on a service bus queue to be processed by an Azure Function, you may not need the Service Bus to be active outside of business hours. You could explore stopping or scaling down the Service Bus during these times.

    Azure Function: If message processing is not critical outside of business hours, you can suspend or scale down the Azure Function that picks up messages from the queues, prepares recipients, and delivers them.

    Azure Key Vault: Azure Key Vault typically doesn't consume many resources when idle. However, if there are secrets, certificates, or connection strings that are only required during business hours, you could explore implementing Key Vault access policies or automations to restrict access or operations outside of these hours.

    Author Bot Registration and User Bot Registration: If these registrations are not actively used or required outside of business hours, you can explore options to suspend or scale down the corresponding Azure resources associated with these registrations during non-operational hours.

    Finally

    Once you identify any from above, that will answer your question.

    However, to solve the problem and address the questions outlined, you can follow these steps:

    STEP 1:

    Analyze usage patterns to determine which Azure resources remain idle outside of business hours.

    • Focus on resources like App Service, Service Bus, Azure Function, and Azure Key Vault mentioned in the user inputs.
    • Review Azure Monitor metrics and logs to analyze resource utilization patterns.
    • Identify resources with low activity or usage outside of business hours.

    STEP 2:

    Utilize Azure Automation or Azure Functions to automate the process of suspending or scaling down resources during non-operational hours.

    • Create PowerShell scripts or Azure Resource Manager (ARM) templates to perform resource scaling or suspension based on predefined schedules.
    • For example, you can create PowerShell scripts or ARM templates to scale down or suspend App Service, Service Bus, Azure Function, and Azure Key Vault.

    Utilize Azure Automation to schedule and execute these scripts automatically.

    # Connect to Azure account
    Connect-AzAccount
    # Get the Azure Web App resource
    $appService = Get-AzWebApp -ResourceGroupName "YourResourceGroup" -Name "YourAppServiceName"
    # Scale the App Service to 0 instances
    $appService | Set-AzWebApp -NumberofInstances 0
    

    STEP 3:

    Define schedules for resource suspension or scaling based on business hours and non-operational hours.

    Utilize Azure Scheduler or Azure Logic Apps to trigger automation scripts at specified times.

    • Define schedules using Azure Scheduler or Azure Logic Apps to trigger the automation scripts during non-operational hours.

    Set up recurring schedules based on business hours and weekends/nights.

    STEP 4:

    Implement logic in the Azure Function to monitor queue length and scale dynamically.

    • Configure Azure Function settings to automatically scale based on queue activity.
    // Azure Function to process messages from Service Bus Queue
    public static async Task Run([ServiceBusTrigger("your-queue-name", Connection = "ServiceBusConnection")] string myQueueItem, ILogger log)
    {
        log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
        // Add your message processing logic here
        // Get queue length
        int queueLength = await GetQueueLength("your-queue-name");
        // Scale dynamically based on queue length
        if (queueLength > 100)
        {
            // Scale out
            log.LogInformation("Scaling out Azure Function to handle increased load...");
            // Code to scale out Azure Function
        }
    }
    public static async Task<int> GetQueueLength(string queueName)
    {
        // Code to get queue length from Service Bus
    }
    
    # PowerShell to Suspend the Azure Function
    # Connect to Azure account
    Connect-AzAccount
    # Get the Azure Function App resource
    $functionApp = Get-AzFunctionApp -ResourceGroupName "YourResourceGroup" -Name "YourFunctionAppName"
    # Suspend the Azure Function
    $functionApp | Stop-AzFunctionApp
    

    STEP 5:

    Implement access policies in Azure Key Vault to restrict access during non-operational hours.

    • Set up scheduled access policies to allow access only during required times.

    By following the above steps and utilizing appropriate automation and scheduling techniques, along with monitoring and scaling strategies, you can effectively optimize costs for the Company Communicator application on Azure while ensuring seamless functionality during operational hours. I assure you of 90% success rate.

    References

    Source: Microsoft Bing. Accessed, 5/5/2024.

    Source: 15% of the procedures were provided by Copilot, which was reaffirm and adjusted by me.

    Kindly utilize the additional resource available by the right side of this page

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    Please remember to "Accept Answer" if answer helped, so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.