Learn how to build the simplest possible Azure Function that will bring inline code execution to your flows! The possibilities are endless — regular expressions, string operations, recursive functions, math operations, and much more. This post contains two parts; how to create the Azure Function and how to execute and use it in Power Automate.
How to create an Azure Function App
Step 1: Firstly go to your Azure Portal, https://portal.azure.com/ (the link opens in a new tab). I use my MSDN subscription and the free credits in Azure for test purposes. If you don’t have an MSDN subscription or any subscription at all. You get access to the popular Azure services for free in 12 months here, https://azure.microsoft.com/en-us/free/ (the link opens in a new tab). The next step assumes that you are logged into the Azure Portal.
Step 2: When you are logged in to the portal you can an overview of your recent resources and azure services. For example:
Step 3: To create an Azure Function App click on + Create a new resource as shown in the image below. This will open a pane with services that are available to create, proceed to the next step.
Step 4: In the Create a resource pane search for “Function App”. This will open the creation page of the Function App directly when hitting enter. Proceed with the steps below the image.
Step 5: Click the create button to start the creation process. Next up is adding information about the resource, like resource group, location, name, and settings for the app.
Step 6: In this step, we add the necessary/required basic information for the Function App, where we add information about the resource, like resource group, location, name, and settings for the app.
When naming resources in Azure I like to follow the naming convention that Microsoft recommends as best practice as can be found below.
https://docs.microsoft.com/en-us/azure/cloud-adoption-framework/ready/azure-best-practices/resource-naming (link opens in a new tab).
The basic configuration in this example is as follows, and the basic information pane is shown with multiple images below.
Subscription: My MSDN subscription
Resource Group: rg-powerautomate-dev-weu-001
Function App Name: PowerAutomateHelper (This must be a unique name)
Runtime stack: Node.js
Version: 12 LTS
Region: West Europe
Step 7: The next step in the creation process is to review the information we added for the Function App and the remaining step is to create it. The step looks as follows in the Review + create step in Azure. This starts the deployment progress, as can be seen in the next step.
Step 8: After clicking the create button the deployment process of the resources starts immediately.
And finishes a short time after. To open the Function App click on Go to resource.
After opening the Function App click on the Functions item in the side menu. This is where we are going to add a function that allows us to write code in Power Automate.
After going to Functions we need to complete some steps to create a new function. These steps are as follows and are shown in the image below:
1. Create a new function by clicking Create. When the configuration pane opens follow the steps in the image below.
2. Choose to Develop in portal since we are going to add a simple code snippet.
3. Choose HTTP trigger since we are going to call this function from Power Automate
4. Give the function a name. For example FlowHelper.
5. Set Authentication level to Function.
6. Then click create and we can add the code in the next step.
When clicking on create in the previous step the function gets created.
Step 9: When the function is created we can add code by going to Code + Test as shown in the image below.
This opens the code editor of the function and can be seen in the image below.
Step 10: In this step, we add the code to allow us to write code in Power Automate. Replace the boilerplate code with the code below and click save. The code creates a new function that takes a body parameter and returns the result. When we write code in Power Automate the Function App will execute the code we pass the function and it will return the result. Make sure to save changes after adding the code.
Step 11: Before we move to Power Automate we need to get the function URL, we need this as we are going to call this function from Power Automate. To get the function URL click on the Get function URL and copy the URL as shown in the images below.
After clicking the Get function URL it opens a dialog with the link. Choose Copy to clipboard and save the URL for use in the next part of this post.
Building the Power Automate Flow
Step 1: The first step is to create a new cloud flow. For this example, we create an Instant cloud flow so that we can trigger it manually. In this example, I have a solution called Power Automate Flow that adds the instant cloud flow to. To create a new flow proceed with the steps in the image below.
Step 2: After the proceeding steps are completed a dialog opens. In the dialog give the flow a name and choose “Manually trigger a flow” and then create. This takes as to the newly created flow. To be able to save the flow an action must be added to be able to save it. We add this in the next step.
Step 3: When the flow is created and the editor opens. We start with adding a string variable that contains special characters that we want to replace with an underscore using code. After the flow trigger > Click add a new action > Search for variable > Select Initialize variable.
Step 4: After adding the variable, give the variable a name, choose a variable type of string, then add a string with special characters.
The code in this example uses regex to find and replace characters that don’t match lower and upper case letters and numbers with an underscore. And replaces multiple underscores if it is multiple special characters in a row that has been replaced.
Step 6: To see the result from the HTTP action we can create a compose action that will output the result.
Step 7: In the compose action, add the body from the HTTP action.
Step 8: When testing/running the flow we can see that the string with special characters has been replaced with a string where all special characters are replaced with an underscore.
The Flow run of the HTTP action looks like this:
Hope you found it valuable and see further use cases this can provide.
Originally published at https://www.fredrikengseth.com on December 6, 2021.