Top conf serverlezz

100
AWS LAMBDA ANTONS KRANGA

Transcript of Top conf serverlezz

Page 1: Top conf   serverlezz

AWS LAMBDA ANTONS KRANGA

Page 2: Top conf   serverlezz

@acankr

@acankr

‣ Eng. Director @ Agile Stacks

‣ Cloud and DevOps automation startup

‣ Full stack developer ~ 15years

‣ Cloud Architect

‣ DevOps evangelist

‣ Speaker

‣ Marathon runner

ANTONS KRANGA

Page 3: Top conf   serverlezz

@acankr

I want a cloud native app

Page 4: Top conf   serverlezz

@acankr

I want it HA

Page 5: Top conf   serverlezz

@acankr

I want rapid scalability

Page 6: Top conf   serverlezz

@acankr

I want to pay around 5$/mo

Page 7: Top conf   serverlezz

@acankr

Sounds fantastic!

Page 8: Top conf   serverlezz

@acankr

How do I do that?

Page 9: Top conf   serverlezz

@acankr

Any drawbacks?

Page 10: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 11: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 12: Top conf   serverlezz

@acankr

1. VM CENTRIC APPS

2. CONTAINER CENTRIC

3. SERVERLESS

EVOLUTION OF CLOUD NATIVE APPS

Page 13: Top conf   serverlezz

@acankr

ECONOMICS OF VMS

Page 14: Top conf   serverlezz

@acankr

UTILIZATION COSTS

Page 15: Top conf   serverlezz

@acankr

ECONOMICS OF APPLICATION VIRTULIZATION ON AWS

▸ 64% or cloud costs refers to EC2 Instances

▸ 53% workloads Small Instances

▸ 29% workloads Medium size

https://goo.gl/1pmqKD

Page 16: Top conf   serverlezz

@acankr

ECONOMICS OF APPLICATION VIRTULIZATION ON AWS

▸ 64% or cloud costs refers to EC2 Instances

▸ 53% workloads Small Instances

▸ 29% workloads Medium size

▸ 16.7% Small instance utilization

▸ 11.9% Medium instance utilization

https://goo.gl/1pmqKD

Page 17: Top conf   serverlezz

@acankr

COSTS SAVING STRATEGIES

▸ Use only what you need

▸ Choose right size for instances

▸ Use Reserved instances

Page 18: Top conf   serverlezz

@acankr

CHALLENGES OF RESERVED INSTANCES

▸ Use only what you need

▸ Choose right size for instances

▸ Use Reserved instances

▸ Expect project run for short time

▸ Undecided about project size

▸ Fear of commitment

Page 19: Top conf   serverlezz

@acankr

CONTAINERS TO THE RESCUE

Page 20: Top conf   serverlezz

@acankr

Chorr Microservice

Page 21: Top conf   serverlezz

@acankr

Chorr Microservice

runtime

Page 22: Top conf   serverlezz

@acankr

Chorr Microservice

container

package

runtime

Page 23: Top conf   serverlezz

@acankr

Chorr Microservice

cloud

container

package

runtime

deploy

Page 24: Top conf   serverlezz

@acankr

Chorr Microservice

runtime

cloud

container

package

deploy

VM

Page 25: Top conf   serverlezz

@acankr

Chorr Microservice

runtime

cloud

container

VM

package

deploy

RAM

# CPU

$$$ per hour

Page 26: Top conf   serverlezz

@acankr

Chorr Microservice

runtime

cloud

container

VM

package

deploy

RAM

# CPU

$$$ per hour

Scales in 9 minutes*

Page 27: Top conf   serverlezz

@acankr

Containers comes with operational overhead

CHALLENGES OF CONTAINERS

Page 28: Top conf   serverlezz

@acankr

Scale up is easy, Scale down is a challenge

CHALLENGES OF CONTAINERS

Page 29: Top conf   serverlezz

@acankr

Persistent services are the challenge

CHALLENGES OF CONTAINERS

Page 30: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 31: Top conf   serverlezz

@acankr

SERVERLESS COMPUTING?

Page 32: Top conf   serverlezz

@acankr

WHAT IS SERVERLESS

Lambda

Page 33: Top conf   serverlezz

@acankr

WHAT IS SERVERLESS

LambdaTrigger

Page 34: Top conf   serverlezz

@acankr

WHAT IS SERVERLESS

Lambda

CodeCommit

SmartHome

AlexaSkill IoT

API Gateway

S3 Storage

CloudWatchEvent

