Slide 1

Slide 1 text

What's the scariest thing about serverless?

Slide 2

Slide 2 text

@slobodan_ Long-running tasks?

Slide 3

Slide 3 text

@slobodan_ Compliance?

Slide 4

Slide 4 text

@slobodan_ Using binaries and large dependencies?

Slide 5

Slide 5 text

@slobodan_ Cold start?

Slide 6

Slide 6 text

@slobodan_ Cold start with VPN?

Slide 7

Slide 7 text

@slobodan_ Node.js?

Slide 8

Slide 8 text

@slobodan_ But, what about…

Slide 9

Slide 9 text

@slobodan_ BIG

Slide 10

Slide 10 text

@slobodan_ BAD

Slide 11

Slide 11 text

@slobodan_ VENDOR LOCK-IN

Slide 12

Slide 12 text

@slobodan_

Slide 13

Slide 13 text

@slobodan_ What is vendor lock-in?

Slide 14

Slide 14 text

@slobodan_ "In economics, vendor lock-in, makes a customer dependent on a vendor for products and services, unable to use another vendor without substantial switching costs."

Slide 15

Slide 15 text

@slobodan_

Slide 16

Slide 16 text

@slobodan_

Slide 17

Slide 17 text

@slobodan_

Slide 18

Slide 18 text

@slobodan_ A guy with a lot of servers.
 Let's call him Jeff.

Slide 19

Slide 19 text

@slobodan_ A guy with a lot of servers.
 Let's call him Jeff.

Slide 20

Slide 20 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 21

Slide 21 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 22

Slide 22 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 23

Slide 23 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 24

Slide 24 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 25

Slide 25 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 26

Slide 26 text

@slobodan_ Jeff is smart, and he knows how do you use his servers.

Slide 27

Slide 27 text

@slobodan_ But what if Jeff is actually a villain?

Slide 28

Slide 28 text

@slobodan_ But what if Jeff is actually a villain?

Slide 29

Slide 29 text

@slobodan_ But what if Jeff is actually a villain?

Slide 30

Slide 30 text

@slobodan_ Your wallet would not be happy…

Slide 31

Slide 31 text

@slobodan_

Slide 32

Slide 32 text

@slobodan_ Another guy with a lot of servers.
 Let's call him Bill.

Slide 33

Slide 33 text

@slobodan_ Another guy with a lot of servers.
 Let's call him Bill.

Slide 34

Slide 34 text

@slobodan_ Another guy with a lot of servers.
 Let's call him Bill.

Slide 35

Slide 35 text

@slobodan_ Another guy with a lot of servers.
 Let's call him Bill.

Slide 36

Slide 36 text

@slobodan_ Another guy with a lot of servers.
 Let's call him Bill.

Slide 37

Slide 37 text

@slobodan_ That's vendor lock-in in the cloud

Slide 38

Slide 38 text

@slobodan_ "My train of thought went like this: the term “lock-in” is misleading. We are really talking about switching costs." Mark Schwartz
 Enterprise Strategist at AWS

Slide 39

Slide 39 text

@slobodan_ "As soon as you commit yourself to a platform or a vendor you will have switching costs if you later decide to change." Mark Schwartz
 Enterprise Strategist at AWS

Slide 40

Slide 40 text

@slobodan_ How to fight vendor lock-in?

Slide 41

Slide 41 text

@slobodan_ Or how to keep your switching costs reasonable?

Slide 42

Slide 42 text

@slobodan_ • Planning and analysis • Good architecture • Deployment procedures How likely will I need to switch? What would be the cost?

Slide 43

Slide 43 text

@slobodan_ That leads us to our topic…

Slide 44

Slide 44 text

Designing testable serverless apps using hexagonal architecture

Slide 45

Slide 45 text

@slobodan_ But, before we continue…

Slide 46

Slide 46 text

Slobodan Stojanovic CTO @ Cloud Horizon & CTO @ Vacation Tracker co-author of Serverless Applications with Node.js book AWS Serverless Hero @slobodan_ serverless.pub

