DevTest Labs – Shutdown Notifications in Teams Chat Messages

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Tech Community.

Azure DevTest Labs automatic shutdown policies can save money by ensuring that VMs are shut down every night and do not sit idle indefinitely.  On those occasions when a lab user works late, the shutdown notification settings allow lab users to be warned when the machine is about to be shutdown.  In this blog post, we will cover how to use the Webhook URL setting for auto-shutdown notification settings to send a direct chat message to someone working late and warn them that their machine is about to be turned off.  We will also cover how to create the chat message so the user can delay the shutdown by an hour or two by clicking a button in the chat message.

Create Logic App to receive shutdown notifications

  1. Create a Logic App.
  2. Add When an HTTP request is received trigger to the Logic App.

Sagar_Lankala_0-1605041267623.png

 

As seen in the picture above, this action needs a JSON schema so information in the request body can be used by actions in the Logic App.  Schema for the request is below for convenience.  The Configure autoshutdown for lab and compute virtual machines in Azure DevTest Labs article contains the latest JSON schema for shutdown notifications.

  

  {

        "$schema": "http://json-schema.org/draft-04/schema#",

        "properties": {

            "delayUrl120": {

                "type": "string"

            },

            "delayUrl60": {

                "type": "string"

            },

            "eventType": {

                "type": "string"

            },

            "guid": {

                "type": "string"

            },

            "labName": {

                "type": "string"

            },

            "owner": {

                "type": "string"

            },

            "resourceGroupName": {

                "type": "string"

            },

            "skipUrl": {

                "type": "string"

            },

            "subscriptionId": {

                "type": "string"

            },

            "text": {

                "type": "string"

            },

            "vmName": {

                "type": "string"

            },

            "vmUrl": {

                "type": "string"

            },

            "minutesUntilShutdown": {

                "type": "string"

            }

        },

        "required": [

            "skipUrl",

            "delayUrl60",

            "delayUrl120",

            "vmName",

            "guid",

            "owner",

            "eventType",

            "text",

            "subscriptionId",

            "resourceGroupName",

            "labName",

            "vmUrl",

            "minutesUntilShutdown"

        ],

        "type": "object"

    }

 

  1. Add a Post your own adaptive card as the Flow bot to a user (preview) action to the Logic App.  This action will send a chat message from the Flow bot to a specific user.  This action also allows  an adaptive card to be sent to a user, which means we can add buttons to the message.  This action does require a connection of a Microsoft Account.

 

The recipient of the message should be the owner of the VM.  Get the owner’s email by searching for the ‘owner’ dynamic content from the HTTP request trigger.

 

The message in this action will be JSON that uses the Adaptive Card JSON schema.  For our example, we have a simple message to the user telling them that their VM will be shutdown soon and buttons to allow the user to skip the shutdown, delay the shutdown 1 hour or delay the shutdown 2 hours.

 

{

    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",

    "type": "AdaptiveCard",

    "version": "1.0",

    "speak": "Your virtual machine is shutting down soon.  Do you want to delay the shutdown?",

    "body": [

        {

            "type": "TextBlock",

            "text": "Virtual Machine Auto-Shutdown",

            "size": "large",

            "weight": "bolder"

        },

        {

            "type": "TextBlock",

            "text": "The virtual machine @{triggerBody()['vmName']} is scheduled for shutdown in @{triggerBody()?['minutesUntilShutdown']} minutes.",

            "wrap": "true"

        }

    ],

    "actions": [

        {

            "type": "Action.OpenUrl",

            "title": "Delay 1 hour",

            "url": "@{triggerBody()['delayUrl60']}"

        },

        {

            "type": "Action.OpenUrl",

            "title": "Delay 2 hours",

            "url": "@{triggerBody()['delayUrl120']}"

        },

        {

            "type": "Action.OpenUrl",

            "title": "Skip shutdown",

            "url": "@{triggerBody()['skipUrl']}"

        }

    ]

}

 

The ‘@triggerBody()’ statements tell the LogicApp to get the value from the HTTP request trigger we created in the previous step.

 

Lastly, set the IsAlert setting in the action to ‘Yes’.  This will cause the user to be notified in their Activity stream when the message is sent.

 

Action should look like the following picture.

Sagar_Lankala_4-1605041410636.png

 

  1. Add a HTTP Response action.  Set the status code to 200 to indicate everything was successful.

Now that we have our Logic App that can handle sending a message to a user, it’s time to setup the DevTest Lab to send notifications to our Logic App.  We will need the url to call the Logic App.  To get the url, expand the When an HTTP request is received trigger step and copy the HTTP POST URL property.

Configure lab auto-shutdown settings

 

Auto-shutdown settings are configured at either the lab level or individual lab VM level.  Individual settings for auto-shutdown notifications are only allowed if the lab owner sets the auto-shutdown policy to allow individual users to override the lab auto-shutdown settings.  See Configure auto-shutdown for lab in Azure DevTest Labs for further details.

Let’s cover how to use the Logic App we created above by configuring auto-shutdown settings at a lab level.

  1. On the home page for your lab, select Configuration and policies.
  2. Select Auto-shutdown in the Schedules section of the left menu.
  3. Select On to enable auto-shutdown policy.
  4. For Webhook URL, paste the url for the Logic App we created earlier.
  5. Select Save.

Sagar_Lankala_2-1605041267648.png

 

 

Conclusion

That’s all we need to do!  Next time a lab VM in our lab is about to be shutdown, the lab VM owner will be sent a chat message in Teams.  The message will allow the lab VM owner to quickly delay shutdown using the action buttons on the bottom of the message.

 

Sagar_Lankala_3-1605041267653.png

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.