Azure CLI script sample - create a logic app
Applies to: Azure Logic Apps (Consumption)
This script creates a sample logic app through the Azure CLI Logic Apps extension, (az logic
). For a detailed guide to creating and managing logic apps through the Azure CLI, see the Logic Apps quickstart for the Azure CLI.
Warning
The Azure CLI Logic Apps extension is currently experimental and not covered by customer support. Use this CLI extension with caution, especially if you choose to use the extension in production environments.
Prerequisites
- An Azure account with an active subscription. If you don't have an Azure subscription, create a free account.
- The Azure CLI installed on your local computer.
- The Logic Apps Azure CLI extension installed on your computer. To install this extension, use this command:
az extension add --name logic
- A workflow definition for your logic app. This JSON file must follow the Workflow Definition language schema.
- An API connection to an email account through a supported Azure Logic Apps connector in the same resource group as your logic app. This example uses the Office 365 Outlook connector, but you can also use other connectors like Outlook.com.
Prerequisite check
Validate your environment before you begin:
Sign in to the Azure portal and check that your subscription is active by running
az login
.Check your version of the Azure CLI in a terminal or command window by running
az --version
. For the latest version, see the latest release notes.- If you don't have the latest version, update your installation by following the installation guide for your operating system or platform.
Sample workflow explanation
This example workflow definition file creates the same basic example Consumption logic app workflow as in the quickstart for the Azure portal.
This sample workflow:
Specifies a schema,
$schema
, for the logic app.Defines a trigger for the logic app in the list of triggers,
triggers
. The trigger recurs (recurrence
) every 3 hours. The actions are triggered when a new feed item is published (When_a_feed_item_is_published
) for the specified RSS feed (feedUrl
).Defines an action for the logic app in the list of actions,
actions
. The action sends an email (Send_an_email_(V2)
) through Microsoft 365 with details from the RSS feed items as specified in the body section (body
) of the action's inputs (inputs
).
Sample workflow definition
Before you run the sample script, you must first create a sample workflow definition.
Create a JSON file,
testDefinition.json
on your computer.Copy the following content into the JSON file:
{ "definition": { "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#", "actions": { "Send_an_email_(V2)": { "inputs": { "body": { "Body": "<p>@{triggerBody()?['publishDate']}<br>\n@{triggerBody()?['title']}<br>\n@{triggerBody()?['primaryLink']}</p>", "Subject": "@triggerBody()?['title']", "To": "test@example.com" }, "host": { "connection": { "name": "@parameters('$connections')['office365']['connectionId']" } }, "method": "post", "path": "/v2/Mail" }, "runAfter": {}, "type": "ApiConnection" } }, "contentVersion": "1.0.0.0", "outputs": {}, "parameters": { "$connections": { "defaultValue": {}, "type": "Object" } }, "triggers": { "When_a_feed_item_is_published": { "inputs": { "host": { "connection": { "name": "@parameters('$connections')['rss']['connectionId']" } }, "method": "get", "path": "/OnNewFeed", "queries": { "feedUrl": "https://www.pbs.org/now/rss.xml" } }, "recurrence": { "frequency": "Hour", "interval": 3 }, "splitOn": "@triggerBody()?['value']", "type": "ApiConnection" } } }, "parameters": { "$connections": { "value": { "office365": { "connectionId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testResourceGroup/providers/Microsoft.Web/connections/office365", "connectionName": "office365", "id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Web/locations/westus/managedApis/office365" }, "rss": { "connectionId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/testResourceGroup/providers/Microsoft.Web/connections/rss", "connectionName": "rss", "id": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/providers/Microsoft.Web/locations/westus/managedApis/rss" } } } } }
Update the placeholder values with your own information:
Replace the placeholder email address (
"To": "test@example.com"
). You need to use an email address compatible with Logic Apps connectors. For more information, see the prerequisites.Replace additional connector details if you're using another email connector than the Office 365 Outlook connector.
Replace the placeholder subscription values (
00000000-0000-0000-0000-000000000000
) for your connection identifiers (connectionId
andid
) under the connections parameter ($connections
) with your own subscription values.
Save your changes.
Sample script
Note
This sample is written for the bash
shell. If you want to run this sample in another shell, such as Windows PowerShell or Command Prompt, you might need to make modifications to your script.
Before you run this sample script, run this command to connect to Azure:
az login
Next, navigate to the directory in which you created your workflow definition. For example, if you created the workflow definition JSON file on your desktop:
cd ~/Desktop
Then, run this script to create a logic app.
#!/bin/bash
# Create a resource group
az group create --name testResourceGroup --location westus
# Create your logic app
az logic workflow create --resource-group "testResourceGroup" --location "westus" --name "testLogicApp" --definition "testDefinition.json"
Clean up deployment
After you've finished using the sample script, run the following command to remove your resource group and all of its nested resources, including the logic app.
az group delete --name testResourceGroup --yes
Script explanation
This sample script uses the following commands to create a new resource group and logic app.
Command | Notes |
---|---|
az group create |
Creates a resource group in which your logic app's resources are stored. |
az logic workflow create |
Creates a logic app based on the workflow defined in the parameter --definition . |
az group delete |
Deletes a resource group and all of its nested resources. |
Next steps
For more information on the Azure CLI, see the Azure CLI documentation.
You can find additional Logic Apps CLI script samples in Microsoft's code samples browser.