August 24th, 2020
Send email notifications from Event Grid triggers.
In the last post, we looked at reading and writing blobs using BlobTriggers and Azue App Functions. Today we are going to add a function to send email notification when blobs are created using Event Grid notifications.
Introduction
This post assumes you have already completed the steps in part 1 of the previous post. In the same directory, lets create another function, using a Event Grid trigger this time.
func new --language javascript --name EventGridNotify --template "Azure Event Grid trigger"
Register API
If you haven't previously used Event Grid in your Azure subscription, you might need to register the Event Grid resource provider. Run the following command to register the provider:
az provider register --namespace Microsoft.EventGrid
It might take a moment for the registration to finish. To check the status, run:
az provider show --namespace Microsoft.EventGrid --query "registrationState"
Create a SendGrid account.
Follow the steps here to create a free SendGrid account.
After you create the account, verify a sender address and create a API key. Then we need to add the API key as a environment variable.
az functionapp config appsettings set \
--name AzureFunctionsTutorialBlog \
--resource-group AzureFunctionsTutorial \
--settings "SendGridAPItoken=<PUT YOUR API KEY HERE>"
Install dependencies
Let's install the SendGrid Javascript client.
npm install --save @sendgrid/mail
Add the code
Add the following code to index.js
. Don't forget to change the sender email address to the address you verified in SendGrid, and the recipient email address to a different and valid address.
const url = require('url');
const sgMail = require('@sendgrid/mail');
module.exports = async function (context, eventGridEvent) {
blobUrl = new URL(eventGridEvent.data.url);
const storageAccount = blobUrl.host;
const blobContainer = blobUrl.pathname.split("/")[1];
const blobPath = blobUrl.pathname.split("/").slice(2).join("/");
sgMail.setApiKey(process.env.SendGridAPItoken);
await sgMail.send({
to: 'event.target@company.com',
from: 'event.source@company.com',
subject: 'Blob Event Notification',
text: `A blob has been updated in storage account ${storageAccount}, container ${blobContainer}, at ${blobPath}.`
});
context.done();
};
Publish the function
Time to package and upload the function to the Function App.
func azure functionapp publish AzureFunctionsTutorialBlog
Create the event topic
We need to link the Storage Account to the Function App using a event subscription. To do this we will need resource IDs of both. Make sure you have the latest version of the Azure CLI installed before running this command.
storageAccountId=$(az storage account show \
--name azurefunctionsblobs \
--resource-group AzureFunctionsTutorial \
--query id \
--output tsv
)
functionAppId=$(az functionapp show \
--name AzureFunctionsTutorialBlog \
--resource-group AzureFunctionsTutorial \
--query id \
--output tsv
)
az eventgrid event-subscription create \
--source-resource-id ${storageAccountId} \
--name GridTestCreate \
--endpoint-type azurefunction \
--endpoint ${functionAppId}/functions/EventGridNotify \
--included-event-types Microsoft.Storage.BlobCreated \
--subject-begins-with /blobServices/default/containers/tutorial
Note that we are adding our function name to end of the Function App resource ID and that we are filtering event subjects to only notify on events coming from our tutorial
container.
That's it, we're done! Now upload a file a to your blob container and wait for the notification.