LESSONS LEARNED FROM AWS LAMBDA

@pierodibello

No ops, just code

Lessons learned from

@dedaniel_xp

Intro

WHAT IS AWS?

CLOUD COMPUTING PLATFORM

AMAZON WEB SERVICES

what does it offer?

  • COMPUTE

  • networking

  • storage

  • DATABASE

  • SECURITY

  • DEVELOPER TOOLS

  • MANAGEMENT TOOLS

  • MANY OTHERS

Full architecture case

serverless architecture

What, why, how

WHAT IS LAMBDA?

  • NO-OPS

  • EVENT DRIVEN

  • SCALE AUTOMATICALLY

  • ONLY PAY FOR WHAT YOU USE

SERVERLESS COMPUTE SERVICE

NO-OPS

  • Server and OS maintenance

  • Server provisioning

  • Monitor and logging

ZERO ADMIN

EVENT DRIVEN

  • Events coming from AWS resources (S3, DynamoDB, KINESIS, SNS, CLOUDWATCH, ...)

  • HTTP requests VIA API GATEWAY

RUN CODE IN RESPONSE TO EVENTS

HIGH AVAILABILITY

  • Your code runs in response to every trigger within milliseconds

  • Lambda will scale automatically TO MATCH THE INCOMING LOAD

JUST UPLOAD YOUR CODE

ZERO DOWNTIME DEPLOYMENT

  • EACH LAMBDA IS VERSIONED

JUST UPLOAD THE NEW VERSION OF YOUR FUNCTION

http://docs.aws.amazon.com/lambda/latest/dg/versioning-aliases.html

ONLY PAY FOR WHAT YOU USE

You are charged based on

  • the number of requests for your functions, plus

  • the time your code executes and the memory it allocates

 You don't pay anything when your code isn't running.

FREE TIER FOR ALL!

YOU GOT FOR FREE EACH MONTh:

  • The first 1 million requests

  • The first 400,000 GB-seconds of compute time

PRICING

  • $0.20 per 1 million requests thereafter

  • $0.00001667 for every GB-second

USE YOUR OWN LANGUAGES

  • Node.js (v0.10, v4.3)

  • Java 8

  • Python 2.7

  • ...and more to come

NO NEW LANGUAGES/FRAMEWORKS TO LEARN

HEAD TOWARDS MICROSERVICES

  • independently deployable

  • PICk YOUR LANGUAGEs

  • NANOSERVICES?

each function is a SMALL service

All that glitters is not gold 

  • THE Execution Environment IS OPAQUE

  • COLD STARTUP

Some things you get TO BE AWARE OF WITH AWS Lambda

THE EXECUTION ENVIRONMENT IS OPAQUE

  • don't keep state on the container

  • don't make assumptions on the Linux OS under the hood

  • don't assume two function calls will land on the same container

BE STATELESS

THE EXECUTION ENVIRONMENT IS NOT UNDER YOU CONTROL

  • With Lambda you cannot log in to MACHINES, or customize the operating system or language runtime

  • You exchange this flexibility with the “no-ops, just code” paradigm.

  • If you need control, choose a IAAS solution

COLD STARTUP

  • Java is the slowest in the startup, the fastest once warmed

  • python is the fastest to startup

The first call to a function could take longer

be ready to think in microservices!

  • longer deploys

  • too much coupling between functions

If you think your app as a monolith, you'll have the same monolith FOR each and every function

FROM DEV TO PRODUCTION

you can have as many different stages as you want, using named alias to different versions of your lambda

ALTERNATIVES TO AWS LAMBDA

  • GOOGLE CLOUD FUNCTIONS

  • MICROSOFT AZURE FUNCTIONS

https://cloud.google.com/functions/

https://azure.microsoft.com/en-us/services/functions/

AWS LAMBDA vs HEROKU

  • heroku: FOCUS ON THE application level

  • LAMBDA: FOCUS ON THE single function level

paas vs serverless

HOW TO LAMBDA

OUR PROJECT BUILT ON LAMBDA

  • THE PROJECT

  • THE architecture

  • THE CODE

  • LESSONS LEARNED

A SOCIAL NETWORK FOR fashion PHOTOGRAPHERS

THE PROJECT

  • ~ 150k photographers

  • ~ 400k PHOTOS

  • ~ 3k uploaded images each day

  • 2 photo editors

A SOCIAL NETWORK FOR fashion and ART PHOTOGRAPHERS

The Architecture

The client is an AngularJS app

THE LAMBDA IS IN NODE.JS

introducing the serverless framework

Build web, mobile and IoT applications with serverless architectures using AWS Lambda and API GATEWAY

serverless framework

  • HELPS IN DRIVING YOUR DEVELOPMENT ON A CLEAR PATH, HIDING ALL THE WORK NEEDED TO CONFIGURE, WIRE AND UPLOAD A LAMBDA TO AWS

  • RUN/TEST AWS LAMBDA FUNCTIONS LOCALLY, OR REMOTELY

  • AUTO-DEPLOYS, VERSIONS & ALIASES LAMBDA FUNCTIONS AND API ENDPOINTS

  • INTERACTIVE CLI DASHBOARD TO EASILY SELECT AND DEPLOY FUNCTIONS AND ENDPOINTS

The code: a function definition

{
  "name": "photosAll",
  "customName": false,
  "customRole": false,
  "handler": "photos/photosAll/handler.handler",
  "runtime": "nodejs4.3",
  "timeout": 6,
  "memorySize": 1024,
  "custom": {},
  "endpoints": [
    {
      "path": "photos",
      "method": "GET",
      "type": "AWS",
      "authorizationType": "none",
      "authorizerFunction": false,
      "apiKeyRequired": false,
      "requestParameters": {},
      "requestTemplates": "$${apiRequestTemplate}",
      "responses": "$${apiResponses}"
    }
  ],
  "events": [],
  "vpc": {
    "securityGroupIds": ["${securityGroupId}"],
    "subnetIds": ["${subnetId}"]
  },
  "environment": "$${environment}"
}

The code: a Handler

'use strict';

var PhotoRepository = require('../../lib/photo/PhotoRepository');
var PhotographerRepository = require('../../lib/photographer/PhotographerRepository');
var PhotoDetail = require('../../lib/photo/PhotoDetail');
var LambdaRequest = require('../../lib/LambdaRequest');
var Auth = require('../../lib/Auth');

module.exports.handler = function(event, context) {
  var request = new LambdaRequest(event, context);
  var loggedPhotographer;
  Auth.getUser(request.getParams())
  .then(function(user) {
    if (!user) {
      return null;
    }
    return PhotographerRepository.findByUserId(user.getId());
  })
  .then(function(photographer) {
    loggedPhotographer = photographer;
    return PhotoRepository.findAllApprovedBy(request.getParams());
  })
  .then(function(collection) {
    var jsonCollection = collection.toJSON();
    jsonCollection.items.map((photo) => PhotoDetail(photo, loggedPhotographer));
    return jsonCollection;
  })
  .then(request.succeed)
  .catch(request.fail)
  .finally(request.tearDown);
};

Thanks!

@pierodibello

@xpeppers

Lessons learned from AWS Lambda - Trentino Cloud Computing Meetup 26.05.2016

By Pietro Di Bello

Lessons learned from AWS Lambda - Trentino Cloud Computing Meetup 26.05.2016

What we learn using AWS Lambda in a real project, to build an API backend, using Node.JS and the Serverless Framework.

  • 1,787