Coder Social home page Coder Social logo

functions-logicapp-startstopvms's Introduction

Start Stop VMs using Azure Functions and Azure LogicApps

This solution allows you to Start/Stop your Azure VM's based on a schedule. It uses Azure Functions and Azure Logic Apps.

The following functionality is available:

  • Ability to Start and Stop Azure VM's for a given Subscription(required) based on a schedule
  • Ability to filter the VM's based on a Resource Group
  • Ability to filter the VM's based on a Tag
  • Ability to configure an email sent whenever an action is taken with the number of VM's and the names of VM's affected.

The implementation uses Azure VM async API's and Azure Functions Durable Functions. The number of VM's that are handled by a single Function can be controlled.

Instructions:

This solution creates an Azure Function and two Azure Logic Apps.

Deployment of Azure Resources

Click the Deploy to Azure Button, this brings up the dialog below in the Azure Portal, choose the subscription and resource group that you want to create your Azure Function and Logic Apps in. Finally choose the name of the "App". This will be used to create the name of the Function and the Logic Apps.

Image of Deployment

Give the required permission to the Functions App MSI

The Function App needs permissions on the VM's to perform the start, stop action on them. The deployment already creates a MSI with the [AppName]. Based on whether you want to want to give permission to this MSI for a whole Subscription, ResoureGroup or even VM's you have to add the "Virtual Machine Contributor" role assignment to the MSI for it. This can be done in the Azure Portal by navigating to the appropriate scope and then to the "Access Control (IAM)" and click the "Add a role assignment". Note that this Subscription/Resource Group/VM's can be in a different Subscription/Resource Group that you have depoyed the app too.

Image of AddRoleAssignment

This opens the dialog box for the Add role assignment. Choose the role "Virtual Machine Contributor" and search for the [AppName] in the Select and select the MSI and click Save. This is the way that you can control access.

Image of RoleAssignment

Configuring Logic Apps

Once deployment is done you will see that several Azure Resources are created. The Start VM and Stop VM functionality is driven through two Logic Apps [AppName]-StartVMs and [AppName]-StopVMs.

Navigate to the Logic Apps section of the Azure Portal and find the Logic Apps created. For each of them, Click through the Logic App and click Edit and Click Code View.

Image of LogicApps

Configure the Schedule that you want the Start or Stop VM's to take place

Click on the first activity where you enter the schedule for when the VM's need to be started or stopped and edit it accordingly. Currently the Start VM Logic App is configured to run at 7:00 AM Pacific Time and the Stop VM Logic App is configured to run at 7:00 PM Pacific Time.

Image of LogicApps

Configure the Azure Subscription, Azure Resource Group (optional) and Tags (optional)

  • SubcriptionId: Click on the Enter SubscriptionId activity and in the Value field enter the Azure Subscription Id that you want to start or stop VM's for (note that the SubscriptionId that you deployed your resources to is already prepopulated, if this is the same Subscription whose VM's you want to Start/Stop then leave that)

  • Resource Group: Optionally, enter the value of a resource group that you want to filter the VM's on by entering a value in the Value field. ((note that the Resource Group that you deployed your resources to is already prepopulated, if this is the same Resource Group whose VM's you want to Start/Stop then leave that else you could remove the value and leave it blank as well)

  • Tags: Optionally, if you want to filter the VM's by Tags, by entering the appropriate value in the Tags Value field. This will filter the VM's to only those that have that Tags defined on the VM (with any value).

Note that SubscriptionId is a required field, but both Resource Group and Tags are optional and can be both used or used individually.

Image of LogicApps

Next, update the Office 365 connection (If you dont have a Office 365 connection, you can use SendGrid, if you dont want email you can even remove this step) and put in the email address you want to send a report to whenever the logic app runs.

Image of Email

Once done, save the Logic Apps. Finally go ahead and Enable the Logic Apps.

Once enabled, the Logic Apps will trigger on the schedule. You can test out the Logic Apps by clicking on Run Trigger to test out if the functionality works.

Cost

There is no additional cost of using this application other than the resources that are being used. Azure Functions and Azure Logic Apps have very generous free caps, so it should be free for most customers. Azure Functions also use Azure Storage. For normal use of this application - that cost should also be very minimal for managing thousands of machines.

Output and Troubleshooting

All the runs of the Logic Apps can be viewed in the Logic Apps output window

Image of Results

Clicking on a Run will take you to the detailed view of the Run result. Clicking on the StartStopVMs activity right at the bottom and looking at the Body will give you a listing on the number of VM's affected and their names. This is typically where you can also find any errors that were encountered.

Image of Results

Common issues

  • Not Authorized

One of the most common errors that you might see is a "Not Authorized" error.

Image of Results

This is usually because you havent configured the Functions MSI to access the Subscription etc.

functions-logicapp-startstopvms's People

Contributors

anirudhgarg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.