Slide 47

Slide 47 text

@slobodan_ Designing testable serverless apps using hexagonal architecture

Slide 48

Slide 48 text

@slobodan_ Why is testing important for serverless apps?

Slide 49

Slide 49 text

@slobodan_ Most of the time serverless apps are not fully isolated monoliths without integrations

Slide 50

Slide 50 text

@slobodan_ Instead, they contain many services interacting with each other and with external dependencies

Slide 51

Slide 51 text

@slobodan_

Slide 52

Slide 52 text

@slobodan_ Integrations can change every moment!

Slide 53

Slide 53 text

@slobodan_ Tests don't prevent changes. They make sure your changes are not accidental.

Slide 54

Slide 54 text

@slobodan_ But how do you know what should you test in a serverless app?

Slide 55

Slide 55 text

@slobodan_ Testing pyramid

Slide 56

Slide 56 text

@slobodan_

Slide 57

Slide 57 text

@slobodan_ Testing pyramid vs "Serverless testing pyramid"

Slide 58

Slide 58 text

@slobodan_

Slide 59

Slide 59 text

@slobodan_ Integration tests are cheaper, but also more important, because the common serverless app is split into many small pieces

Slide 60

Slide 60 text

@slobodan_ Designing testable serverless apps using hexagonal architecture

Slide 61

Slide 61 text

@slobodan_ Ok, so which architecture is the best for serverless apps?

Slide 62

Slide 62 text

@slobodan_ Any architecture that will let you test your serverless app easily and keep switching costs low.

Slide 63

Slide 63 text

@slobodan_ Because sooner or later you'll need to switch/migrate pieces of your app.

Slide 64

Slide 64 text

@slobodan_ Not to another cloud vendor, but to your new service, new or changed integration…

Slide 65

Slide 65 text

@slobodan_ Risks to consider when building a serverless app

Slide 66

Slide 66 text

@slobodan_ • configuration risks • technical workflow risks • business logic risks • integration risks

Slide 67

Slide 67 text

@slobodan_ One of the app architectures that fits these needs is
 hexagonal architecture

Slide 68

Slide 68 text

@slobodan_ Designing testable serverless apps using hexagonal architecture

Slide 69

Slide 69 text

@slobodan_ "Allow an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases." Alistair Cockburn
 Creator of Hexagonal architecture

Slide 70

Slide 70 text

@slobodan_

Slide 71

Slide 71 text

@slobodan_

Slide 72

Slide 72 text

@slobodan_ An example

Slide 73

Slide 73 text

@slobodan_

Slide 74

Slide 74 text

@slobodan_

Slide 75

Slide 75 text

@slobodan_

Slide 76

Slide 76 text

@slobodan_ Code, please

Slide 77

Slide 77 text

@slobodan_ const {
 httpResponse, parseApiEvent,
 SnsRepository
 } = require('../common')
 const main = require('./main')
 async function handler(event) { const { body, headers } = parseApiEvent(event) // Create instance of SNS notification repository
 const notification = new SnsRepository(
 process.env.topic ) // Invoke main function with all dependencies
 await main(body, headers, notification)
 return httpResponse()
 }

Slide 78

Slide 78 text

@slobodan_ await main(body, headers, notification)

Slide 79

Slide 79 text

@slobodan_ Unit tests

Slide 80

Slide 80 text

@slobodan_ await main(body, headers, mockNotification) Mock notification repository instance Some static values

Slide 81

Slide 81 text

@slobodan_ Integration tests

Slide 82

Slide 82 text

@slobodan_ await main(body, headers, localNotification) Local notification adapter, using JS events for example Some static values

Slide 83

Slide 83 text

@slobodan_ Simple and nice

Slide 84

Slide 84 text

@slobodan_ But, do you remember…

Slide 85

Slide 85 text

@slobodan_ BIG

Slide 86

Slide 86 text

@slobodan_ BAD

Slide 87

Slide 87 text

@slobodan_ VENDOR LOCK-IN

Slide 88

Slide 88 text

@slobodan_

