This sample shows one approach for setting up a Logic App via Terraform. Terraform is used to provision the Azure resources, including a connection to Office 365 for purposes of sending an email.
-
Initialize Terraform.
terraform init
-
Create a
terraform.tfvarsfile for your Terraform variables.location = "[YOUR DESIRED AZURE REGION]" resource_group_name = "[YOUR RESOURCE GROUP NAME]" log_analytics_workspace_name = "[YOUR DESIRED LOG ANALYTICS WORKSPACE NAME]" application_insights_name = "[YOUR DESIRED APPLICATION INSIGHTS NAME]" logic_app_plan_name = "[YOUR DESIRED LOGIC APP WORKFLOW PLAN NAME]" logic_app_name = "[YOUR DESIRED LOGIC APP NAME]" logic_app_storage_account_name = "[YOUR DESIRED STORAGE ACCOUNT NAME FOR USE BY THE LOGIC APP]" notification_storage_account_name = "[YORU DESIRED STORAGE ACCOUNT NAME FOR NOTIFICATIONS]"
-
Plan the Terraform deployment.
terraform plan
-
Apply the Terraform plan to provision the resources.
terraform apply
Create a resource group and Azure Storage account to use while developing & testing the Logic App in Visual Studio code.
az login
az group create --name MyResourceGroup --location eastus
az storage account create \
--name mystorageaccount \
--resource-group MyResourceGroup \
--location eastus \
--sku Standard_LRS
az storage queue create \
--name myqueue \
--account-name mystorageaccount \
--account-key mystorageaccountkey - Modify the included
local.settings.SAMPLE.jsonfile to include your settings. - In the local.settings.json file, set the value of the
notification_queue_connection_stringsetting to the connection string of the desired Azure Storage queue. - Using Visual Studio Code, open the Logic App in the designer.
- In the Logic App designer, click to open the "Send an email (V2)" action. Click the "Change connection" link to create a new connection.
- Sign into Office 365.
- Complete the series of authentication and redirect prompts.
- Open the newly created "office365" API connection. Copy the "Connection Runtime Url" from the Properties section and paste into the
OFFICE365_CONNECTION_RUNTIME_URLsetting in thelocal.settings.jsonfile. - Use the Visual Studio Code debugger to "Attatch to Logic App".
The included /source/send-notifications.py Python script will send a message to the designated Azure Storage queue every 1 minute until stopped.
-
Create enviroment variables by creating a
.envfile in the/sourcedirectory. Include the name of the Azure Storage account and queue to send messages.NOTIFICATION_QUEUE_NAME=my-notifications-queue NOTIFICATION_STORAGE_ACCOUNT_NAME=stnotifications NOTIFICATION_RECIPIENT=you@email.com -
Grant yourself permissions to send messages to the queue.
az role assignment create \ --assignee [USER-PRINCIPAL-NAME] \ --role "Storage Queue Data Contributor" \ --scope /subscriptions/[AZURE-SUBSCRIPTION-ID]/resourceGroups/[RESOURCE-GROUP]] -
Run the Python script in the
/sourcedirectory.python send-notifications.py
-
Press Ctrl+C to exit and stop sending messages.
Use the included deploy-logic-app.sh to create a zip file and deploy the Logic App.
-
Create a
.envfile containing the following environment variables, substituting your Azure resource values as appropriate:LOGIC_APP_NAME="YOUR-LOGIC-APP-NAME" RESOURCE_GROUP="YOUR-RESOURCE-GROUP" SUBSCRIPTION_ID="YOUR-AZURE-SUBSCRIPTION-ID" PROJECT_DIR="./source" ARCHIVE_DIR="project_output" ZIP_FILE_PATH="workflow.zip"
-
Run the script. The script should create a zip file and deploy the zip using the Azure CLI.
Use the VS Code extension for Logic App Standard to deploy the Logic App to the recently provisioned Logic App resource.
- Right-click on the
/sourcedirectory and select the "Deploy to Logic App..." context menu option. - Select the desired Azure subscription.
- Select the recently created Logic App resource.
- Agree to the warning prompt about overwriting previous deployments by clicking the Deploy button.
After deploying the workflow to Azure, the Office 365 connection will need authenticated using your Office 365 account.
- In the Azure portal, click on the "office365" resource. You should notice a "Test connection failed." error near the top of the page.
- Click the error to edit the API connection.
- Click the Authorize button.
- Authenticate using your Office 365 account.
- Click the Save button to save the changes.
- If while running the Logic App you receive an authentication error on the Office 365 send email task, you may need to create a new API connection
- See Set up DevOps deployment for Standard logic app workflows in single-tenant Azure Logic Apps for more on creating API connections.
Credit to https://gist.github.com/SharonHart and the post at Provisioning Azure Logic Apps API Connections with Terraform for the knowledge on setting up the API connections with Terraform.