FABRIKAM
Adityo Pratomo
Mozilla Indonesia Rust
Meetup
FABRIKAMFABRIKAM
About Me
• CTO at Labtek Indie
• Technically, generalist
• Host of Rabu Rust
• Current interest: Kubernetes,
Microservice, Serverless, PWA and
Street Fighter V
FABRIKAM
Microservice
• An architecture pattern that divides
monolithic application into several
different services
• Each service is responsible for a smaller
amount context
• Each one is independent and can be
invoked separately also, required to
communicate between services
• In practice, each service is deployed in
different server and has its own
database and communication interface
Quick In tro ductio n
This Photo by Unknown Author is licensed under CC BY-SA
FABRIKAM
Serverless Application
• A server logic part of the application,
deployed inside a fully managed server,
normally in cloud -> BaaS (Backend as a
Service)
• A server logic part of the application,
deployed as an independent function that
can live only as long as it is being invoked
-> FaaS (Function as a Service)
• In both cases, application can be scaled
automatically, according to usage
• This frees up developer to fully
concentrate on developing application,
and not managing server
Some alternative definitions
4
FABRIKAM
Some Serverless Solutions
5
FABRIKAM
Serverless Solutions
• The aforementioned serverless solutions
are offered as a cloud-based
infrastructure offering
• Each has its own rule of what
environment they provide, what
languages they support and so forth
• Mostly accepted languages are:
• NodeJS
• Python
• Go
• PHP
Rule of the platform
6
FABRIKAM
How to get Rust
into Serverless
Platform?
7
FABRIKAM8
2 Main Solutions
embed into
Dockerize binary
FABRIKAM9
From Rust to Node in AWS Lambda
Function Handler
(NodeJS)
AWS API Gateway
Actual Function
(Rust)
• Implemented using Neon, a Rust crate that allows creation of
native Node modules from Rust code
• The Node code which implements the function handler, will call
this native module
• The Node code is called as usual JS script from the project root
(node .)
• Note that in order for Lambda function to work,it require a
function handler in Node
FABRIKAM10
From Rust to Node in AWS Lambda
// native/src/lib.rs
#[macro_use]
extern crate neon;
extern crate postgres;
use postgres::{Connection, TlsMode};
use neon::prelude::*;
fn hello(mut cx: FunctionContext) -> JsResult<JsString> {
Ok(cx.string("hello from Rust"))
}
register_module!(mut cx, {
cx.export_function("hello", hello)
});
// lib/index.js
const native = require('../native’);
exports.handler = (event, context, callback) =>
callback(null, native.hello());
FABRIKAM11
Deploy Rust Binary Inside Docker Container for Now Deployment
• Compile Rust into binary that fits Now’s requirement
• The easiest way is to build upon Now example and just deploy from that
• Deploy into Now network using the command now
• The serverless application will then live as an HTTP-based endpoint, ready to be used anytime
Rust binary Dockerized Now network
FABRIKAM
Demo
12
FABRIKAM
Reflection
Rust can be used to develop
serverless microservice with fast
response. However, it’s not yet
primed for production in leading
serverless platform. Docker-based
microservice is the most exciting
option for the time being
FABRIKAM
THANK YOU
A D I T YO P R ATO M O
+ 6 2 8 7 8 7 6 7 2 3 1 0 0
d i d i t @ l a b t e k i n d i e . c o m

Developing Serverless Microservice in Rust

  • 1.
  • 2.
    FABRIKAMFABRIKAM About Me • CTOat Labtek Indie • Technically, generalist • Host of Rabu Rust • Current interest: Kubernetes, Microservice, Serverless, PWA and Street Fighter V
  • 3.
    FABRIKAM Microservice • An architecturepattern that divides monolithic application into several different services • Each service is responsible for a smaller amount context • Each one is independent and can be invoked separately also, required to communicate between services • In practice, each service is deployed in different server and has its own database and communication interface Quick In tro ductio n This Photo by Unknown Author is licensed under CC BY-SA
  • 4.
    FABRIKAM Serverless Application • Aserver logic part of the application, deployed inside a fully managed server, normally in cloud -> BaaS (Backend as a Service) • A server logic part of the application, deployed as an independent function that can live only as long as it is being invoked -> FaaS (Function as a Service) • In both cases, application can be scaled automatically, according to usage • This frees up developer to fully concentrate on developing application, and not managing server Some alternative definitions 4
  • 5.
  • 6.
    FABRIKAM Serverless Solutions • Theaforementioned serverless solutions are offered as a cloud-based infrastructure offering • Each has its own rule of what environment they provide, what languages they support and so forth • Mostly accepted languages are: • NodeJS • Python • Go • PHP Rule of the platform 6
  • 7.
    FABRIKAM How to getRust into Serverless Platform? 7
  • 8.
    FABRIKAM8 2 Main Solutions embedinto Dockerize binary
  • 9.
    FABRIKAM9 From Rust toNode in AWS Lambda Function Handler (NodeJS) AWS API Gateway Actual Function (Rust) • Implemented using Neon, a Rust crate that allows creation of native Node modules from Rust code • The Node code which implements the function handler, will call this native module • The Node code is called as usual JS script from the project root (node .) • Note that in order for Lambda function to work,it require a function handler in Node
  • 10.
    FABRIKAM10 From Rust toNode in AWS Lambda // native/src/lib.rs #[macro_use] extern crate neon; extern crate postgres; use postgres::{Connection, TlsMode}; use neon::prelude::*; fn hello(mut cx: FunctionContext) -> JsResult<JsString> { Ok(cx.string("hello from Rust")) } register_module!(mut cx, { cx.export_function("hello", hello) }); // lib/index.js const native = require('../native’); exports.handler = (event, context, callback) => callback(null, native.hello());
  • 11.
    FABRIKAM11 Deploy Rust BinaryInside Docker Container for Now Deployment • Compile Rust into binary that fits Now’s requirement • The easiest way is to build upon Now example and just deploy from that • Deploy into Now network using the command now • The serverless application will then live as an HTTP-based endpoint, ready to be used anytime Rust binary Dockerized Now network
  • 12.
  • 13.
    FABRIKAM Reflection Rust can beused to develop serverless microservice with fast response. However, it’s not yet primed for production in leading serverless platform. Docker-based microservice is the most exciting option for the time being
  • 14.
    FABRIKAM THANK YOU A DI T YO P R ATO M O + 6 2 8 7 8 7 6 7 2 3 1 0 0 d i d i t @ l a b t e k i n d i e . c o m