Logs

SNS

Kinesis

Messages

DynamoDB

Internet of Things

Streaming

Development and Ops

Security

Trigger

Cognito

Page 35: Top conf   serverlezz

@acankr

WHAT IS SERVERLESS

Lambda

CodeCommit

SmartHome

AlexaSkill IoT

API Gateway

S3 Storage

CloudWatchEvent

Logs

SNS

Kinesis

Messages

DynamoDB

Internet of Things

Streaming

Development and Ops

Security

Trigger

Container

Application CodeCognito

Page 36: Top conf   serverlezz

@acankr

WHAT IS SERVERLESS

Lambda

CodeCommit

SmartHome

AlexaSkill IoT

API Gateway

S3 Storage

CloudWatchEvent

Logs

SNS

Kinesis

Messages

DynamoDB

Internet of Things

Streaming

Development and Ops

Security

Trigger Event AWS Service

Container

Application CodeCognito

Page 37: Top conf   serverlezz

@acankr

Scales in few milliseconds

ADVANTAGES OF AWS LAMBDA

Page 38: Top conf   serverlezz

@acankr

Pricing model: 0.2$ per 1M of executions

ADVANTAGES OF AWS LAMBDA

Page 39: Top conf   serverlezz

@acankr

Easy deployment

ADVANTAGES OF AWS LAMBDA

Page 40: Top conf   serverlezz

@acankr

Low maintenance costs

ADVANTAGES OF AWS LAMBDA

Page 41: Top conf   serverlezz

@acankr

Implements Actor model => good for async calls

ADVANTAGES OF AWS LAMBDA

Page 42: Top conf   serverlezz

@acankr

Have timeout limit for 5 minutes (cannot change)

DISADVANTAGES OF AWS LAMBDA

Page 43: Top conf   serverlezz

@acankr

Provides 2 cores vCPU (cannot change)

DISADVANTAGES OF AWS LAMBDA

Page 44: Top conf   serverlezz

@acankr

Stateless (you can only write to /tmp)

DISADVANTAGES OF AWS LAMBDA

Page 45: Top conf   serverlezz

@acankr

Problem of initial startup time (caches for few mins)

DISADVANTAGES OF AWS LAMBDA

Page 46: Top conf   serverlezz

@acankr

Doesn’t fit to sync calls with user interactions

DISADVANTAGES OF AWS LAMBDA

Page 47: Top conf   serverlezz

@acankr

LAMBDA CONTAINER

▸ Price: $0.208 - $2.501 per 1M executions

▸ RAM: 128MB - 1536MB

▸ vCPU Cores: 2

▸ Ephemeral Disk: 512MB

