{"id":3920,"date":"2022-07-28T09:54:57","date_gmt":"2022-07-28T08:54:57","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=3920"},"modified":"2022-07-28T09:54:57","modified_gmt":"2022-07-28T08:54:57","slug":"trigger-logic-app-when-azure-virtual-desktop-starts","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2022\/07\/trigger-logic-app-when-azure-virtual-desktop-starts\/","title":{"rendered":"Trigger logic app when Azure Virtual Desktop starts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We have several use cases where we want to &#8220;do something&#8221; when a user starts an Azure Virtual Desktop. One method could be a login\/startup script, but this would run under the user&#8217;s or Managed Identity&#8217;s context. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A better way is to use an <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/event-grid\/system-topics\" target=\"_blank\">Azure Event Grid System Topic<\/a> on the resource group that contains the VM&#8217;s, which can then forward any event that happens in the resource group.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A system topic is easily deployed using ARM:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n    {\n        \"type\": \"Microsoft.EventGrid\/systemTopics\",\n        \"apiVersion\": \"2021-12-01\",\n        \"name\": \"evgt-listenToAvdEvents-01\",\n        \"location\": \"global\",\n        \"properties\": {\n            \"source\": \"&#x5B;concat('\/subscriptions\/',subscription().subscriptionId,'\/resourceGroups\/rg-avd-weeu-01')]\",\n            \"topicType\": \"microsoft.resources.resourcegroups\"\n        }\n    }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">That having been deployed, we&#8217;ll deploy a logic app that is triggered by the topic. In this case, I want to do some advanced filtering so the logic app is only triggered when a VM is started by a user (vs automation). This is indicated by the Guid (principal ID) of Microsoft&#8217;s AVD serviceprincipal, in our case 068e1c948d874baba249f9a122cd8003 because we use &#8216;<a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/virtual-desktop\/start-virtual-machine-connect?tabs=azure-portal\" target=\"_blank\" rel=\"noreferrer noopener\">Start On Connect<\/a>&#8216;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/event-grid\/event-filtering\" target=\"_blank\">advanced filtering<\/a> in a logic app, use &#8220;enableAdvancedFilteringOnArrays&#8221;: true<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The full trigger section of the logic app (in ARM) is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n                    \"triggers\": {\n                        \"When_a_resource_event_occurs\": {\n                            \"splitOn\": \"@triggerBody()\",\n                            \"type\": \"ApiConnectionWebhook\",\n                            \"inputs\": {\n                                \"body\": {\n                                    \"properties\": {\n                                        \"destination\": {\n                                            \"endpointType\": \"webhook\",\n                                            \"properties\": {\n                                                \"endpointUrl\": \"@{listCallbackUrl()}\"\n                                            }\n                                        },\n                                        \"filter\": {\n                                            \"includedEventTypes\": &#x5B;\n                                                \"Microsoft.Resources.ResourceActionSuccess\",\n                                                \"Microsoft.Resources.ResourceDeleteSuccess\",\n                                                \"Microsoft.Resources.ResourceWriteSuccess\"\n                                            ],\n                                            \"subjectBeginsWith\": \"&#x5B;concat('\/subscriptions\/',subscription().subscriptionId,'\/resourceGroups\/rg-avd-weeu-01\/providers\/Microsoft.Compute\/virtualMachines')]\",\n                                            \"enableAdvancedFilteringOnArrays\": true,\n                                            \"advancedFilters\": &#x5B;\n                                                {\n                                                    \"operatorType\": \"StringIn\",\n                                                    \"key\": \"data.authorization.action\",\n                                                    \"values\": &#x5B;\n                                                        \"Microsoft.Compute\/virtualMachines\/start\/action\"\n                                                    ]\n                                                },\n                                                {\n                                                    \"operatorType\": \"StringIn\",\n                                                    \"key\": \"data.authorization.evidence.principalId\",\n                                                    \"values\": &#x5B;\n                                                        \"068e1c948d874baba249f9a122cd8003\"\n                                                    ]\n                                                }\n                                            ]\n                                        },\n                                        \"topic\": \"&#x5B;concat('\/subscriptions\/',subscription().subscriptionId,'\/resourceGroups\/rg-avd-weeu-01')]\"\n                                    }\n                                },\n                                \"host\": {\n                                    \"connection\": {\n                                        \"name\": \"@parameters('$connections')&#x5B;'azureeventgrid']&#x5B;'connectionId']\"\n                                    }\n                                },\n                                \"path\": \"&#x5B;concat('\/subscriptions\/@{encodeURIComponent(''',subscription().subscriptionId,''')}\/providers\/@{encodeURIComponent(''Microsoft.Resources.ResourceGroups'')}\/resource\/eventSubscriptions')]\",\n                                \"queries\": {\n                                    \"x-ms-api-version\": \"2021-12-01\"\n                                }\n                            }\n                        }\n                    },\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">You may also want to use the VM&#8217;s name in your logic app, this is easily parsed from the Subject field, e.g. as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\"Parse_Subject\": {\n    \"runAfter\": {},\n    \"type\": \"InitializeVariable\",\n    \"inputs\": {\n        \"variables\": &#x5B;\n            {\n                \"name\": \"subject\",\n                \"type\": \"string\",\n                \"value\": \"@triggerBody()?&#x5B;'subject']\"\n            }\n        ]\n    }\n},\n\"Parse_MachineName\": {\n    \"runAfter\": {\n        \"Parse_Subject\": &#x5B;\n            \"Succeeded\"\n        ]\n    },\n    \"type\": \"InitializeVariable\",\n    \"inputs\": {\n        \"variables\": &#x5B;\n            {\n                \"name\": \"machineName\",\n                \"type\": \"string\",\n                \"value\": \"@{last(split(variables('subject'),'\/'))}\"\n            }\n        ]\n    }\n},\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Important considerations:<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>the Logic App needs to have a managed identity<\/li><li>The LA&#8217;s MI needs to have the EventGrid Contributor role on the system topic<\/li><li><strong>you cannot edit this logic app through the gui<\/strong>, doing so will break it and cause the following error: &#8220;Unable\u00a0to\u00a0match\u00a0incoming\u00a0request\u00a0to\u00a0an\u00a0operation&#8221;<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Adding eventgrid contributor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New-AzRoleAssignment -ObjectId $la.Identity.PrincipalId -RoleDefinitionName \"EventGrid Contributor\" -Scope \"\/subscriptions\/$($context.Subscription.Id)\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We have several use cases where we want to &#8220;do something&#8221; when a user starts an Azure Virtual Desktop. One method could be a login\/startup script, but this would run under the user&#8217;s or Managed Identity&#8217;s context. A better way is to use an Azure Event Grid System Topic on the resource group that contains &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2022\/07\/trigger-logic-app-when-azure-virtual-desktop-starts\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Trigger logic app when Azure Virtual Desktop starts<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[49,4,5,6],"tags":[],"class_list":["post-3920","post","type-post","status-publish","format-standard","hentry","category-arm","category-automation","category-azure","category-windows-virtual-desktop"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=3920"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3920\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=3920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=3920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=3920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}