Azure Functions Part 1
Azure Functions Part 1
• We will BUILD stuff during the lectures.... (its really not that difficult)
Azure Functions: End Project
• End Project : you can choose between one of 2 projects
• Last year – one fail – average mark was quite high
Metric Points
Cloud Database Architecture:
The application is designed with scalability of data services in mind. 20
N tier application
Consists of the Data Tier, Business Tier (service layer, validation and 5
business rules), presentation tier (not required)
Loosely coupled
Services are independent 10
• Part 2
• Azure Functions: Bindings (Input and Output)
• Azure Functions: Pipelines aka Chaining
• Azure Functions: Durable Functions
• Azure Functions: Fan Out – Fan In ?
• Azure Function: Human Interaction? (background info)
• Azure Functions: Error Handling Tips and Do’s and Dont‘s (background info)
Serverless: Definition
• The current working definition of serverless is as follows:
• Serverless fits use cases from simple web applications to massively scaled and complex
applications with millions of users.
• Yes!!
• Event driven workloads (to respond to events, such as HTTP requests, database changes, incoming messages, or timers.
Execute code in response to these events without the need to manage underlying infrastructure.
• POC
• Task scheduling
• IoT stuff
• Nooooooo!!
• No replacement for Web Api’s – long running processes
• Consistent Workloads – cost effective?
• Resource-Intensive Tasks: designed to be lightweight and stateless
• Complex State Management: app requires complex state management between function invocations, Azure Functions'
stateless nature might not be suitable. Consider using Azure Durable Functions or Azure Logic apps.
Monolith to Serverless
Monolithic App example
Monolithic to Serverless Functions
“Functional Decomposition”
Azure functions – The Concept
• Monolithic architecture – break/decompose/extend pieces off
the monolithic app
• Eg: payment in FAAS – puts message in queue
• Message placed in queue – generate email
• Payment placed in table storage
• Generate report (nightly - timer)
Azure functions – The Concept
• Code: just one HTTP function (C#/java/javascript/node.js/whatev’s)
• Just code to respond to EVENT
• Describe EVENT which will trigger the function
• Events eg: timer event – run every 5 min
Data arrives – queue, storage
Http request
No boilerplate code
Automatic scaling
Azure functions – Offerings
Functions are a FAAS -App service plan (built on webjobs)
Offer: Integreted security (OAuth)
Simplified integration (eg Sendgrid)
Certificates, domains, Auth, CI/CD
• Billing Function App is a Timer Trigger azure function. It will execute every day.
• Billing Function app will charge all the active subscribers who have passed the
billing date.
• Based on the charge success/failure response, it pushes a message in email-msg-
queue. This will trigger the Email Sending Function App to notify the subscriber
about billing.
• Billing Function App will push the message in invoice-generation-queue to
complete the billing.
• Also, it will trigger the Invoice Generation Function, and it will generate invoice
reports. Email Sending Function App will send the invoice reports to the service
owner.
Azure Serverless: Demo business case
source: https://www.altkomsoftware.com/blog/azure-functions
Azure Serverless: Demo business case
• User uploads CSV file with Beneficiaries to a specific data storage – Azure Blob Container.
• The above action triggers a function GenerateBillingItemsFunc that is responsible for:
– generating billing items, which use prices from an external database – CosmosDB and saving them in the table –
Azure Table;
– sending message about the need to create a new invoice to Azure Queue;
• When a new message appears on the queue, next function is triggered (GenerateInvoiceFunc). This function creates
domain object Invoice and saves this object in database – CosmosDB. After successful save, it sends a message to two
Azure Queues.
• When a new message appears in one of queues, function PrintInvoiceFunc is triggered. This function uses external
engine to PDF generation – JsReport and saves PDF file in Azure Blob Storage.
• When a new message appears in the second queue, function NotifyInvoiceFunc is triggered. This function uses two
external systems – SendGrid to email sending and Twilio to SMS sending.
• We tried to create functions with the best practices, i.e. they would be small, simple and would work independently.
• Note:
– FunctionName
– Authorizationlevel....
– Get and Post
– FunctionContext: Object that represents the context in which a function is executed, contains information about function
invocation, such as trigger details, invocation ID and logging capabilities
Azure Functions: Mini Workshop
(10 min)
• Build the Http triggered Azure Function as shown above
• Use .net 6.0 LTS in ISOLATED mode
• Test in Browser and/or Postman
Azure functions: Proposed Project Layout
Each Azure Function “lives” in its own project
Azure Functions: HttpTrigger
Deserilization
• Note: “cron like” way of timing -> hmmm where did I see this before?
• Timer fires every 1 minute
Azure Serverless: Azure Queue Storage
• Queue: KEY component in limitless scaling (truely loosley coupled architecture?)
• Characteristics:
FIFO
Point to Point communication
Async
• Popular queues:
Azure Storage Queue
Event Hubs: Big Data event streaming service - milj messages p/s – (IOT)
Event Grids: managed event routing – real or near real time services
Azure Service Bus: message broker (intermediary program module that
translates a message from messaging protocol of the sender to the messaging
protocol of the receiver. (wiki)
Azure Functions: Queue trigger
• Queue trigger: FunctionAppQueueDemo
• Add to queue in Storage explorer:
• NB: local.settings.json
Azure Functions: Queue trigger (contd.)
• Item is “dequeued”
Azure Functions: Mini Workshop Queue
(10 min)
• Download and install Microsoft Azure Storage Explorer
• Create a queue in the (local) Emulator - called “myqueue”
• Create Queuetriggered Azure Function which references
“myqueue”
• Insert an item into the Queue and check that trigger is fired
• Is the item dequeued?
10 min Break
• Lets get some coffee and NOT talk about programming or code
Azure Functions: Dependency injection
• DI: object receives dependant object through injection (mostly
ctor injection)
• injecting services, ideal for writing N-tiers layers, and unit tests
(esp. “mocking”)
• Registering of Dependencies is done in Programs.cs eg:
Azure Functions: Dependency injection
Demo
• AzureFunctionDependencyInjection
• Steps: NB: remove static in class def
• Register services in Program.cs
• Note:
FakeDB : “database” faked as a List!!
Azure Functions Mini Workshop
(est. time 30 - 45min)
• Design app conform N-Tier architecture
• Business layer contains Order (prop’s OrderId, OrderName)
• Create IOrderRepo with Create and getOrder methods
• Create IOrderService which implements the OrderRepo
• Implement dependency injection
• Implement Azure functions to get all Orders and add a Order to
the Repo (List)
DI and IOC
• Azure Functions DI allow N-tier architecture
• Building of loosely coupled applications
• Allows for Unit testing
Azure Functions: Demo code
• https://github.com/asc-lab/azure-functions-billing
•
• https://github.com/chsakell/azure-functions-proxies