Search code examples
azurepowershellazure-resource-managerazure-rm-template

How to create ARM template for logic App with API connection to Gmail?


{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "logicAppName": {
      "type": "string",
      "defaultValue": "la-send-mail",
      "metadata": {
        "description": "Name of the Logic App."
      }
    },
    "logicAppLocation": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location of the Logic App."
      }
    },
    "gmail_name": {
      "type": "string",
      "defaultValue": "gmail"
    },
    "gmail_displayName": {
      "type": "string",
      "defaultValue": "[email protected]"
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Logic/workflows",
      "apiVersion": "2016-06-01",
      "name": "[parameters('logicAppName')]",
      "location": "[parameters('logicAppLocation')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/connections', parameters('gmail_name'))]"
      ],
      "properties": {
        "definition": {
          "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "$connections": {
              "defaultValue": {},
              "type": "Object"
            }
          },
          "triggers": {
            "manual": {
              "type": "Request",
              "kind": "Http",
              "inputs": {
                "schema": {
                  "properties": {
                    "body": {
                      "type": "string"
                    },
                    "bodyHTML": {
                      "type": "string"
                    },
                    "ccAddress": {
                      "type": "string"
                    },
                    "color": {
                      "type": "string"
                    },
                    "datafactoryName": {
                      "type": "string"
                    },
                    "pipelineName": {
                      "type": "string"
                    },
                    "pipelineRunId": {
                      "type": "string"
                    },
                    "time": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    },
                    "toAddress": {
                      "type": "string"
                    }
                  },
                  "type": "object"
                }
              }
            }
          },
          "actions": {
            "Initialize_variable": {
              "runAfter": {},
              "type": "InitializeVariable",
              "inputs": {
                "variables": [
                  {
                    "name": "HTMLBody",
                    "type": "string",
                    "value": "<div>\n<h1 style=\"Color:@{triggerBody()?['color']};\"> Executed successfully </h1>\n<hr/>\nData Factory Name: <b>@{triggerBody()?['datafactoryName']}</b><br/>\nPipeline Name: <b>@{triggerBody()?['pipelineName']}</b><br/>\nPipeline Run Id<b>@{triggerBody()?['pipelineRunId']}</b><br/>\nTime: <b>@{triggerBody()?['time']}</b><br/>\n<hr/>\n<p>@{triggerBody()?['body']}</p>\n<div>@{triggerBody()?['bodyHTML']}</div>\n</div>"
                  }
                ]
              }
            },
            "Send_email_(V2)": {
              "runAfter": {
                "Initialize_variable": [
                  "Succeeded"
                ]
              },
              "type": "ApiConnection",
              "inputs": {
                "body": {
                  "Body": "<p>@{variables('HTMLBody')}</p>",
                  "Cc": "@triggerBody()?['ccAddress']",
                  "Subject": "@triggerBody()?['title']",
                  "To": "@triggerBody()?['toAddress']"
                },
                "host": {
                  "connection": {
                    "name": "@parameters('$connections')['gmail']['connectionId']"
                  }
                },
                "method": "post",
                "path": "/v2/Mail"
              }
            }
          },
          "outputs": {}
        },
        "parameters": {
          "$connections": {
            "value": {
              "gmail": {
                "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',parameters('logicAppLocation'),'/managedApis/gmail')]",
                "connectionId": "[resourceId('Microsoft.Web/connections', parameters('gmail_name'))]",
                "connectionName": "[parameters('gmail_name')]"
              }
            }
          }
        }
      }
    },
    {
      "type": "Microsoft.Web/connections",
      "apiVersion": "2016-06-01",
      "location": "[parameters('logicAppLocation')]",
      "name": "[parameters('gmail_name')]",
      "properties": {
        "api": {
          "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',parameters('logicAppLocation'),'/managedApis/gmail')]"
        },
        "displayName": "[parameters('gmail_displayName')]"
      }
    }
  ],
  "outputs": {}
}

This is the template I use, it always gives the "The deployment 'la-send-mail-1_2' failed with error(s). Showing 1 out of 1 error(s). Status Message: The operation on workflow 'la-send-mail' cannot be completed because it contains connections to 'gmail' connector which are not valid. Please re-authorize the connections and try again. (Code:GmailConnectorPolicyViolation)" error

I am run deployment from simple PowerShell script. Could someone help me to fix this issue


Solution

  • Thank you Thomas. Posting your suggestions as an answer to help other community members.

    The Authorize document will help you in authorizing the OAuth connections.

    • Manually authorize OAuth connections by opening your logic app in Logic App Designer, either in the Azure portal or in Visual Studio. When you authorize your connection, a confirmation page might appear for you to allow access.

    For Oauth connection to ARM template you need to script it. But the easiest way is to create manually these connection then deploy ARM.

    Refer Logic App Connection Auth Document for further information.

    • This script will retrieve a consent link for a connection (and can also create the connection at the same time) for an OAuth Logic Apps connector. It will then open the consent link and complete authorization to enable a connection. This can be used after deployment of connections to make sure a Logic App is working end-to-end.