Slide 89

Slide 89 text

@slobodan_ How does hexagonal architecture help you fighting vendor lock-in?

Slide 90

Slide 90 text

@slobodan_ Story time

Slide 91

Slide 91 text

@slobodan_ Vacation Tracker vacationtracker.io

Slide 92

Slide 92 text

@slobodan_ vacationtracker.io

Slide 93

Slide 93 text

@slobodan_ • Serverless prototype • Small team (1 fulltime developer) • Initial product was Serverless chatbot + Express.js and MongoDB • Growing fast (200+ teams using it)

Slide 94

Slide 94 text

@slobodan_ • Express -> Serverless migration • MongoDB -> DynamoDB migration

Slide 95

Slide 95 text

@slobodan_ Let's talk about MongoDB -> DynamoDB switch

Slide 96

Slide 96 text

@slobodan_

Slide 97

Slide 97 text

@slobodan_

Slide 98

Slide 98 text

@slobodan_

Slide 99

Slide 99 text

@slobodan_ How does this look like?

Slide 100

Slide 100 text

@slobodan_ describe('DynamoDB repository', () => { describe('unit', () => { ... }) describe('integration', () => { beforeAll(() => { // Create test DB }) afterAll(() => { // Destroy test DB }) // Tests }) })

Slide 101

Slide 101 text

@slobodan_ beforeAll(async () => { const params = { ... } await dynamoDb.createTable(params).promise() await dynamoDb.waitFor('tableExists', { TableName: tableName }).promise() })

Slide 102

Slide 102 text

@slobodan_ afterAll(async () => { await dynamoDb.deleteTable({ TableName: tableName }).promise() await dynamoDb.waitFor('tableNotExists', { TableName: tableName }).promise() })

Slide 103

Slide 103 text

@slobodan_ And they lived happily ever after…

Slide 104

Slide 104 text

@slobodan_ Beyond testing

Slide 105

Slide 105 text

@slobodan_ What should we do with things that can't be tested?

Slide 106

Slide 106 text

@slobodan_ What should we do with things that can't be tested? For example, Google or someone else deprecated an API while your app is in production

Slide 107

Slide 107 text

@slobodan_ Make sure you are monitoring your app and tracking errors

Slide 108

Slide 108 text

@slobodan_ Monitoring/error-tracking tools

Slide 109

Slide 109 text

@slobodan_ • Built-in tools (CloudWatch, X-Ray) • Epsagon • IOpipe • Thundra • Lumigo • and many others

Slide 110

Slide 110 text

@slobodan_ Serverless apps often heavily relies on front end, in those cases you need to track front end errors as well

Slide 111

Slide 111 text

@slobodan_ desole.io

Slide 112

Slide 112 text

@slobodan_

Slide 113

Slide 113 text

@slobodan_

Slide 114

Slide 114 text

@slobodan_ Summary

Slide 115

Slide 115 text

@slobodan_ • Good architecture helps you to maintain your switching costs low (or at least reasonable) • Hexagonal architecture is a nice fit for serverless apps • Test your integrations (and app in general) • Testing is not enough, you'll need monitoring and error tracking for your serverless apps

Slide 116

Slide 116 text

@slobodan_ Links to the things I mentioned

Slide 117

Slide 117 text

@slobodan_ • Mark Schwartz - Switching Costs and Lock-In: amzn.to/2S4iT3G • Hexagonal Architecture: bit.ly/hex-arch • Vacation Tracker: vacationtracker.io • Hexagonal Architecture at Vacation Tracker: bit.ly/vt-hex-arch • Desole (error-tracking tool): desole.io

Slide 118

Slide 118 text

@slobodan_ One more thing!

Slide 119

Slide 119 text

@slobodan_

Slide 120

Slide 120 text

@slobodan_ Serverless Applications with Node.js Use claudia40 promo code for 40% off serverless.pub/book @slobodan_

Slide 121

Slide 121 text

@slobodan_ Thank you!

Slide 122

Slide 122 text

@slobodan_ Thank you! Danke