Understanding serverless and

AWS Lambda within NodeJS examples

Artem Arkhipov, Lead Web Developer at Techmagic

What is serverless ?

It is not about NO servers.

It means you don't care about them so much

BaaS

FaaS

Backend as a Service

Function as a Service

Rich client app

User provisioning

Cloud database

Client app

API Gateway

Function 1

Function 3

Function 5

Function 2

Function 4

Function ...

Function as a Service

- Event-driven

- Pay as you go

- Less about infrastructure
  more about code

- Burst auto-scaling

AWS

GOOGLE

MICROSOFT

IBM

AWS Lambda

Supported languages:

Execution time:

15 minutes

Resources available:

128 - 3008 MB memory

500 MB virtual /tmp folder 

1024MB 

Pricing example:

Free: 400 000 seconds and 1000000  requests Then: 0.000001667$ per 100ms exec time
           0.20$ per 1000000 requests

Function code

const smthng = require('smthng');

exports.handler = function(event, context, callback) {

    const val1 = event.key1 || 0;
    const val2 = event.key2 || 0;
    const result = sum(val1, val2);
    
    callback(null, result);
}

function sum(a, b) { return a+b };

event 

context 

callback 

Event data

Information about current executing 

Function used to pass err or data to caller

Autoscaling demo

vs

//create some dumb load on machine
for (let i = 0; i <= 100000; i++) {
  let to = setTimeout(() => {
      let val = Math.random()*Math.random();
      clearTimeout(to);
      if (i === 100000) {
        callback(val);
      }
  }, 4)
}

5 users online

50 users online

Simply about containers

Cold container, just spinned up, its first invocation

Warm container, is ready to immediately handle event

Busy container, function is executing just now

You still pay only for execution time

( it is not official terminology )

Cold invocations usually take a bit more time

No events for a quite long time

Load is low

Load increases

Cristiano Ronaldo twitted a link to your app

By default You can have up to 1000 concurrent executions

But if you really need, AWS will increase limits for free

But, unfortunately, nobody liked it:

And some time later:

Tips about container reuse

- AWS automatically creates and destroys containers itself

- Remember that you can not depend on container reuse

- Cold starts are longer than warm, it depends on initialization time and size of your code (zip archive).

- Your functions should be stateless

- You can run background processes, keep database connections, etc if they are outside of your handler function

- If Cristiano tweets your app - AWS Lambda saves you

Lambda triggers & usecases overview

API Gateway

- Your API

- Web-hooks from 3d party services (git, jira, payments, etc)

S3 Events

- Uploaded files processing (image cropping, file converting)

Cloudwatch events and logs

- Scheduled tasks (report generating, automated backups)

- Infrastructure management

- Log analysis

DynamoDB Table updates

SES activities

SNS Notifications

and more...

Serverless framework

$ npm install -g serverless
$ sls create --template aws-nodejs my-service

Install

Create service

$ cd my-service
$ sls deploy

Deploy service

Moment from real experience

Transform this:

To this:

High level overview of the solution

You may run executables on Lambda 

- Remeber to unref child processes and use /tmp dir as cwd

- Binaries should be compiled for matching version of linux

- Don't exceed package limitations

Move to serverless =)

Serverless community

@serverless.ukraine

#makeserverless

Ukraine

Let's become serverless heroes together!

Artem Arkhipov, Lead Web Developer at Techmagic

ar.arkhipov@gmail.co

skype:  tamango92

twitter: ar_arkhipov

Understanding AWS Lambda (meetup)

By Artem Arkhipov

Understanding AWS Lambda (meetup)

What is serverless? What is AWS Lambda? How lambda autoscales and what is container? Few examples and usecases. Serverless Community Ukraine

  • 483