Slide 1

Slide 1 text

Patterns for Efficient Serverless Development

Slide 2

Slide 2 text

Yan Cui http://theburningmonk.com @theburningmonk AWS user since 2010

Slide 3

Slide 3 text

Developer Advocate @ Yan Cui http://theburningmonk.com @theburningmonk

Slide 4

Slide 4 text

Independent Consultant advise training delivery Yan Cui http://theburningmonk.com @theburningmonk

Slide 5

Slide 5 text

FAST Feedback Loop

Slide 6

Slide 6 text

Myth: there is no local development for serverless

Slide 7

Slide 7 text

Option 1: Hexagonal Architecture

Slide 8

Slide 8 text

An architectural pattern for creating loosely-coupled application components that can easily connect to their environment via ports and adapters.

Slide 9

Slide 9 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 10

Slide 10 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 11

Slide 11 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 12

Slide 12 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 13

Slide 13 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 14

Slide 14 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 15

Slide 15 text

Port Port Port Port Adapter Adapter Lambda Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 16

Slide 16 text

Port Port Port Port Adapter Adapter Lambda Adapter Client B Adapter External system Adapter Application Core Domain External system External system (event, context) CreateUserCommand

Slide 17

Slide 17 text

Port Port Port Port Adapter Adapter Lambda Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 18

Slide 18 text

Port Port Port Port Adapter Adapter Lambda Adapter Fargate Adapter External system Adapter Application Core Domain External system External system

Slide 19

Slide 19 text

Creates portability that is useful when you’re not sure if a workload should run in Lambda or container

Slide 20

Slide 20 text

Port Port Port Port Adapter Adapter Lambda Adapter Fargate Adapter External system Adapter Application Core Domain External system External system

Slide 21

Slide 21 text

Port Port Port Port Adapter Adapter Lambda Adapter Fargate Adapter External system Adapter Application Core Domain External system DynamoDB

Slide 22

Slide 22 text

Port Port Port Port Adapter Adapter Lambda Adapter Fargate Adapter External system Adapter Application Core Domain External system Mock

Slide 23

Slide 23 text

Port Port Port Port Adapter Adapter Lambda Adapter Fargate Adapter External system Adapter Application Core Domain External system RDS

Slide 24

Slide 24 text

Port Port Port Port Adapter Adapter Client A Adapter Client B Adapter External system Adapter Application Core Domain External system External system

Slide 25

Slide 25 text

Option 1: Hexagonal Architecture Universally applicable (language, framework, etc.) ??? Portability

Slide 26

Slide 26 text

Serverless is more than Lambda

Slide 27

Slide 27 text

Serverless means: 1. No need to manage servers. 2. Scale to zero. 3. Usage-based pricing with no minimum.

Slide 28

Slide 28 text

“Use Lambda to transform data, NOT transport data”

Slide 29

Slide 29 text

API Gateway

Slide 30

Slide 30 text

API Gateway Cognito

Slide 31

Slide 31 text

API Gateway DynamoDB Cognito

Slide 32

Slide 32 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus

Slide 33

Slide 33 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus 3rd party API

Slide 34

Slide 34 text

Option 1: Hexagonal Architecture Universally applicable (language, framework, etc.) Can’t test direct integrations Portability

Slide 35

Slide 35 text

Option 1: Hexagonal Architecture Universally applicable (language, framework, etc.) Can’t test direct integrations Portability Upfront work to create abstraction layers

Slide 36

Slide 36 text

Option 2: Local simulation

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus

Slide 39

Slide 39 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus

Slide 40

Slide 40 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus IAM enforcement

Slide 41

Slide 41 text

API Gateway DynamoDB Cognito EventBridge Pipes EventBridge Bus Lambda

Slide 42

Slide 42 text

Option 2: Local simulation Not only Lambda. ???

Slide 43

Slide 43 text

Option 2: Local simulation Not only Lambda. Never 100% accurate - missing APIs, bugs, behaviour di ff erences, etc.

Slide 44

Slide 44 text

Option 3: Lambdalith

Slide 45

Slide 45 text

Lambdalith: run web app (e.g. express.js) inside Lambda

Slide 46

Slide 46 text

https://github.com/awslabs/aws-lambda-web-adapter

Slide 47

Slide 47 text

https://github.com/CodeGenieApp/serverless-express

Slide 48

Slide 48 text

https://bref.sh

Slide 49

Slide 49 text

https://github.com/zappa/Zappa

Slide 50

Slide 50 text

Option 3: Lambdalith Familiar to developers ??? Portability

Slide 51

Slide 51 text

Option 3: Lambdalith Familiar to developers Can’t test direct integrations Portability

Slide 52

Slide 52 text

Option 3: Lambdalith Familiar to developers Can’t test direct integrations Portability Cold start penalty with large web framework dependency

Slide 53

Slide 53 text

