/azure-functions-event-grid-terraform

This sample shows how to create Storage Event subscriptions to Azure Functions, publish events to a custom Event Grid topic, and deploy it all with Terraform

Primary LanguageHCL

page_type languages products
sample
csharp
yaml
dotnet
dotnet-core
azure-functions
azure-event-grid
azure-storage
azure-blob-storage
azure-devops

Subscribing an Azure Function to Event Grid Events via Terraform

This sample will show you how to create Terraform scripts that create an Azure Function and Subscribe it to Event Grid Storage events. This is especially interesting as, for Event Grid Subscriptions, the target endpoint must answer EG's "Subscription Validation Event" which it cannot do until it is deployed. So this method - affectionally coined a "Terraform Sandwich" - shows how to do just that.

Deploying locally

  1. Open the repo in its VS Code Dev Container (this ensures you have all the right versions of the necessary tooling)
  2. run az login and az account set --subscription <target subscription id> to connect to your Azure Subscription
  3. cd infrastructure/terraform
  4. run terraform init
  5. run terraform apply -var 'prefix=<some unique prefix here>' -target module.functions
  6. cd ../../src/FunctionApp
  7. run func azure functionapp publish <the name of the functionapp outputted by terraform apply> --csharp
  8. cd ../../infrastructure/terraform
  9. run terraform apply -var prefix=<same prefix as before>

What it does

  • Tells terraform to run everything except the event grid subscription piece
  • Deploys the function app out to Azure so it's ready to answer the subscription wire-up that Terraform will do next
  • Tells terraform to apply everything, which issues the necessary changes to Azure to add the event grid subscription to an 'inbox' storage account

Deploying via Azure DevOps

By importing the azure-piplines.yaml file in to an Azure DevOps pipeline, you'll get the same process as the above local execution.

Note: Be sure to uncomment change the PREFIX variable to something unique to you to avoid naming collisions on storage & function apps

Azure DevOps successful run

Running the sample

This sample has an Azure Function that subscribes to Blob Storage Events and then simply passes the event on to a custom Event Grid Topic. The receiving and sending of events is accomplished via the Event Grid Binding for Azure Functions.

To exercise the sample:

  1. Open the 'inbox' storage account created by the deployment
  2. Create a new container
  3. Upload a file in to the container

Next, go to the Azure Portal, and the Storage Account created by the deployment. Click the 'Events' area and you will see one or more events have come through:

Azure Portal Storage Events area

Then go to the custom topic created by deployment, and you'll see that one or more events have been posted to it:

Azure Portal Custom Topic Events area

Finally, if you wish to see the output from the Function, go to the Application Insights resource created by deployment and look through the logged TRACE events:

Azure Portal Custom Topic Events area