Introducing serverless (full-code) functions

· 5 min read
Ümit Çakmak

We are excited to announce the release of serverless (full-code) functions. With full-code functions, you write simple, single-purpose functions (e.g., custom code) that are attached to events emitted from your backend application. Your function is triggered when an event being watched is fired and your code is executed in a fully managed environment. There is no need to provision any infrastructure or worry about managing any servers.

Altogic full-code functions provide an additional layer of logic that lets you write custom code to extend your app's cloud services. Full-code functions augment existing no-code services and allow you to address an increasing number of use cases with arbitrary programming logic.

Each serverless function can be used to handle requests triggered from the below channels:

  • Endpoints: You can direct RESTful API requests of an endpoint to your function
  • Message queues: You can direct the processing of messages submitted to a queue to your function
  • Cron jobs: You can schedule tasks that run at specific times or periods and direct the processing of these tasks to your function

Full-code functions can be deployed to your application's environment. You can create several environments for your apps at dozens of regions across the world. You can deploy your full-code functions to a single app environment or to multiple environments to improve latency and availability and meet your development needs.

Creating full-code functions

To create and deploy your full-code functions, you can use Altogic CLI, which is the command line interface specifically designed to manage your serverless functions.

When writing your own Altogic serverless function, you must export the code in certain ways. To deploy a serverless Node.js API, create a function in a .js file within the /src directory at the root of your project. Below is an example Node.js full-code function using Express.js-like helper methods from the Request and Response objects.

~ /src/handler.js
module.exports = async function (req, res) {
res.json({
quote: "Hello world!",
});
};

You can also include a package.json file along with your function code and import any modules you would like to use in your full-code function.

When your function is called, you receive two parameters, a request and a response object. The request object contains all data that was sent to the function. The response object has two methods, send(text, status = 200) and json(json, status = 200) that can be used to send data back to the calling party. If the function is triggered by calling an endpoint, the response returned by send() or json() is returned to the calling party. However, if the function is triggered by a message queue or cron job, there is no tangible calling party, the response returned by send() or json() method is added to the log records.

Deploying your function

You can deploy your serverless functions using Altogic CLI. Make sure you have installed Altogic CLI and you have successfully logged into your Altogic account. Please ensure you are in the same folder as your altogic.json and run altogic deploy to deploy your function. You will be prompted to select which environment to deploy if you have multiple execution environments.

$ altogic deploy

When you run the deploy command, Altogic creates a new image and applies this image to your app's execution environment. Depending on the size of your code and its dependencies, it may take a couple of minutes to build and deploy your function. You can run altogic get builds and altogic get deployments to get the status of your builds and deployments respectively.

Linking your function to triggers

After writing and successfully deploying your function to your execution environment, you need to link your function to an endpoint, message queue, and/or scheduled task (cron job). You can use Altogic Designer to configure your endpoint, message queue, and cron job handlers. After linking your functions, you can invoke them by sending a RESTful API request to the linked endpoint, submitting a message to the linked queue or they can also be invoked by the linked scheduled task (e.g., cron-job) trigger.

Local testing of your functions

You can test your node.js runtime functions locally by running the start command. This command will launch a local HTTP server and provide you the endpoint (URL) of the function. The start command has hot-reloading capabilities. It will watch for any changes to your files and restart the HTTP server.

$ altogic start

Local development HTTP server running at port:4000.
You can now test your function using the following endpoint: http://localhost:4000

Testing deployed functions

You can use Altogic Tester or any other API testing tool (e.g., Postman) to test your serverless functions. In your function code, you can log messages using console.log, console.info, console.warn, console.error and, console.debug methods. Upon execution of your deployed function, these log messages are also returned in response body.

If you have any questions about full-text search or want to share what you have built, please post a message in our community forum or discord channel.