This post is part of a series about implementing serverless approval workflows on Azure:
- Approval Workflow on Logic Apps with Twilio
- Approval Workflow on Azure Durable Functions and SendGrid (this)
- Approval Workflow on Azure Durable Functions and Slack
Durable Functions is a new (in preview at the time of writing) and very interesting extension of Azure Functions that allows you to build stateful and serverless code-based workflows. The Durable Functions extension abstracts all the state management, queueing, and checkpoint implementation commonly required for an orchestration engine. Thus, you just need to focus on your business logic without worrying much on the underlying complexities. Thanks to this extension, now you can:
- Implement long-running serverless code-based services beyond the current Azure Function limitation of 10 minutes (as long as you can break down your process into small nano-services which can be orchestrated);
- Chain Azure functions, i.e., call one function after the other and pass the output of the first one as an input to the next one (Function chaining pattern);
- Execute several functions asynchronously and then continue the workflow when any or all of the asynchronous tasks are completed (Fan-out and Fan-in pattern);
- Get the status of a long-running workflow from external clients (Async HTTP APIs Pattern);
- Implement the correlation identifier pattern to enable human interaction processes, such as an approval workflow (Human Interaction Pattern) and;
- Implement a flexible recurring process with lifetime management (Monitoring Pattern).
It’s worth noting that Azure Durable Functions is not the only way to implement stateful workflows in a serverless manner on Azure. Azure Logic Apps is another awesome platform, core component of the Microsoft Azure iPaaS, that allows you to build serverless and stateful workflows using a designer. In a previous post, I showed how to implement the approval workflow pattern on Logic Apps via SMS messages leveraging Twilio.
In this post, I will show how to implement the Human Interaction Pattern on Azure Durable Functions with SendGrid. You will see on the way that this implementation requires other Durable Functions patterns, such as, function chaining, fan-out and fan-in, and optionally the Async HTTP API Pattern.
To illustrate this pattern on Durable Functions, I will be using a fictitious cat model agency called Furry Models Australia. Furry Models is running a campaign to attract the most glamorous, attractive, and captivating cats in Australia. They will be receiving photos of all aspiring cats and they need a streamlined approval process to accept or reject those applications. Furry Models want to implement this in an agile manner with a short time-to-market and with a very cost-effective solution. They know that serverless is the way to go!
To build this solution, we will need:
- A SendGrid account. Given that Azure Functions provides an output binding for SendGrid to send emails, we will be relying on this service. In case you want to implement this solution, you would need a SendGrid account. Once you sign up, you need to get your API Key, which is required for the Azure binding. You can get more information about the SendGrid binding for Azure Functions and how to use it here.
- An Azure Storage Account: The solution requires a Storage Account with 3 blob containers: requests, approved, and rejected. The requests container should have public access level so blobs can be viewed without a SAS token. For your own solution, you might want to make this more secure.
The picture below shows an overview of the approval workflow solution I've build based on Durable Functions.
Pictures of the aspiring cats are to be dropped in an Azure storage blob container called requests. At the end of the approval workflow, pictures should be moved to the approved or rejected blob containers accordingly.
The steps of the process are described as follows:
- The process is being triggered by an Azure Function with the BlobTrigger input binding monitoring the requests blob container. This function also implements the DurableOrchestrationClient attribute to instantiate a Durable Function orchestration
- The DurableOrchestrationClient starts a new instance of the orchestration.
- Then, the Durable Function orchestration calls another function with the ActivityTrigger input binding, which is in charge of sending the approval request email using the SendGrid output binding.
- SendGrid sends the approval request email to the (cat) user.
- Then, in the orchestration, a timer is created so that the approval workflow does not run forever, and in case no approval is received before the timer finishes the request is rejected.
- The (cat) user receives the email, and decides whether the aspiring cat deserves to join Furry Models or not, by clicking the Approve or Reject button. Each button has a link to an HttpTrigger Azure Function which expects the selection and the orchestration instanceId as query params
- The HttpTrigger function receives the selection and the orchestration instanceId. The function checks the status of the orchestration instance, if it’s not running, it returns an error message to the user. If it’s running, it raises an event to the corresponding orchestration instance.
- The corresponding orchestration instance receives the external event.
- The workflow continues when the external event is received or when the timer finishes; whatever happens first. If the timer finishes before a selection is received, the application is automatically rejected.
- The orchestration calls another ActivityTrigger function to move the blob to the corresponding container (approved or rejected).
- The orchestration finishes.
A sample of the email implemented is shown below.
The implemented solution code can be found in this GitHub repo. I’ve used the Azure Functions Runtime v2. I will highlight some relevant bits of the code below, and I hope that the code is self-explanatory 😉:
This BlobTrigger function is triggered when a blob is created in a blob container and starts the Durable Function ochestration (Step 1 above)
This is the Durable Function orchestration which handles the workflow and is started by the step 2 above.
ActivityTrigger function which sends the approval request via email with the SendGrid output binding (Step 3 above).
HttpTrigger function that handles the Http Get request initiated by the user selection (click) on the email (Step 7 above).
ActivityTrigger function that moves the blob to the corresponding container (Step 10 above).
These are the settings which configure the behaviour of the solution, including the storage account connection strings, the SendGrid API key, templates for the email, among others.
You would need to implement these as app settings when deploying to Azure
In this post, I’ve shown how to implement an Approval Workflow (Human Interaction pattern) on Azure Durable Functions with SendGrid. Whether you wanted to learn more about Durable Functions, to implement a serverless approval workflow or you run a cat model agency, I hope you have found it useful :) Please feel free to ask any questions or add your comments below.
Liked what you read? Drop us an email at email@example.com
if you would like our integration expert team to help in your next project.