The server is dead,

long live the λάμδα!

About me

Mark

twitter: @markbaraban

github: @shmuga

How many of you can handle 1 RPS?

... and scale it to

1000 RPS without literally doing nothing

diagram from cloudcraft

...and after 5 minutes downgrade it to

1 RPS 

again

Calm down.

Take it easy.

But what is

AWS Lambda?

AWS Lambda

Serverless architectures refer to applications that significantly depend on third-party services (knows as Backend as a Service or "BaaS") or on custom code that's run in ephemeral containers (Function as a Service or "FaaS"), the best known vendor host of which currently is AWS Lambda.

Martin Fowler

exports.handler = (event, context, callback) => {
    console.log(JSON.stringify(event));
    callback(null, 'Done!');
};

All you need to create a function:

Who can fire an event

  • Api Gateway
  • CloudWatch
  • SNS
  • SQS
  • DDB
  • Alexa
  • and other

event

is your payload (in other words json)

it's configured on the level of service who fires it

context

is information about currently called function and it's parameters

callback

is old-school technology to handle async actions

Ok, so what about performance.

Performance

is tightly connected with

Price

Each function has

  • provisioned memory (RAM from 128Mb up to 1536Mb) 
  • time it's working ( minimum 100ms)
  • network/cpu is connected with RAM.

Everything is calculated in

GB-seconds

Example 1:

You have function been called 

100 000 times with 128Mb RAM parameter and execution time was 1s

 

so you have used 0.128 * 100 000 = 12800 GB-seconds

 

and price per 1GB-second = 0.00001667

So you pay 0.21$

Actually NO.

You pay nothing.

product owner counting saved money

Free tier

400 000 GB-second*

*is free permanently 

For what it's enough

400 000 GB-seconds is enough for 128Mb RAM function to work whole month

If your function works

 

whole month with

 

128Mb RAM it costs 5$.

Environment

  • C#

  • Java 8

  • Python 2.7

  • Python 3.6

  • Node 4.3

  • Node 6. 10

1 time a year

update cycle of node version

But how can i make simple HTTP(s) call?

API Gateway

you just say you want to call function GetUser

 

when you receive call such call

POST /get-user

API Gateway

out of the box stuff

  • cache
  • authorized endpoints 
  • custom domains
  • free https certificates
  • Swagger
  • throttling

How can I manage

logs

of functions?

CloudWatch

Each function call creates CW log group.

 

Each console.log() call puts records into this log. 

CloudWatch

out of the box stuff

  • metrics and custom filters
  • alarms
  • notifications

Simple metric

Simple alarm

How can I manage

state or use DB

for functions?

ddb

You can easily use any lib/db.

But DynamoDB is preferred.

DynamoDB

is key-value auto-scalable DB with atomic operations.

You can use Lambda

  • to handle http
  • to call it periodically 
  • to handle some queue
  • to use it as trigger to DB/S3
  • pipe lambda functions
  • and other.

Automation.

Title Text

Tool #1. CloudFormation

1k lines of json config

to deploy 1 function.

Tool #2.

Serverless config

general information

service: Users

provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 512
  timeout: 20
  region: eu-west-1
  profile: default
  role: arn:aws:iam::XXX:role/APIRole

Serverless config

function definition

functions:
  addUser:
    handler: functions/add-user.default
    name: ${self:service}AddUser-${opt:stage, self:provider.stage}
    description: Adds user
    events:
      - http:
          path: /user/add
          method: post
          authorizer:
            arn: arn:aws:lambda:eu-west-1:XXX:function:Authorizer
            resultTtlInSeconds: 600
            identitySource: method.request.header.AuthToken

Serverless config

other options

package:
  individually: true
  exclude:
    - test/**
    - .git/**
    - .vscode/**
    - .idea/**
    - yarn.lock
    - node_modules/**/README.*
    - node_modules/**/test/**

plugins:
  - serverless-plugin-webpack

PROS

  • auto scaling
  • force you to think in small functions
  • force you to use less modules
  • good for decomposition
  • pay as you go
  • minimizing DevOps

CONS

  • not suitable for real highload
  • vendor lock
  • no connection pool
  • no state?
  • needs some skills with aws
  • no websockets

Bonuses

Happy Hacking!

λάμδα

By Mark Orel

λάμδα

  • 224
Loading comments...

More from Mark Orel