Serverless Functions
Accordingly, to Wikipedia "Function as a service (FaaS)" is a category of cloud computation service that provides a platform allowing 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.
Digital Enabler ClouDev is exactly this: a tool that allows to launch functions in several managed runtime environments.
The ClouDev user can then stay focused on the business logic of his application without taking care of the deployment, the availability or the resources consumption.
Getting started
The main instance of ClouDev is available at https://cloudev.demo.digital-enabler.eng.it
Please note that in order to use the tool you should be registered on the platform as a Developer.
After the login you will be redirected to the tool's main page that you can see below.
The page shows the currently deployed functions and allows you to create new ones through the top-right corner placed red button.
Functions
A Function is the main entity in ClouDev. It is a single-purposed logical unit that is hosted on the cloud infrastructure. The developer can use functions for various purposes like for example request some third-party service and retrieve data from it, connect to some Database, manipulate and analyze data..
Currently ClouDev allows to write functions for running in six different runtime environments:
- Python 3.8
- NodeJS 10
- Java 8
- Ruby 2.5
- Go 1.14
- PHP 7.2
Trigger
To invoke deployed functions, you need to associate a Trigger to that function.
A Trigger allows to make a function available through a HTTP Endpoint.
Note that one function can only be associated with one trigger.
So far, only HTTP triggers are available but several other triggers are in the ClouDev roadmap.
User manual
Create your first Cloudev function
First step for creating Cloudev function is to click on create button that is located on left upper side of the app landing page. After that new page will open where you can choose by clicking area of language the programming language in which you wrote your function code. Example of images below.
After choosing the programming language new page with code editor will appear.
On images you will see code editor where you can write code of your function or use the default example of code that already exist in editor for every language. Very important thing to mention is that every function must have main function also your function code could have more than one function that are invoked inside main function. On this example of function init is main function. Also, there are option to upload your code by clicking on folder icon that are located in upper right corner. Pop-up for uploading file will appear and you can select file with function that you want to upload. After uploading file your code will automatically appear in code editor. Very important rule for creating Java function is that name of the class must be the same as name of the function otherwise function won't work.
On left bottom side is located setting icon. By clicking on that icon new dialog will be opened.
There are two input fields.
- Timeout - for setting execution timeout of the function. By default, is 1000 ms. If a function takes more than that in being executed, that process will be terminated.
- Handler - need to contain main method of the function.
After click on save button you function will used new values for Timeout and Handler.
Also, you can click on Close button to close this dialog.
After that by clicking on save button new function will be deploy and you will be redirected to landing page.
Add dependencies
On page for creating function, you could find that are named in our case package.json. This is in correlation with the programing language that you choose. For example, if you choose Java this section will be named pom.xml instead of package.json and for PHP it will be name composer.json .
There is option to give dependency to your function by adding dependency by typing and uploading file. You can edit existing dependency or uploaded file in code editor. Also, you can add third-party library as dependency and used in your function code.
List Functions
Listing of deployed functions will be visible on landing page of Cloudev. In upper right corner there is search field for searching deployed dependency.
In the extension of the function name, you can find three dots.
By clicking on those dots new drop down many will appear as you can see on images below.
- Run for running code of deployed function.
- Details for redirection to function details page.
- Delete for delete function and triggers that are associated with that function
- Associate for associate that function to trigger and exposed through HTTP.
Function details
On Function details page you can see all details of deployed function.
Dependency that you uploaded for that function.
Runtime for that function ( Which runtime are using deployed function)
Version - specific version of that function how is generated automatically by ClouDev.
Handler - full name of function handler how is constructed ‘function name + name function main method that you defined in function created page’.
Timeout - execution timeout of the function
There are also subsection "Triggers" where is shown triggers that are association with function.
By clicking on the info icon it will redirect you to trigger details page.
Delete function
You can delete Cloudev function form landing page by clicking three dots and selection option delete.
After that new modal will appear.
By clicking on YES button function will be deleted and removed from the list. Button NO if you decided not to delete selected function.
Associate Triggers
If you want to associate a trigger to a function you can do it in landing page by clicking on three dots and selecting option associated. After that you pop-up messages will appear in upper lift side of application with message "function is successfully associated to trigger".
If function already have associated trigger user will get the error message "function is already associated with trigger".
You can check is trigger active by going to trigger details page.
Trigger Details
_To locate on trigger details page, you can do by clicking on the info icon in function details page in Triggers subsection.
After that you will be redirected to trigger details page which looks like as on image below.
You can see here three things.
First is a Trigger name that is constructed "username+workspace+function name + trg (extension)"
Second is Function subsection where you can find function that are associated with trigger.
The last one in HOST section where you can find the API where is function exposed trough HTTP.
To test is trigger working you can copy URL in HOST section and paste it to another tab in browser or you can just click it on link that will redirect you to HOST URL. If the everything works alright you should get response status 200 with some response context if your function is return some content (mostly it will be JSON response).
If server return response status 503 this means that function is not deployed yet and you need to wait a couple of second until to server finished deploying of your function.
Code Examples
Expose a REST API in NodeJS
module.exports = {
/* IMPORTANT: The entrypoint of the function is the "init" method.
Please be sure that your code contains one
*/
init: function (event,context) {
console.log(event);
return {
"body": event.data,
"header": event.extensions.request.headers,
"query": event.extensions.request.query,
"params": event.extensions.request.params
};
}
}
Analyze data in Python
Function code
def init(event, context):
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/fthuin/fakepeople/master/fake_people.csv')
df = df[['Gender','State','Kilograms']]
out = df.groupby(['State','Gender']).mean().reset_index()
return out.to_json(orient='records')
Dependency file
pandas
Connect to a DB in PHP
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
}
FAQ
How to update a function?
In cloudev there isn't option to update the function, but you could perform next set of operation to achieve same functionality as update.
First you must to delete current function and then choose the same runtime and re-created the function with same name but with different function code or with different dependencies. Then by clicking on create button you will be redirected to landing page and you can find there in function list your recreated function.
Why I receive the error "You are not authorized to use this tool"
In case after you log in to ClouDev you received this message on landing page "You are not authorized to use this tool" as you could see on image below.
This means that you don't have to proper role to use Cloudev. You must have assigned developer role to your account which will allow you to use Cloudev. To do this please contact your customer support on this email antonino.sirchia@eng.it.