cloudwoud

Logo

25 April 2022

Use Bicep to create Logic Apps with an Azure Queue connector

by Rik Groenewoud

How it started

On April 1st, I started my new job at Xpirit as an Azure & DevOps Consultant. As part of the Managed Services team I will focus on implementing and monitoring Azure infrastructure. During my second week my teammates said: “Create the Infrastructure as Code (IaC) for the Logic Apps”. It will be fun they said… And yes, fun it was, especially when I finally made it work! s It all started pretty easy, some of the Bicep fot the Logic App was already in place so I did not have to start from scratch. The templates and reference for the Logic App and API Connection can be found respectively here and here. But in this current scenario where we had to migrate existing infrastructure to Bicep I always suggest to export the existing resource via the portal to have a blueprint of how the code should look like. This can be done via the Azure Portal under the Automation options:

Portal

Soon enough I had the Azure resources available and the Logic App without the Azure Storage Queue Connector was working just fine. However, the Logic Apps with the Azure Queues connector in their design kept on failing. In the Logic App Designer this was the error I saw:

Error

A snippet from the Bicep I had for the Logic App in question:

actions: {
        'Put_a_message_on_a_queue_(V2)' : {
          runafter: {}
          type: 'ApiConnection'
          inputs: {
            body: 'start'
            host: {
              connection: {
                name: azureQueueConnectionId
              }
            }
              method: 'post'
              path: '/v2/storageAccounts/${storageAccountName}/queues/dailymaintenance/messages'
            
          }

Notice that for connection name I used a variable azureQueueConnectionId. This variable refers to a parameter azureQueueConnectionIdParameter and this parameter was declared in the main deployment bicep file as follows:

azureQueueConnectionIdParameter: logicAppConnection.outputs.id 

This way of referring to external resources and using output from the Logic App is normal practice within Bicep so I thought this was the best way to do it.

Troubleshoot time

So how did I troubleshoot?

How it was fixed

Now I knew it could work. What remained was an exercise in comparing the two templates. And yes, after some initial trial and error it dawned on me that it must be something in the definition of the API Connection in the Logic App. So I changed some of the working Bicep to the variable for the connection name (which is actually the API Connection id):

connection: {
                name: azureQueueConnectionId
              }

And there it was! Now the same error was thrown.

So after this test, I knew I was looking in the right direction. I decided to replace the variable I was using with the syntax from the working example.
The parameter $connections now is declared in the resource itself and the connention name refers to this param as a string:

actions: {
  'Put_a_message_on_a_queue_(V2)' : {
  runafter: {}
  type: 'ApiConnection'
  inputs: {
    body: 'start'
    host: {
      connection: {
        name: '@parameters(\'$connections\')[\'azurequeues\'][\'connectionId\']'
      }
    }
      method: 'post'
      path: '/v2/storageAccounts/${storageAccountName}/queues/dailymaintenance/messages'
    }
  }
}
parameters: {
  '$connections': {
    value: {
      azurequeues: {
        connectionId: logicAppConnection.id
        connectionName: 'LogicAppConnection'
        id: '/subscriptions/xxxxxxxxxxx/providers/Microsoft.Web/locations/westeurope/managedApis/azurequeues'
      }
    }
  }
}

Although to me it is not entirely clear what is the technical explanation behind it, everything worked with the above syntax :). For now I am happy with the result. An idea for improvement is to see if this connection name parameter can be set in the main deployment file after all and by doing this make the Bicep more readable and clean.

Other References

tags: microsoft - azure - bicep - iac - logicapp - azurequeue