FaaS : Function as a Service
Tags: faas, azure, aws, serverless
Published: 2022-08-13 09:00:00

**Function as a service (FaaS)** is a cloud computing services providing a platform to customers to develop, run, and manage application functionalities, without the complexity of building and maintaining the infrastructure associated with developing and launching an app.

Building an application following this model is one way of achieving a **serverless architecture** and is typically used when building microservices applications. Use cases for FaaS are associated with _<u>"on-demand" functionality</u>_ that enables the supporting infrastructure to be powered down and not incur charges when not in use.

**AWS Lambda** was the first FaaS offering by a large public cloud vendor, followed by Google Cloud Functions, Microsoft Azure Functions, IBM/Apache's OpenWhisk (open source) in 2016 and Oracle Cloud Fn (open source) in 2017.

Let's understand how a function work with a serverless framework, taking **Azure Functions** into consideration :

Serverless Functions execute code—written in the language of one's choice with Azure Functions, with an <u>_event-driven compute_</u> experience. 
Azure Functions scales on demand and one pays only for the time your code is executed. 
Available as a managed service in Azure and Azure Stack, the open source Functions runtime also <u>_works on multiple destinations_</u>, including Kubernetes, Azure IoT Edge, on-premises, and even in other clouds.

A Function should be written <u>_to perform a single job_</u> such as:
    Saving a user to the database
    Processing a file in a database
    Performing a scheduled task
<u>_Separation of concerns_</u> should be taken into consideration

Few **sample scenarios to implement Azure Functions** :

![Sample scenarios set to implement Azure Functions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pa5wnw6a5ap4ajigdrq8.png)
 
Anything that triggers an Azure Function to execute is regarded as an **Event** or **Trigger**. A trigger defines how a function is invoked and a function _<u>must have exactly one trigger</u>_. Triggers have associated data, which is often provided as the payload of the function.
<u>_Few Triggers on Azure Functions are :_</u>
- An HTTP Trigger (e.g., for a REST API)
- A scheduled timer (e.g., run every 5 minutes)
- A Service Bus Queue trigger (e.g. a workitem from another Function)
- An IoT/Event Hub message (e.g., a message from a device or service)
- A Webhook fires (e.g., Github project update)
And many more..

**Binding** to a function is a way of declaratively connecting another resource to the function; bindings may be connected as _<u>input bindings, output bindings, or both</u>_. Data from bindings is provided to the function as parameters.
You can mix and match different bindings to suit your needs. <u>_Bindings are optional_</u> and a function might have <u>_one or multiple_</u> input and/or output bindings.
<u>_Like, Blob Storage, Queue, CosmosDB etc_</u>

Triggers and bindings let you avoid hardcoding access to other services. 
Each function has its own code file and binding configuration file as **function.json** where triggers are triggers are configured.
(used along different languages like Java, Javascript, Python, C#).

`{
  "scriptFile": "azure-functions-example.jar",
  "entryPoint": "com.example.Function.echo",
  "bindings": [
    {
      "type": "httpTrigger",
      "name": "req",
      "direction": "in",
      "authLevel": "anonymous",
      "methods": [ "GET","POST" ]
    },
    {
      "type": "http",
      "name": "$return",
      "direction": "out"
    }
  ]
}
`
**AWS Lambda** has similar use case and can cater to file processing, stream processing, web applications, IoT backends and mobile backends.
One such scenario is illustrated as below - <u>_File Processing_</u> :

![AWS Lambda File Processing](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bxty6tjduhxh3h73duv.png)

Amazon Simple Storage Service (Amazon S3) to trigger AWS Lambda data processing in real time after an upload, or connect to an existing Amazon EFS file system to enable massively parallel shared access for large-scale file processing.

<u>References :</u>
[docs.microsoft.com/azure-functions](https://docs.microsoft.com/en-us/azure/azure-functions)
[aws.amazon.com/lambda/](https://aws.amazon.com/lambda/)
[serverless.com](https://www.serverless.com/framework/docs)
[azure.microsoft.com/serverless/](https://azure.microsoft.com/en-gb/solutions/serverless/)

[Wikipedia - Serverless Computing](https://en.wikipedia.org/wiki/Serverless_computing)
[Wikipedia - FaaS](https://en.wikipedia.org/wiki/Function_as_a_service)
[Wikipedia - Microsoft Azure](https://en.wikipedia.org/wiki/Microsoft_Azure)

<u>Image Resources :</u>
[AWS Lambda - File Processing](https://d1.awsstatic.com/product-marketing/Lambda/Diagrams/product-page-diagram_Lambda-RealTimeFileProcessing.a59577de4b6471674a540b878b0b684e0249a18c.png)