DEV Community

Cover image for How to use multiple runtimes in a single serverless microservice
We're Serverless! for Serverless Inc.

Posted on • Originally published at serverless.com

How to use multiple runtimes in a single serverless microservice

Originally posted at Serverless on September 15th, 2017

As a developer on the cloud, there are many tools at your disposal. The Serverless Framework supports an array of runtimes to enable you to use different languages in your Serverless application.

To manage these related codebases, you might choose to divide your application’s functions into a number of different services. However, if you prefer to deploy a single service for all your functions, regardless of what language they are written in, the Serverless Framework empowers you to do just that.

Let’s consider a small application that uses two runtimes and provides two functions. This example will use Python and Node targeting AWS, but the concepts will be broadly applicable in other circumstances. The full the project files can be found here

We’ll create an application that has an endpoint that reports the current system timestamp, and a web controller that displays the time in the browser. The configuration will look largely similar to a single-runtime application. We specify the name of the service and the target provider in our serverless.yml:


Note that I omitted the usual declaration of runtime inside the provider section. If you specify it here, it will serve as a fallback for any functions that do not have a runtime specified individually.

Here I specify a function that will render the webpage markup:


This web controller is a Python module, so I specify the python3.6 runtime. The handler field points to the module located in my project at web/handler.py and names the function hello as the handler for received events.

Here’s what the implementation looks like:


The other function is a Node-backed endpoint that reports a timestamp:

Again, this looks the same as in a single-runtime service, with the exception that it specifies the runtime nodejs6.10 alongside the function declaration. The module for this function is located at api/handler.js, and exports a function named timestamp. It is not necessary to move files of different languages to separate folders, but depending on complexity and build procedure, you may find it useful.

The function responds with the millisecond timestamp:


Deploying the service with serverless deploy tells us the URL of the page at /greet.

Accessing the page shows the greeting message and tells us the date.

To try it out, download the project files and run serverless deploy from the directory that contains serverless.yml.

Originally published at https://www.serverless.com.

Top comments (0)