- Print
- DarkLight
- PDF
Deploy a Logic app to send email
- Print
- DarkLight
- PDF
This article aims to help you deploy an Azure Logic App in your environment using Infrastructure-as-Code with Azure Bicep to trigger an action that sends a mail.
Azure Bicep is a domain-specific language (DSL) that uses a declarative syntax to deploy Azure resources.
The Bicep is an abstraction on Azure Resource Manager (ARM) templates to define Azure resources using declarative Infrastructure as Code.
Prerequisites
• An Active Azure account: You can create an account for free.
• Azure Bicep is installed on your local machine.
• Azure PowerShell. See: Install Azure PowerShell.
• A resource group in your Azure subscription
• A Sendgrid API connection. You can create your account here
Let's get started!
Solution Overview
We will author a Bicep template that creates a custom Logic App.
The solution will include the following files:
• 📄 main.bicep: This is the Bicep template
• 📄 azuredeploy.parameters.json: This parameter file contains the values for deploying your Bicep template.
2. Azure Bicep Template — parameters
Please create a new file in your working directory and name it main. Bicep. We will define the following parameters:
@description('The name for the logic app.')
param logicAppName string
@description('The SendGrid API key from the SendGrid service.')
@secure()
param sendgridApiKey string
@description('The name for the SendGrid connection.')
param sendgridName string
@description('Location for all resources.')
param location string = resourceGroup().location
2. Azure Bicep Template — resources
We will define the following resources:
resource sendgrid 'Microsoft.Web/connections@2018-07-01-preview' = {
location: location
name: sendgridName
properties: {
api: {
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, 'sendgrid')
}
displayName: 'sendgrid'
parameterValues: {
apiKey: sendgridApiKey
}
}
}
resource logicApp 'Microsoft.Logic/workflows@2019-05-01' = {
name: logicAppName
location: location
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: {
'$schema': 'http://json-schema.org/draft-04/schema#'
properties: {
emailbody: {
type: 'string'
}
from: {
type: 'string'
}
subject: {
type: 'string'
}
to: {
type: 'string'
}
}
required: [
'from'
'to'
'subject'
'emailbody'
]
type: 'object'
}
}
}
}
actions: {
Send_email: {
type: 'ApiConnection'
inputs: {
body: {
body: '@{triggerBody()[\'emailbody\']}'
from: '@{triggerBody()[\'from\']}'
ishtml: false
subject: '@{triggerBody()[\'subject\']}'
to: '@{triggerBody()[\'to\']}'
}
host: {
connection: {
name: '@parameters(\'$connections\')[\'sendgrid\'][\'connectionId\']'
}
}
method: 'post'
path: '/api/mail.send.json'
}
}
}
outputs: {
}
}
parameters: {
'$connections': {
value: {
sendgrid: {
id: subscriptionResourceId('Microsoft.Web/locations/managedApis', location, 'sendgrid')
connectionId: sendgrid.id
}
}
}
}
}
}
3. Parameters file
Create a new file named azuredeploy.parameters.json. The code below shows the definition of the parameters file:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"logicAppName": {
"value": "sendgridApp"
},
"sendgridapiKey": {
"value": "Your-API-Key"
},
"sendgridName": {
"value": "sendgridConnection"
}
}
}
4. Azure Bicep Template — Deployment
We will use the command below to deploy our Bicep template:
$date = Get-Date -Format "MM-dd-yyyy"
$rand = Get-Random -Maximum 1000
$deploymentName = "AzInsiderDeployment-"+"$date"+"-"+"$rand"
New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName azinsider_demo -TemplateFile .\main.bicep -TemplateParameterFile .\azuredeploy.parameters.json -c
The image below shows the preview of the deployment:
Then we will execute the deployment. The image below shows the deployment output:
The image below shows the deployment output in the Azure Portal:
Now you can go to the Logic Apps and verify the workflow using the Logic App Designer as shown below
Ensure the SendGrid API Key is present in your configuration
Testing your Logic App
You can test the Logic App using a payload with the below example:
{
"from": "my@email.com,"
"to": "your@email.com,"
"subject": "My logic app works,"
"emailbody": "Hello world"
}
You should see the success message in the overview tab of your Logic App:
Source Code
You can find the code of this solution in the following URL. Feel free to contribute!
https://github.com/daveRendon/azinsider/tree/main/application-workloads/logic-app-send-mail