Logo

dev-resources.site

for different kinds of informations.

How to use multiple runtimes in a single serverless microservice

Published at
4/27/2022
Categories
serverless
microservices
runtimes
serverlessframework
Author
goserverless
Author
12 person written this
goserverless
open
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.

serverlessframework Article's
30 articles in total
Favicon
Build a highly scalable Serverless CRUD Microservice with AWS Lambda and the Serverless Framework
Favicon
Building Scalable Event Processing with Fan-out Pattern using the Serverless Framework
Favicon
Importing CloudFormation Resources to help fix deployments to Production
Favicon
How to add Amazon Cognito Auth to a Web App (part 4)
Favicon
Setting up for Serverless Development with AWS
Favicon
Basic Integration Testing with Serverless Framework
Favicon
Building an API with Ruby and the Serverless Framework
Favicon
How to use multiple runtimes in a single serverless microservice
Favicon
ShreyTheCray Interview: Serverless Framework & Cloud Explained
Favicon
Introducing multi-service deployments via Serverless Framework Compose
Favicon
Simple Serverless Scheduler
Favicon
Serverless (FaaS) vs. Containers — when to pick which?
Favicon
Provisioned Concurrency: What it is and how to use it with the Serverless Framework
Favicon
AWS Lambda Function URLs with Serverless Framework
Favicon
The ABCs of IAM: Managing permissions with Serverless
Favicon
AWS Lambda Destination Support
Favicon
Managing Stages and Environments in Serverless Framework
Favicon
Using API Gateway WebSockets with the Serverless Framework
Favicon
How to Create a REST API with Azure Functions and the Serverless Framework — Part 1
Favicon
How to deploy multiple micro-services under one API domain with Serverless
Favicon
How to Test Serverless Applications
Favicon
DynamoDB On-Demand: When, why and how to use it in your serverless applications
Favicon
The definitive guide to using Terraform with the Serverless Framework
Favicon
Serverless Framework example for Golang and Lambda
Favicon
Using SQS with AWS Lambda and Serverless
Favicon
Container Image Support for AWS Lambda
Favicon
Major changes to serverless-dotenv-plugin
Favicon
Using dotenv with the Serverless Framework
Favicon
API Gateway WebSocket APIs with the Serverless Framework
Favicon
Takeaways from using AppSync

Featured ones: