How to use multiple runtimes in a single serverless microservice
Tags: serverless, microservices, runtimes, serverlessframework
Published: 2022-04-27 16:50:15

Originally posted at [Serverless](https://www.serverless.com/blog/building-mutliple-runtimes) 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](https://github.com/serverless/examples/tree/master/aws-multiple-runtime)

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:
{% gist https://gist.github.com/richardgrantserverless/375cb61d78312728b12c54a90676a37d.js %}
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:
{% gist https://gist.github.com/richardgrantserverless/1f3847e81747bae937206c860ab5c9a2.js %}
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:
{% gist https://gist.github.com/richardgrantserverless/c85912b6a064c7bcf5e13f6f1b246583.js %}
The other function is a Node-backed endpoint that reports a timestamp:
{% gist https://gist.github.com/richardgrantserverless/4daba5427afe6f5e04e765778f6ffaff.js %}
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:
{% gist https://gist.github.com/richardgrantserverless/6b266a5af0ce531d454776e8c1309be8.js %}
Deploying the service with serverless deploy tells us the URL of the page at /greet.
{% gist https://gist.github.com/richardgrantserverless/2ffc3f042e5e7166a735b0b94a077efb.js %}
Accessing the page shows the greeting message and tells us the date.

![](https://cdn-images-1.medium.com/max/2212/0*pLclwV9dGJmtEJlS.png)

To try it out, download [the project files](https://github.com/serverless/examples/tree/master/aws-multiple-runtime) and run serverless deploy from the directory that contains serverless.yml.

![](https://cdn-images-1.medium.com/max/2000/1*NCHqjhEtW2FQQm5NO0ekAg.png)

*Originally published at [https://www.serverless.com](https://www.serverless.com/blog/building-mutliple-runtimes).*