DEV Community

Cover image for Creating COVID19 stats newsletter using Azure Function Apps
Vivek0712
Vivek0712

Posted on

Creating COVID19 stats newsletter using Azure Function Apps

Welcome to my blog series Fun with Function Apps where we will be learning about function apps their implementation through amazing use cases.

In this blog, we are create two function apps - HTTP Trigger & Timer Trigger which will help us create a newsletter consisting of COVID-19 India daily stats. The users can subscribe/ unsubscribe to the newsletter through the website

Lets get started...

The architecture of the solution will be as followsScreenshot 2021-04-14 at 2.16.05 AM

Pre-requisites

  1. Azure Account - (Free)
  2. Basic Python Programming
  3. Basic Web Development Skills (HTML, CSS & JS)
Warning: Kindly be aware of the incurring costs 
Enter fullscreen mode Exit fullscreen mode

Step 1. Setting up Storage Account

Go to Azure Portal and create a storage account. Under the storage account, create a table with ParitionKey, RowKey and email as its columns.
Make note of the table name, storage account name, key for the later use.

Screenshot 2021-04-14 at 12.00.07 PM

Step 2. Creating HTTP Trigger Function App

The purpose of the HTTP Trigger Function App is to perform subscribe and unsubscribe actions and the respective data will be updated in the Storage Account table.

Follow the steps given here to create a Python HTTP Trigger Function App

Replace the init.py code with the following and substitute all the necessary requirements.

After deploying your app, copy the function url.

To subscribe sample@sample.com
< function url > /api/HttpTrigger?action=subscribe&email=sample@sample.com

To unsubscribe sample@sample.com
< function url > /api/HttpTrigger?action=unsubscribe&email=sample@sample.com

Step 3: Create SendGrid Account

To send the mails, you need require SendGrid Account. Follow the steps to generate your SendGrid API Key.

Step 4: Create Timer Trigger Function App

Follow the steps in this tutorial to create a Timer Trigger Function App. Also, make sure you select Python Run Time.
We need to send the COVID-19 email newsletter at 6pm IST to all the subscribers.

Use the NCRONTAB schedule : 0 30 12 * * *

Add the SendGrid bindings to your function.json file

{
  "scriptFile": "__init__.py",
  "version": "2.0",

  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 30 12 * * *"
    },
    {
      "type": "sendGrid",
      "name": "sendGridMessage",
      "direction": "out",
      "apiKey": "sendgrid_api",
      "from": "vivekraja98@gmail.com"
    }
  ],
  "disabled": false
}
Enter fullscreen mode Exit fullscreen mode

The init.py to send the emails on schedule is as follows

Now navigate to your Function App in the azure portal -> Configuration. Under Application settings, create a key "sendgrid_api" and paste the Send Grid API Key you had generated in the previous step.

Step 5: Creating & Deploying the website to Azure Static Web App Service

Create a simple website where the user can enter his email address to subscribe/ unsubscribe. Call the respective HTTP Trigger Function URL for the same.

Screenshot 2021-04-14 at 12.33.12 PM

Create a Github Account and push all your source code.

Now to deploy you web app to Azure Static Web App Service.

  1. Create a repository in Github and upload all the source files.
  2. Go to Azure Portal -> Azure Static Web App (Preview) -> Create
  3. Enter the relevant details and sign up with your Github Account for the deployment details.
  4. Select your repo and the branch.
  5. Create

Once your app is deployed, you will be able to see the URL of the website. Copy the URL and navigate to the HTTP Trigger Functions. Under it, go the CORS and paste your website URL.

Congratulations! You have created a COVID-19 stats Newsletter.

Feel free to fork my GitHub Repo to come up with more interesting use cases.

GitHub logo Vivek0712 / covid-newsletter

Creating a COVID-19 India stats newsletter using Azure Function App

If you have made to this far end of the article, like and share to your fellow devs. For any doubts, help, suggestions and feedback, reach out to me through Twitter

Top comments (2)

Collapse
 
gsamarthyam profile image
Ganesh Samarthyam

Nice blog post with code as well! Great going Vivek Raja! :-)

Collapse
 
vivek0712 profile image
Vivek0712

Thank you :)