What Is FaaS and Why Is It Important?
FaaS stands for Function as a Service. It is a category of cloud computing services that provides a platform which allows customers to develop, run, and manage application functionalities without the complexity of building and maintaining the infrastructure typically associated with developing and launching an app. FaaS is often considered a part of serverless computing, but serverless computing also includes other services like Backend as a Service (BaaS).
Here's why FaaS is important:
1. Scalability: With FaaS, the service provider automatically manages the scaling of functions. Each function call is handled by a separate instance of the function, allowing the system to scale with demand seamlessly and instantly, from a few requests per day to thousands per second.
2. Simplified Deployment: FaaS abstracts much of the deployment infrastructure. Developers can focus on writing code for individual functions without worrying about the underlying operating system, server configuration, or maintenance.
3. Event Driven: Functions are typically executed in response to events or requests. This makes FaaS a good fit for scenarios such as processing data streams, handling web requests, or integrating with other cloud services.
4. Faster Time to Market: With less infrastructure to manage, developers can push code to production faster. This rapid deployment cycle can be critical for businesses looking to quickly adapt to market changes or launch new features.
5. Microservices Architecture: FaaS fits well with the microservices architecture by allowing developers to create and deploy granular functionalities, making applications easier to scale and update.
6. Reduced Operational Management: Since the cloud provider is responsible for the servers, runtime, and infrastructure, developers and IT teams have fewer operational concerns and can focus on development and innovation.
Steps to Create a FaaS Function on E2E Networks
Click on Functions under the Compute tab on https://myaccount.e2enetworks.com/
E2E currently supports writing functions in NodeJs, Python, C#, and Ruby. Let’s write a function in Python and do a breakdown.
In the context of Function as a Service (FaaS) and the provided Python code, let's discuss the event and context parameters, how they're used in the `handle` function, and the automatic header setting based on the type of response.
The `event` Parameter
The `event` parameter contains data about the incoming HTTP request that triggered the execution of the function. This data includes:
- `body`: The raw request payload, which is typically in bytes. In the context of this code, the `body` is not directly used since the function expects data to be passed through query parameters.
- `headers`: A dictionary of the request headers. This code does not make use of headers directly, but headers could be used for authentication, content type determination, etc.
- `method`: The HTTP method (e.g., GET, POST, PUT, DELETE). In this code, the HTTP method is not checked; however, you could expand the function to behave differently based on the method (e.g., GET for retrieving data, POST for creating data).
- `query`: A dictionary of query string parameters. The code uses `event.query['number']` to extract the 'number' parameter for the factorial calculation.
- `path`: The path of the request URL. This is not used in the example but can be useful for routing within the function based on the requested endpoint.
The `context` Parameter
The `context` parameter includes basic information about the function's runtime environment, such as:
- `hostname`: The name of the host/server handling the request. In this code, the `context` is not utilized, but it could be referenced for logging, debugging, or adapting the function's behavior based on the host.
Response and Automatic Header Setting
The function processes the event data and constructs a response that includes:
- `statusCode`: The HTTP status code to be sent back to the client. `200` indicates success, and `400` indicates a bad request.
- `body`: The content of the response. The body should be a dictionary or a string.
- If a dictionary is provided, as in the success case where the result of the factorial calculation is returned, the FaaS platform will serialize the dictionary to JSON and automatically set the `Content-Type` header to `application/json`.
- If a string is returned, as in the error case where the function informs that the number was not provided, the FaaS platform will treat it as HTML and set the `Content-Type` header to `text/html; charset=utf-8`.
In the given code, the `handle` function checks if the 'number' query parameter is present. If so, it calculates the factorial of the number and returns a JSON response with a `200` status code. If the 'number' is not provided, it returns an error message in plain text with a `400` status code.
It is important to note that while the `body` in the successful response is shown as a dictionary, it should be serialized to a JSON string using `json.dumps()` before returning, to adhere to the requirements of the HTTP protocol and the FaaS platform's expected format.
—
You can add dependencies to be installed that are needed for your function in the requirements.txt. Also if there are any environment variables like API keys that are needed for the execution of the function, they can be added as follows:
Then you can launch your function and you’ll receive an API endpoint URL for it as follows:
You can query this API endpoint with curl requests. Let’s send in a few requests and look at the results:
We get the answer as 120, which is indeed a factorial of 5.
We can modify our handle function to receive information about the context.
Now when we send a curl request,
{"factorial": 3628800, "context_info": {"hostname": "e2e-function-33711-69fbbfb978-nfp6p"}}
we see the metadata of the hostname for our function.
Final Words
Using E2E Networks to deploy this function demonstrates the simplicity and power of FaaS. With a provided API endpoint, users can make requests to the function and receive computed results in real-time. This not only streamlines the development process but also ensures that resources are efficiently used, as the infrastructure scales automatically with the demand for the function.
For more information, refer to: https://docs.e2enetworks.com/faas_doc/faas.html