Option 3: Lambdalith Familiar to developers Can’t test direct integrations Portability Cold start penalty with large web framework dependency Might not work as well for non-API workloads

Slide 54

Slide 54 text

Option 3: Lambdalith Familiar to developers Can’t test direct integrations Portability Cold start penalty with large web framework dependency Might not work as well for non-API workloads Less granular alerting and access controls

Slide 55

Slide 55 text

Option 4: Deployment frameworks

Slide 56

Slide 56 text

sls invoke local sam local invoke sst dev

Slide 57

Slide 57 text

Option 4: Deployment frameworks Framework does the heavy lifting for you ???

Slide 58

Slide 58 text

Option 4: Deployment frameworks Framework does the heavy lifting for you Framework (and sometimes language) speci fi c

Slide 59

Slide 59 text

Option 4: Deployment frameworks Framework does the heavy lifting for you Framework (and sometimes language) speci fi c Can’t test direct integrations

Slide 60

Slide 60 text

Option 4: Deployment frameworks Framework does the heavy lifting for you Framework (and sometimes language) speci fi c Can’t test direct integrations Exploratory testing is NOT the same as automated tests

Slide 61

Slide 61 text

Option 5: “remocal” testing

Slide 62

Slide 62 text

Remocal testing: execute code locally against remote resources

Slide 63

Slide 63 text

Local testing Runs code locally Can use debugger Change code without deployment FAST FEEDBACK!

Slide 64

Slide 64 text

Test against mocks Test against real thing “does it work?” “does it do what I expect?”

Slide 65

Slide 65 text

Expectations/ Assumptions are sometimes wrong… Reality! Test against mocks Test against real thing “does it work?” “does it do what I expect?”

Slide 66

Slide 66 text

Local testing Runs code locally Can use debugger Change code without deployment Limited coverage Prone to false positives FAST FEEDBACK! LOW CONFIDENCE

Slide 67

Slide 67 text

Your application consists of more than just your code

Slide 68

Slide 68 text

Your job is to ensure all of it works Your application consists of more than just your code

Slide 69

Slide 69 text

Local testing Runs code locally Can use debugger Change code without deployment Limited coverage Prone to false positives FAST FEEDBACK! LOW CONFIDENCE HIGH CONFIDENCE! Remote testing Test in the cloud Realistic tests Better coverage

Slide 70

Slide 70 text

Local testing Runs code locally Can use debugger Change code without deployment Limited coverage Prone to false positives FAST FEEDBACK! LOW CONFIDENCE Slow deployments Every change needs deploying… SLOW FEEDBACK… HIGH CONFIDENCE! Remote testing Test in the cloud Realistic tests Better coverage

Slide 71

Slide 71 text

Local testing Remote testing

Slide 72

Slide 72 text

Runs code locally, talk to real AWS services REMOCAL testing Can use debugger Change code without deployment Realistic tests

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

Cognito API Gateway AppSync EventBridge

Slide 75

Slide 75 text

Cognito API Gateway AppSync EventBridge IAM, event pattern, etc. IAM, URL path, method, etc. IAM, resolver con fi g, resolver template, etc.

Slide 76

Slide 76 text

Option 5: Remocal testing Universally applicable (language, framework, etc.) ???

Slide 77

Slide 77 text

Option 5: Remocal testing Universally applicable (language, framework, etc.) Can’t test direct integrations

Slide 78

Slide 78 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM

Slide 79

Slide 79 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM Unit Test Remocal Test E2E Test

Slide 80

Slide 80 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM Unit Test Remocal Test E2E Test E2E Test E2E Test E2E Test E2E Test

Slide 81

Slide 81 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM Unit Test Remocal Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test

Slide 82

Slide 82 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM Unit Test Remocal Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test Unit Test

Slide 83

Slide 83 text

Auth Cognito AWS_IAM API Key Lambda authorizer Request validation Request transform Integration Lambda Direct service integration Domain logic Integrations Response transform IAM Unit Test Remocal Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test E2E Test Unit Test Remocal Test

Slide 84

Slide 84 text

DEMO TIME

Slide 85

Slide 85 text

REMOCAL testing AWS resources need to be provisioned Runs code locally, talk to real AWS services Can use debugger Change code without deployment Realistic tests

Slide 86

Slide 86 text

Ephemeral environments

Slide 87

Slide 87 text

Myth: there is no local development for serverless

Slide 88

Slide 88 text

Option 1: Hexagonal Architecture Option 2: Local simulation Option 3: Lambdalith Option 4: Deployment frameworks Option 5: Remocal testing

Slide 89

Slide 89 text

productionreadyserverless.com Join 20+ AWS Heroes & Community Builders and 1000+ happy students in levelling up your serverless game.

Slide 90

Slide 90 text

“You could spend tens of thousands on costly mistakes, and years of dev time to learn these lessons, or you could spend a few days downloading Yan's extensive experience. The choice is clear!”

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Questions?