Andrew Bennett
Andrew Bennett

Reputation: 13

How to get Resource Group name from within Logic App

In an Azure Logic App, how can I get the name of the Resource Group containing the current logic app?

I want to include some tracking details in the JSON output that I am sending to another system.

I can get the run Identifier ( using @{workflow()['run']['name']} ),

and the current logic app name ( using @{workflow()['name']} )

However, I cant work out how to get the name of the resource group to which the logic app is deployed.

As a last resort, I will use the resource group name used by the deployment template, but that will be wrong if the logic app is moved later. I could also use tags, but again that could get out of step if the logic app is moved.

Thanks

Upvotes: 1

Views: 4628

Answers (3)

Costin
Costin

Reputation: 13

If you're deploying the Logic Apps using ARM templates (e.g. edit in Visual Studio, check into Azure DevOps git repo and deploy using release pipeline), you can create an ARM parameter:

"resGroup_ARM": {
  "type": "string",
  "defaultValue": "[resourceGroup().name]",
  "metadata": {
    "description": "Resouce group name"
  }
}

Then, you can create a workflow parameter:

        "resGroup_LA": {
          "type": "string",
          "defaultValue": "ResGroup LA default"
        }

... and give it a value in the parameters initialisation section:

      "resGroup_LA": {
        "value": "[parameters('resGroup_ARM')]"
      }

You can get all the other properties of resourceGroup() in a similar manner, see: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource?tabs=json#resourcegroup

Upvotes: 1

Dony van Vliet
Dony van Vliet

Reputation: 11

A simple formula may be:

split(workflow().id, "/")[4]

Upvotes: 1

Hury Shen
Hury Shen

Reputation: 15754

First we can create a "Initialize variable" action to get all of the data in workflow, shown as below screenshot:

enter image description here

Then we can find the data in workflow is:

{
    "id": "/subscriptions/*****/resourceGroups/huryTest/providers/Microsoft.Logic/workflows/hurylogicblob",
    "name": "hurylogicblob",
    "type": "Microsoft.Logic/workflows",
    "location": "eastus",
    "tags": {},
    "run": {
        "id": "/subscriptions/*****/resourceGroups/huryTest/providers/Microsoft.Logic/workflows/hurylogicblob/runs/*****",
        "name": "*****",
        "type": "Microsoft.Logic/workflows/runs"
    }
}

It contains the resource group name, so we just need to get the property "id" and substring it to get resource group name. The length of "resourceGroups/" is 15, so in the expression below I use add(,15) and sub(,15).

You can use the expression as below:

substring(workflow()['id'],add(indexOf(workflow()['id'],'resourceGroups/'),15),sub(sub(indexOf(workflow()['id'],'/providers'),indexOf(workflow()['id'],'resourceGroups/')),15))

At last, I got the resource group name of the logic app:

enter image description here

Upvotes: 1

Related Questions