0

I created a logic web app workflow with Terraform. I created correctly a trigger, and now I wanted to create an Action that send email. If I see the generated template everything looks good, but something I do not understand goes wrong.

enter image description here

I let you see the template generated of the action:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "send-email": {
                "inputs": {
                    "body": {
                        "Body": "...",
                        "From": "...",
                        "Subject": "...",
                        "To": "..."
                    },
                    "host": {
                        "connection": {
                            "name": "/subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.Web/connections/zzz"
                        }
                    },
                    "method": "post",
                    "path": "/SendEmailV3"
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "triggers": {...}
    },
    "parameters": {}
}

I let you see also what I see if I create the action by hand on portal azure:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Send_Email_(V3)": {
                "inputs": {
                    "body": {
                        "Body": "...",
                        "From": "...",
                        "Subject": "...",
                        "To": "..."
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['smtp']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/SendEmailV3"
                },
                "runAfter": {},
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {...}
    },
    "parameters": {
        "$connections": {
            "value": {
                "smtp": {
                    "connectionId": "/subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.Web/connections/zzz",
                    "connectionName": "smtp-5",
                    "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/westeurope/managedApis/smtp"
                }
            }
        }
    }
}

As you can see above, the only difference is the parameters block. If I create the action by hand, the portal generate the parameters and then it references the connection in the host.connection.name.

Instead, I simply set the connection name in the host.connection.name. In a pratical way it seems like there is no difference.

I tried also to set the workflow_paramters in the azurerm_logic_app_workflow but it is not clear how I must set the value. Here the doc I am trying to follow:

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/logic_app_workflow#workflow_parameters

And here how I am trying to set the value:

workflow_parameters = [{
      "$connections" =  {
        "type": "string",
        "defaultValue": "${azurerm_api_connection.noreply_email_api.id}"
      }
    }
  ]

But this is the error I get:

Inappropriate value for attribute "workflow_parameters": map of string required.

Any idea how to set the connection in logic web app workflow? Thank you

Simone
  • 2,304
  • 6
  • 30
  • 79
  • `workflow_parameters` should be a map but you're declaring it as an array - try removing the square brackets `[]`: `workflow_parameters = { "$connections" = { ... } }` – Rui Jarimba Mar 29 '23 at 07:00

2 Answers2

0

Please check the below code: Api connection can be first made with correct parameters given as follows:

Using azurerm_api_connection : azurerm_api_connection | Resources | hashicorp/azurerm | Terraform Registry

and the host can be given with name:

 "host": {
         "connection": {
       "name": "${azurerm_api_connection.example.id }"
                         }
          },

below is the Api connection deployment using arm template.

resource "azurerm_resource_group_template_deployment" "example" {
  name                = "apideployment"
  resource_group_name = data.azurerm_resource_group.example.name
  deployment_mode     = "Incremental"
    //depends_on = [azurerm_api_connection.example]
  template_content =  <<TEMPLATE
{   
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
    "variables": {},
    "resources": [
        {
  "type": "Microsoft.Web/connections",
  "apiVersion": "2016-06-01",
  "name": "samplegridname",
  "location": "eastus",
  "kind": "V1",
  "properties": {
        "displayName": "sendgrid",
        "authenticatedUser": {},
        "overallStatus": "Connected",
        "statuses": [
            {
                "status": "Connected"
            }
        ],
        "connectionState": "Enabled",
        "parameterValues": {
            "connections_name": {
            "defaultValue": "sendgrid",
            "type": "string"
        },
        },
        "customParameterValues": {},
        "createdTime": "2023-04-03T10:48:07.0647941Z",
        "changedTime": "2023-04-03T10:48:07.0647941Z",
        "api": {
            "name": "sendgrid",
            "displayName": "SendGrid",
            "description": "SendGrid Connection Provider lets you send email and manage recipient lists.",
            "iconUri": "https://connectoricons-prod.azureedge.net/releases/v1.0.1623/1.0.1623.3210/sendgrid/icon.png",
            "brandColor": "#34455F",
            "category": "Standard",
            "id": "/subscriptions/8xxx/providers/Microsoft.Web/locations/eastus/managedApis/sendgrid",
            "type": "Microsoft.Web/locations/managedApis"
        },
        "testLinks": [
            {
                "requestUri": "https://management.azure.com:443/subscriptions/8xxxdf8-7x/resourceGroups/v-sakavya-Mindtree/providers/Microsoft.Web/connections/samplegridname/extensions/proxy/v3/scopes?api-version=2018-07-01-preview",
                "method": "get"
            }
        ],
        "testRequests": [
            {
                "body": {
                    "request": {
                        "method": "get",
                        "path": "v3/scopes"
                    }
                },
                "requestUri": "https://management.azure.com:443/subscriptions/xxx6/resourceGroups/xxx/providers/Microsoft.Web/connections/samplegridname/dynamicInvoke?api-version=2018-07-01-preview",
                "method": "POST"
            }


        ]
    

         
    }
    }
    ],
    "outputs": {
      "exampleOutput": {
        "type": "string",
             "value" : "[resourceId('Microsoft.Web/connections', parameters('connectionname'))]"
      }
    }
}
TEMPLATE

}




locals {
  connectionid = jsondecode(azurerm_resource_group_template_deployment.example.output_content).exampleOutput.value
}

In some cases, it is required to manually connect the api:

see Logic Apps workflows via Terraform and Azure by Ansuman bal for reference and the steps to follow.

code:

resource "azurerm_logic_app_workflow" "example" {
  name                = "workflow1"
  location           = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_logic_app_trigger_recurrence" "trigger" {
  name         = "Recurrence"
  logic_app_id = azurerm_logic_app_workflow.example.id
  frequency    = "Week"
  interval     = 1
  schedule {
   ...
  }
  depends_on = [
    azurerm_resource_group_template_deployment.apiconnections,
     azurerm_logic_app_workflow.example
  ]
}

enter image description here

enter image description here

kavyaS
  • 8,026
  • 1
  • 7
  • 19
0

It is also possible to do it fully in terraform. You need to watch out to set the parameters and workflow_parameters argument in the azurerm_logic_app_workflow resource the correct way. (key value map where the value is jsonencoded).

It is shown in this guide.