25 days of Serverless - Challenge 3

Solution to 25 days of Serverless - Challenge 3

Posted by thebernardlim on June 7, 2020

For Challenge #3 of the ‘25 days of Serverless’ challenge, we are to implement a solution to store images in a database if there is a ‘png’ image being pushed into a GitHub repo.

Solution

As per the instructions, we will be required to use Azure Functions and GitHub Webhooks.

Here are the steps:

  1. Create a GitHub Webhook. This can be accessed through the ‘Settings’ tab of your Repo. Choose the ‘Pushes’ event as your trigger. If you do not have a URL at this point, you can get a temporary webhook URL 'Webhooks' tab under Settings

    Webhook triggers: 'Webhooks' Triggers

  2. Do a test push to test out the webhook. In the ‘Recent Deliveries’ section we can see the request payload which we shall view to identify what fields we will need to parse.

    Here is a list of recent deliveries, i.e. Webhook triggers Recent Deliveries

    On click of any of the option, we can see the sample payload sent Payload sample

  3. Create Azure Function to parse payload. This is pretty straightforward as it was just a matter of parsing the request payload, filtering if the pushes contained a ‘png’ file, and then inserting into the database. I went with the Azure SQL route for simplicity. Code for the Azure Function can be found here

  4. In terms of authentication between Azure Function and Azure SQL, I decided to enable Managed Identity for Azure Function.

    Managed Identity

    Once that is done, create a contained DB user in Azure SQL DB which will represent the Azure Function. Assign the respective rights to allow insertions into the SQL DB.

    Create Contained DB User

  5. The integration should be ready at this point and once you push a ‘png’ image into your branch, the Image URLs should be now inside your database.

    Image