Azure Functions + EventGrid Subscriptions With Terraform
I think I figured it out. I have been working with Serverless Microservices Architecture on Azure for quite some time. I’ve been trying to leverage Terraform to effectively manage the extensive configuration of PaaS services on Azure. Due to immaturity of the Terraform AzureRM provider I’ve encountered well-documented challenges. This is the last major hurdle that I’ve overcome.
Thanks to this code I found here I was able to piece together EventGrid subscriptions for Azure Functions. The key was that you need to deploy code to your Azure Function that has an EventGridTrigger function in it. Without that your Azure Function will never load the EventGrid Extension. Up until recently, I had separate the code deployment into its own Release Pipeline. Therefore, when Terraform provisioned my Azure Functions, it did so without specifying code packages to run.
This was essentially, deploying an empty infrastructure that I could then deploy code to later in the traditional sense. However, the infrastructure I wanted to deploy also included EventGrid Subscriptions but because I needed a special Azure Function EventGrid Extension System Key in order to do this, and this didn’t get loaded until code with EventGridTriggers have been loaded I couldn’t deploy my entire infrastructure using Terraform…..until now.
I’ve added an additional module to my Azure Function Microservices Terraform Module Library that includes a version of an Azure Function Microservice that also uses EventGrid. This assumes that you will be passing in the “WEBSITE_USE_ZIP” app setting with a nicely SASified blob storage URL (more on that later).
The “azfn” module is designed to provision a single Function App and return the system key and event grid key necessary to use with API Management and Event Grid.
The “azfn_microservices” module is designed to provision the API Management components that are required for a single backend. You can see that the “function_key” that is output from the “azfn” module is be used by the API Management Backend to configure / authenticate with the Azure Function. Likewise, the “eventgrid_key” is used to build the WebHook Endpoint required by an EventGrid Subscription.
With this Terraform Module Library you can rapidly build / deploy your Serverless Microservices Architecture using Azure Functions EventGrid and CosmosDB (AFEC).