Introduction To Serverless Computing Using Azure Function

Serverless Computing is driven by performing some action as a response to the specific event or trigger. While building serverless apps you don’t need to provision a server or infrastructure. Billing is based on resources consumed or actual time code is running. There's no need to build app to run the code, no need to set up infrastructure, no need to setup deployment infrastructure. Just focus on code.

Serverless Computing

  • Serverless Computing is driven by performing some action as a response to specific event or trigger.
  • While building serverless apps you don’t need to provision server or infrastructure.
  • Billing is based on resources consumed or actual time code is running.
  • No need to build the app to run the code, no need to set up infrastructure, no need to setup deployment infrastructure. Just focus on code.

Azure Function

  • Azure Function is a small piece of code that gets executed when some event occurs.
  • Just focus on code without worrying about application and infrastructure to run it.
  • This is also called as “function-as-a-service”
  • You can choose your favorite language to write Azure Function, such as C#, F#, Node.js, Java or PHP.
  • Azure function helps you to build fully serverless applications

When to Use Azure Functions

  • Azure Functions can be a great solution while building small microservices or building simple APIs.
  • This can be helpful in scenarios like order processing, image processing, logging, file processing etc.
  • You can also perform sheduling or batch processing tasks using Azure Functions.

Azure Function Trigger Templates

Azure Functions provide templates for key scenarios.

  • HTTPTrigger – Triggers some code when HTTP request received.
  • TimerTrigger – Performs a scheduled task like resources clean-up activities.
  • GitHub Webhook – Performs an action when something happened to GitHub repository.
  • Generic Webhook – Processes HTTP requests from services that support webhooks.
  • CosmosDBTrigger – Processes Cosmos DB documents when they get added or updated.
  • BlobTrigger – Processes Azure Blobs when they get added to containers.
  • QueueTrigger – Responds messages when they get added to Storage Queue
  • EventHubTrigger – Responds to Event Hub events
  • ServiceBusQueueTrigger – Responds to message when get added in Azure Service bus queue
  • ServiceBusTopicTrigger – Subscribes to Service Bus topics and respond to topic events.

Azure Function Apps

  • Function apps are containers of Azure Function.
  • All Functions serving the same business value can be grouped together under a single Function App.

Create Your First Azure Function App

  • Login to Azure Portal and click "New".
  • In search text box, write "Function App".

    Azure Function
  • Give a unique name to the Function app, select Subscription, Resource Group, and OS.
  • Select Hosting Plan

    • Consumption Plan – When the function runs, Azure sets up all necessary resources for you and you pay for the time that your code runs.
    • App Service Plan - Run Azure Function as a part of already existing App Service with no additional cost.

      Azure Function
  • Once the app gets created, you can view the newly created app under Function apps.

    Azure Function

Creating First Azure Function

  • Click on Functions tab available under Function App.
  • Click on "New Function".

    Azure Function

  • Select HTTP Trigger Template.

    Azure Function

  • Select language as C# and give some name to the Function and click "Create".

    Azure Function

Test Function Using Portal

  • You can test the newly created function using Azure Portal.
  • Click on the "Test" tab available at the right-hand side of portal.

    Azure Function

  • Click on "Run".

    Azure Function

  • Observe the "Output". 

    Azure Function

Test Function using Postman

You can test the Azure function using Postman.

  • On Azure Portal, click on ‘Get function URL’ option.

    Azure Function
  • Copy the URL. 
    Azure Function
  • Open Postman tool.
  • Paste the URL and set HTTP method as POST.
  • Click "Body" tab and select the raw option.
  • Select "Body Type" as application/JSON.
  • Add the following body.
    1. {  
    2.    "name":"Mangesh"  
    3. }  
  • Click on "Send".

    Azure Function
  • Observe the output.

    Azure Function