▸ Write Partition: /tmp/*

▸ Timeout: 300sec

▸ Body Payload: 6MB

]▸ Price: $0.023 per Hour (t2-small)

▸ RAM: 2GB

▸ vCPU Cores: 1

▸ Ephemeral Disk or EBS

▸ Timeout: no

Lambda EC2 (VM)

VS

Page 48: Top conf   serverlezz

@acankr

WHAT IS LAMBDA?

BUILD.GRADLEapply plugin: 'java'version = '1.0.0'mainClassName='Main'

repositories { mavenCentral()}

dependencies { compile ( 'com.amazonaws:aws-lambda-java-core:1.1.0', 'com.amazonaws:aws-lambda-java-events:1.1.0' )}

MAIN.JAVApublic class Main implements RequestHandler<String, String> {

public String handleRequest(String input, Context context) {context.getLogger().log("My input is: " + input);return "Hello: " + input

}

}

INDEX.PYimport logginglog = logging.getLogger()log.setLevel(logging.INFO)

def handler(event, context): log.debug(event) return {'message': 'Hello from Lambda'}

INDEX.JS

exports.handler = (event, context, callback) => { console.log(event) callback(null, {'message': 'Hello from Lambda'});};

Page 49: Top conf   serverlezz

@acankr

▸ NodeJS 4.6

▸ NodeJS 6.10

▸ Python 2.7

▸ Python 3.6

▸ Java 8

▸ C#

▸ EdgeJS 4.6

Language Runtimes First Execution Next Execution RAM Used

3.06ms 0.34ms 23MB

3.07-9.06ms 0.25 - 4.67ms 22MB

12.07 - 30.56ms 0.37 - 0.64ms 50MB

31.07ms 18MB0.29 - 9.96ms

0.17ms 0.18 - 0.38ms 20MB

MINIMALISTIC LAMBDA EXECUTION

Page 50: Top conf   serverlezz

@acankr

MINIMALISTIC LAMBDA EXECUTION

▸ NodeJS 4.6

▸ NodeJS 6.10

▸ Python 2.7

▸ Python 3.6

▸ Java 8

▸ C#

▸ EdgeJS 4.6

Language RuntimesCONFIGURATION MANAGEMENT

SECRET MANAGEMENT

SERVICE DISCOVERY

EXPOSURE AND AUTH

PRIVATE CLOUD ACCESS

Page 51: Top conf   serverlezz

@acankr

DEPLOYMENT

Page 52: Top conf   serverlezz

@acankr

LAMBDA

SERVICE

Page 53: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

SERVICE

Page 54: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

SERVICE ALIAS

LATEST

Page 55: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

V2

SERVICE ALIAS

LATEST

Page 56: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

V2

V3

SERVICE ALIAS

LATEST

Page 57: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

V2

V3

SERVICE ALIAS

LATEST

V4

Page 58: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

V2

V3

SERVICE ALIAS

STABLE

V4

LATEST

Page 59: Top conf   serverlezz

@acankr

CODE VERSIONS

LAMBDA

V1

V2

V3

SERVICE ALIAS

STABLE

V4

ENV

DEV

TEST

PROD

LATEST

Page 60: Top conf   serverlezz

@acankr

▸ CloudFormation and/or Terraform for initial deployment

▸ Setup Cloud Resources

▸ Inject dependencies via ENV VARS

▸ Encrypt Secrets with KMS

▸ CLI “update-function-code” for incremental deployment

Page 61: Top conf   serverlezz

@acankr

EXPOSE LAMBDA

Page 62: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 63: Top conf   serverlezz

@acankr

API Gateway

Lambda

+

- API Management Tool

- Authorization + Custom Authorizer

- Defines: Environment Variables for Lambda

- Can be defined with Swagger and imported

- Code Supports Versioning

- Integrated with CloudWatch

- Lambda Containers are Cached for 5 minutes

- Can be deployed with “apex.run” tool

- User can write files in /tmp

Page 64: Top conf   serverlezz

@acankr

GETPOSTPUTDELETE

dataAPI Gateway Lambda

ajax event

USER

Page 65: Top conf   serverlezz

@acankr

GETPOSTPUTDELETE

dataAPI Gateway Lambda

ajax event

USER

AuthorizerLambda

IdentityService Provider

Page 66: Top conf   serverlezz

@acankr

STATEFUL LAMBDA

Page 67: Top conf   serverlezz

@acankr

GETPOSTPUTDELETE

dataAPI Gateway

ajax event

USER

DynamoDB Table

Environment Variables

Lambda DynamoDB

Page 68: Top conf   serverlezz

@acankr

GETPOSTPUTDELETE

dataAPI Gateway

ajax event

USER

DB_URL

DB_PORT

DB_USER

Environment Variables

Lambda

VPC

KMS encrypted DB_PASSWORD

Page 69: Top conf   serverlezz

@acankr

STEP FUNCTIONS

Page 70: Top conf   serverlezz

@acankr

▸ Model flows of Lambda Functions

▸ Conditional flows

▸ Design error handling

▸ Design conditional execution

▸ Output of previous function will be input of next

Page 71: Top conf   serverlezz

@acankr

WEBSITE EXAMPLE

Page 72: Top conf   serverlezz

@acankr

GET

Static HTML

CSS/Media

Rich JavaScript Apps

S3 StorageCloudFront

GETPOSTPUTDELETE

Dynamic DataData from DatabaseData from External Service

dataAPI Gateway Lambda

ajax

http

eventUSER

DynamoDB

R53 Domain

example.com

Page 73: Top conf   serverlezz

@acankr

HIPSTER PORTAL

Page 74: Top conf   serverlezz

@acankr

HIPSTER PORTAL

"...USE GIT AS THE BASIS FOR A LIGHTWEIGHT CMS, WITH TEXT-BASED EDITING FORMATS. GIT

HAS POWERFUL FEATURES FOR TRACKING CHANGES AND EXPLORING ALTERNATIVES, WITH A

DISTRIBUTED STORAGE MODEL THAT IS FAST IN USE AND TOLERANT OF NETWORKING ISSUES."

ThoughtWorks Technology Radar https://www.thoughtworks.com/radar/techniques/git-based-cms-git-for-non-code

assess since May 2015

Page 75: Top conf   serverlezz

@acankr

Lambda

+ - Lambda doesn’t have GIT client.

- You can “statically link” git libraries with git2go library (libgit2)

- To read SSH key file with Lambda it must be stored in “/tmp” directory

- SSH private key must have 600 credentials

- SSH private key must be owned by user “sandbox”

Code Commit

- Git Repository Service

- Backed by S3 storage

- Price: $1 per user

- Only: us-west-1 region

Page 76: Top conf   serverlezz

@acankr@acankr

USER

GETS3 Storage

CloudFront

GETPOSTPUTDELETE

dataAPI Gateway Lambda

ajax

http

event

Lambda

push

EDITOR

event

document commit

PUT

Checkout documentRender or post-process Publish

CodeCommit

DynamoDB

STREAM

Invalidate Cache

Lambda

Page 77: Top conf   serverlezz

@acankr

SERVERLESS CI

Page 78: Top conf   serverlezz

@acankr

CompilationLambda

push

DEV

event

document commit

Checkout Compile

CodeCommit

PUT

S3

GET

Checkout Compile

TestingLambda

if neededlong running tests

VMs

CREATE

DeploymentLambda

Lambda

SNS

ChatOps

Page 79: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 80: Top conf   serverlezz

@acankr

We tried number of frameworks for different runtimes

LESSONS LEARNED

Page 81: Top conf   serverlezz

@acankr

Most mature: serverless and apex

LESSONS LEARNED

Page 82: Top conf   serverlezz

@acankr

Quite mature: serverless (node) and apex

LESSONS LEARNED

Page 83: Top conf   serverlezz

@acankr

Interesting: chalice (python)

LESSONS LEARNED

Page 84: Top conf   serverlezz

@acankr

All frameworks failed to aws-sdk

LESSONS LEARNED

Page 85: Top conf   serverlezz

@acankr

All cloud deployment frameworks failed except aws-cli and terraform

LESSONS LEARNED

Page 86: Top conf   serverlezz

@acankr

Different event sources sends you different messages

LESSONS LEARNED

Page 87: Top conf   serverlezz

@acankr

json-schema is a way to validate who is calling you

LESSONS LEARNED

Page 88: Top conf   serverlezz

@acankr

AWS loves to encode json as string inside of another json

LESSONS LEARNED

Page 89: Top conf   serverlezz

@acankr

Optimize for latency (not for CPU)

LESSONS LEARNED

Page 90: Top conf   serverlezz

@acankr

Lambda is amazing at handling Cloud events

LESSONS LEARNED

Page 91: Top conf   serverlezz

@acankr

Minimize API calls (introduces latency and cost)

LESSONS LEARNED

Page 92: Top conf   serverlezz

@acankr

Avoid call lambda from another lambda (use local exec)

LESSONS LEARNED

Page 93: Top conf   serverlezz

@acankr

Unit tests are hard and no remote debug

LESSONS LEARNED

Page 94: Top conf   serverlezz

@acankr

Use LAMBDA_PROXY integration wiht APIG

LESSONS LEARNED

Page 95: Top conf   serverlezz

@acankr

AGENDA

▸ Evolution of Cloud Native Applications

▸ Function as a Service

▸ Serverless Blueprints

▸ Lessons Learned

▸ Takeaways

Page 96: Top conf   serverlezz

@acankr

▸ Optimize for what you use

▸ Split deployment code to: initial and incremental

▸ Lambdas are best for rare events (cluster events, chatbots etc)

▸ Lambdas bad for UI

TAKEWAYS

Page 97: Top conf   serverlezz

@acankr

▸ All frameworks were

▸ Use CloudFormation and Terraform for initial deployment

▸ Serverless to support Lambda on NodeJS

▸ Chalice for Python runtime

TAKEAWAYS

Page 98: Top conf   serverlezz

@acankr

- AZURE FUNCTIONS

- Runtimes: - ASP .NET (1Core)- NodeJS- etc

- Deployment:- REST API- PowerShell

- GOOGLE CLOUD FUNCTIONS

- Runtimes:- NodeJS (only)

- Deployment:- gcloud

Page 99: Top conf   serverlezz

@acankr

GOOD BOOK

Book: AWS Lambda in Action MEAP

Begin in 2016 February

Publication: March 2017

Author: Danilo Poccia

ISBN: 9781617293719

https://www.manning.com/books/aws-lambda-in-action

Page 100: Top conf   serverlezz

@acankr

THANK YOU

https://github.com/akranga/topconf-serverless