Parameterising Logic App (Standard) connections.json with bicep - Part 2
Tags: azure, bicep, logicapps
Published: 2022-05-01 03:18:41

Continuing from [where we left off](https://dev.to/leonards/parameterising-logic-app-standard-connectionsjson-with-bicep-part-1-2mhi), the next step is to update the appsettings, and set connections.json to read from appsettings.

This is where it gets interesting. After lots of searching thru' the internet and testing, there are a couple of points to highlight:

1. We can't replace the `managedApiConnections` information <u>as a whole string</u>, i.e. the below setting won't work

    ```json
    ...

    "managedApiConnections": {
      "azureblob_1": {
        "api": {
          "id": "@appsetting('azureBlobApiId')"
        },
        "authentication": {
          "type": "ManagedServiceIdentity"
        },
        "connection": {
          "id": "@appsetting('azureBlobConnectionId')"
        },
        "connectionRuntimeUrl": "@appsetting('azureBlobConnectionRuntimeUrl')"
      }
    }
    ...
    ```
    With the above, our connections seems to be working. Looking at the API connections' status, it reports "Connected". All is well, it seems...until we try to save a workflow. When we try to save the workflow, we're shown a puzzling error: 
    ```
    Workflow saved successfully. Workflow validation failed for the workflow 'xxxxxxxxxx'. The file 'connections.json' contains invalid expressions. The allowed expression functions are 'xxxxxxxxxx'.
    ```

2. To workaround the above issue, we can find the hint from this doc [Create cross-environment parameters for workflow inputs in Azure Logic Apps](https://docs.microsoft.com/en-us/azure/logic-apps/create-parameters-workflows?tabs=standard#parameterize-connections-file). In essence, rather than the above, we should instead create the following (the below code will <u>still cause an error</u> when saving a workflow. see step 3 for the last observation and workaround)

    ```json
    ...

    "managedApiConnections": {
      "azureblob_1": {
        "api": {
          "id": "/subscriptions/@{appsetting('WORKFLOW_SUBSCRIPTION_ID')}/providers/Microsoft.Web/locations/@{appsetting('WORKFLOW_LOCATION')}/managedApis/azureblob"
        },
        "authentication": {
          "type": "ManagedServiceIdentity"
        },
        "connection": {
          "id": "/subscriptions/@{appsetting('WORKFLOW_SUBSCRIPTION_ID')}/resourceGroups/@{appsetting('WORKFLOW_RG_NAME')}/providers/Microsoft.Web/connections/azureblob"
        },
        "connectionRuntimeUrl": "@appsetting('azureBlobConnectionRuntimeUrl')"
      }
    }
    ...
    ```

    <u>Note:</u> only `connectionRuntimeUrl` can successfully use appsettings. `apiId` and `connectionId` can **somehow** be partially parameterised.

3. Last observation, the above Microsoft doc recommendation for interpolated format by enclosing expression using curly brackets `{}` doesn't seem to work. In the end, we have to remove the curly brackets so that the final `connections.json` will look like this:

    ```json
    ...

    "managedApiConnections": {
      "azureblob_1": {
        "api": {
          "id": "/subscriptions/@appsetting('WORKFLOW_SUBSCRIPTION_ID')/providers/Microsoft.Web/locations/@appsetting('WORKFLOW_LOCATION')/managedApis/azureblob"
        },
        "authentication": {
          "type": "ManagedServiceIdentity"
        },
        "connection": {
          "id": "/subscriptions/@appsetting('WORKFLOW_SUBSCRIPTION_ID')/resourceGroups/@appsetting('WORKFLOW_RG_NAME')/providers/Microsoft.Web/connections/azureblob"
        },
        "connectionRuntimeUrl": "@appsetting('azureBlobConnectionRuntimeUrl')"
      }
    }
    ...
    ```

Once the above settings are done, we're now able to make use of the API connections and save workflows successfully.