Serverless Computing
- Serverless Computing is driven by performing some action as a response to a 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 set up 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 the application and infrastructure to run it.
- This is also called “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 scheduling 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 resource clean-up activities.
- GitHub Webhook – Performs an action when something happened to the 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 topical 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 the search text box, write "Function App".
- 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.
- Once the app gets created, you can view the newly created app under Function apps.
Creating First Azure Function
- Click on the Functions tab available under Function App.
- Click on "New Function".
- Select HTTP Trigger Template.
- Select language as C# and give some name to the Function and click "Create".
Test Function Using Portal
- You can test the newly created function using Azure Portal.
- Click on the "Test" tab available on the right-hand side of the portal.
- Click on "Run".
- Observe the "Output".
Test Function using Postman
You can test the Azure function using Postman.
- On Azure Portal, click on the ‘Get function URL’ option.
- Copy the URL.
- Open Postman tool.
- Paste the URL and set the HTTP method as POST.
- Click the "Body" tab and select the raw option.
- Select "Body Type" as application/JSON.
- Add the following body.