From Blob Container to SendGrid email as attachment via Azure Functions
Serverless is no more a tech buzzword. Many enterprises are embracing it in a BIG way. Microsoft Azure has great offerings of Serverless services in the form of Azure Functions , Azure Durable Functions and Logic Apps. Infact all these offerings from Azure has make life of a developer very easy, you as a developer doesn’t have to be bothered about any plumbing code. Just decorate your functions with appropriate trigger attributes and use the output parameter binding to chain the output of one function as an input to the another function. Isn’t that so cool ?
In this blog post I will share with you that how you can use Azure Functions for a scenario where any blob that is getting uploaded into the container, and you want send it as an attachment in the email using SendGrid. Yes you heard it right , Azure Functions also support SendGrid. In approximately 50 lines of the code this functionality can be achieved , if you try do this in a traditional manner without serverless I’m sure it wouldn’t be so simple. So here is the code
The code is pretty straight forward. The function is decorated with BlobTrigger attribute and the container name is “email-attachments”. The storage account connection is stored in the local.settings.json file. One thing to keep in mind that SendGrid requires the attachment to be converted / attached in Base64 format. Hence the input blob stream is converted into the bytes and later on converted into required format.
Azure Functions SDK provides all the nuts and bolts for event based programming model and it is deeply integrated with other Azure services, which helps devs to concentrate on their core business logic and writing clean and efficient code to the point. It also allows the extensibility where by which you can create your own binding parameters and runtime can parse them. Last but not least Serverless offers a saving on your cloud billing as you only pay for the actual usage (Only available in Consumption Plan).