Slide 1

Slide 1 text

Yan Cui @theburningmonk

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

Yan Cui http://theburningmonk.com @theburningmonk Independent Consultant

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Billing alarms

Slide 7

Slide 7 text

Everyone should use billing alarms.

Slide 8

Slide 8 text

They are not perfect. But they can still save your a$$.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Keeping logging cost under control

Slide 13

Slide 13 text

CloudWatch often costs much more than your actual application.

Slide 14

Slide 14 text

As cost goes up, value goes down.

Slide 15

Slide 15 text

How to keep CloudWatch Logs cost under control 1. Do structured logging.

Slide 16

Slide 16 text

How to keep CloudWatch Logs cost under control 1. Do structured logging. DEBUG INFO WARN ERROR Detailed events for debugging application. General information that highlights progress of application. Potential problems, but doesn’t stop application from working. Issues that require immediate attention.

Slide 17

Slide 17 text

How to keep CloudWatch Logs cost under control 1. Do structured logging. Log at INFO or above in production. DEBUG INFO WARN ERROR Detailed events for debugging application. General information that highlights progress of application. Potential problems, but doesn’t stop application from working. Issues that require immediate attention.

Slide 18

Slide 18 text

How to keep CloudWatch Logs cost under control 1. Do structured logging. Log at INFO or above in production. 2. Sample DEBUG logs in production. e.g. 5% of invocations.

Slide 19

Slide 19 text

How to keep CloudWatch Logs cost under control 1. Do structured logging. Log at INFO or above in production. 2. Sample DEBUG logs in production. e.g. 5% of invocations. 3. Set log retention to 30 days.

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

CloudWatch Logs AWS Lambda AWS Lambda stdout asynchronously invokes any log aggregation service

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

CloudWatch Logs AWS Lambda AWS Lambda stdout asynchronously invokes any log aggregation service double paying for ingesting logs

Slide 24

Slide 24 text

Lambda Extensions + Telemetry API

Slide 25

Slide 25 text

https://docs.aws.amazon.com/lambda/latest/dg/telemetry-api.html

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

CloudWatch Logs AWS Lambda

Slide 28

Slide 28 text

https://lumigo.io/blog/lumigo-launches-log-management

Slide 29

Slide 29 text

Remember system messages

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

DEBUG INFO WARN platform.initStart platform.start platform.initRuntimeDone platform.initReport platform.runtimeDone platform.report unhandled exception

Slide 37

Slide 37 text

DEBUG INFO WARN platform.initStart platform.start platform.initRuntimeDone platform.initReport platform.runtimeDone platform.report unhandled exception

Slide 38

Slide 38 text

Tobias Schmidt Sandro Volpicella “de fi nitive guide for learning CloudWatch” - me cloudwatchbook.com

Slide 39

Slide 39 text

Right-sizing Lambda memory

Slide 40

Slide 40 text

More memory = more CPU = more network bandwidth

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Easy to be wrong by an order of magnitude.

Slide 43

Slide 43 text

STORY TIME

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Right-sizing Lambda functions

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

https://github.com/alexcasalboni/aws-lambda-power-tuning

Slide 49

Slide 49 text

Use ARM architecture

Slide 50

Slide 50 text

25% cheaper

Slide 51

Slide 51 text

Performance may vary…

Slide 52

Slide 52 text

Best for functions with a lot of IO wait time.

Slide 53

Slide 53 text

No lambda-to-lambda invocations

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

SYNCHRONOUS Lambda-to-Lambda are almost always a sign of bad design.

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Double paying for execution time

Slide 61

Slide 61 text

Service Boundary

Slide 62

Slide 62 text

Service Boundary

Slide 63

Slide 63 text

Service Boundary

Slide 64

Slide 64 text

AWS Lambda function !== lambda function in programming

Slide 65

Slide 65 text

Service Boundary Service Boundary

Slide 66

Slide 66 text

Service Boundary Service Boundary Downstream system shouldn’t rely on an implementation detail.

Slide 67

Slide 67 text

Service Boundary Service Boundary HTTP

Slide 68

Slide 68 text

Service Boundary Service Boundary HTTP

Slide 69

Slide 69 text

Service Boundary Service Boundary HTTP Stable interface Implementation detail

Slide 70

Slide 70 text

What about async invocations?

Slide 71

Slide 71 text

HTTP Async invocation Secondary responsibilities Do the thing the user wants

Slide 72

Slide 72 text

HTTP Async invocation Secondary responsibilities Do the thing the user wants Better user experience

Slide 73

Slide 73 text

HTTP Async invocation Secondary responsibilities Do the thing the user wants More robust error handling

Slide 74

Slide 74 text

HTTP Async invocation Secondary responsibilities Do the thing the user wants Service Boundary

Slide 75

Slide 75 text

Service Boundary Service Boundary Never a good idea!

Slide 76

Slide 76 text

Are async Lambda-to-Lambda invocations OK?

Slide 77

Slide 77 text

It depends…

Slide 78

Slide 78 text

Every component in your architecture should serve a purpose and provide a ROI.

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

?

Slide 81

Slide 81 text

? ?

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Caching

Slide 84

Slide 84 text

Caching is a cheat code for building performant & scalable applications.

Slide 85

Slide 85 text

Route53 CloudFront API Gateway Lambda DynamoDB

Slide 86

Slide 86 text

Route53 CloudFront API Gateway Lambda DynamoDB client-side caching

Slide 87

Slide 87 text

Route53 CloudFront API Gateway Lambda DynamoDB client-side caching edge caching

Slide 88

Slide 88 text

Route53 CloudFront API Gateway Lambda DynamoDB client-side caching edge caching application-level caching

Slide 89

Slide 89 text

Route53 CloudFront API Gateway Lambda DynamoDB client-side caching edge caching application-level caching ElastiCache

Slide 90

Slide 90 text

Route53 CloudFront API Gateway Lambda DynamoDB client-side caching edge caching application-level caching Momento

Slide 91

Slide 91 text

Route53 TTL

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

Use longer TTL for stable domains

Slide 94

Slide 94 text

Avoid CORS

Slide 95

Slide 95 text

Enabling CORS for API Gateway is easy

Slide 96

Slide 96 text

Enabling CORS for API Gateway is easy But you still pay for those CORS requests!

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

You might be double paying for every user request to your API…

Slide 99

Slide 99 text

Solution: roll your own OPTIONS methods

Slide 100

Slide 100 text

or…

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

Choose the right service

Slide 103

Slide 103 text

Every architectural decision is a buying decision.

Slide 104

Slide 104 text

Using the wrong service can be very costly.

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

Assuming 1KB per request

Slide 108

Slide 108 text

Assuming 1KB per request

Slide 109

Slide 109 text

Services that charge by uptime are order(s) of magnitude cheaper at scale.

Slide 110

Slide 110 text

Services that charge by uptime are order(s) of magnitude cheaper at scale. But, you must understand the cost dimensions of individual services.

Slide 111

Slide 111 text

Assuming 1MB per request ? ?

Slide 112

Slide 112 text

Assuming 1MB per request 😂 $53,837.87 $68.99

Slide 113

Slide 113 text

www.thefrugalarchitect.com

Slide 114

Slide 114 text

Law I. Make Cost a Non-functional Requirement. Law II. Systems that Last Align Cost to Business. Law III. Architecting is a Series of Trade-offs. Law IV. Unobserved Systems Lead to Unknown Costs. Law V. Cost Aware Architectures Implement Cost Controls. Law VI. Cost Optimization is Incremental. Law VII. Unchallenged Success Leads to Assumptions.

Slide 115

Slide 115 text

Law I. Make Cost a Non-functional Requirement. Law II. Systems that Last Align Cost to Business. Law III. Architecting is a Series of Trade-offs. Law IV. Unobserved Systems Lead to Unknown Costs. Law V. Cost Aware Architectures Implement Cost Controls. Law VI. Cost Optimization is Incremental. Law VII. Unchallenged Success Leads to Assumptions.

Slide 116

Slide 116 text

IOT core AppSync API Gateway Messages Connection Time $2 per million $0.08 per million mins $0.25 per million mins $1 per million $0.08 per million mins $1 per million

Slide 117

Slide 117 text

Users Connection Time

Slide 118

Slide 118 text

Users Connection Time Revenue

Slide 119

Slide 119 text

Users Connection Time Revenue Engagement

Slide 120

Slide 120 text

Users Connection Time Revenue Engagement

Slide 121

Slide 121 text

www.gomomento.com/services/topics

Slide 122

Slide 122 text

No Connection Time cost!

Slide 123

Slide 123 text

IOT core AppSync API Gateway Messages Connection Time $2 per million $0.08 per million mins $0.25 per million mins $1 per million $0.08 per million mins $1 per million Momento $1 per million - Data Transfer - EC2 rates EC2 rates EC2 rates

Slide 124

Slide 124 text

Speaking of picking cost-e ff i cient services…

Slide 125

Slide 125 text

No content

Slide 126

Slide 126 text

https://www.youtube.com/watch?v=SCIfWhAheVw

Slide 127

Slide 127 text

Lambda: $6 per 100 GB Hrs 🤯 ~7x markup!!!

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

DANGER DANGER DANGER DANGER DANGER

Slide 130

Slide 130 text

Simplify your architecture

Slide 131

Slide 131 text

Avoid unnecessary moving parts to your architecture.

Slide 132

Slide 132 text

? Synchronous

Slide 133

Slide 133 text

? ? Synchronous

Slide 134

Slide 134 text

? Asynchronous

Slide 135

Slide 135 text

? Asynchronous Not fan-out, just here to avoid Lambda-to-Lambda invocations

Slide 136

Slide 136 text

? ? Asynchronous

Slide 137

Slide 137 text

Every component in your architecture should serve a purpose and provide a ROI.

Slide 138

Slide 138 text

The most dangerous phrase in the language is "we've always done it this way". - Grace Hopper

Slide 139

Slide 139 text

Function URLs

Slide 140

Slide 140 text

Function URL

Slide 141

Slide 141 text

If you’re not using API Gateway features (e.g. Cognito authoriser, request models, direct integration)

Slide 142

Slide 142 text

Or, if you’re hitting API Gateway limits (e.g. 29s timeout, no response streaming)

Slide 143

Slide 143 text

Have to write Lambdaliths

Slide 144

Slide 144 text

Have to write Lambdaliths (No per-endpoint metrics & alerts, no fi ne-grained access control, no per-endpoint auth)

Slide 145

Slide 145 text

Have to write Lambdaliths (No per-endpoint metrics & alerts, no fi ne-grained access control, no per-endpoint auth) (Large frameworks a ff ect cold start performance)

Slide 146

Slide 146 text

Best for public or internal APIs

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

Functionless

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No Lambda = no cold starts

Slide 151

Slide 151 text

No Lambda = no Lambda costs

Slide 152

Slide 152 text

EventBridge Pipes

Slide 153

Slide 153 text

EventBridge Pipes

Slide 154

Slide 154 text

EventBridge Pipes Transform

Slide 155

Slide 155 text

Use Lambda functions to transform data, NOT transport data

Slide 156

Slide 156 text

OpenSearch

Slide 157

Slide 157 text

No-Code ETL OpenSearch

Slide 158

Slide 158 text

AppSync EventBridge OpenSearch

Slide 159

Slide 159 text

Step Functions EventBridge OpenSearch

Slide 160

Slide 160 text

Every component in your architecture should serve a purpose and provide a ROI.

Slide 161

Slide 161 text

No content

Slide 162

Slide 162 text

Direct client access to AWS

Slide 163

Slide 163 text

No content

Slide 164

Slide 164 text

No content

Slide 165

Slide 165 text

No content

Slide 166

Slide 166 text

No content

Slide 167

Slide 167 text

No content

Slide 168

Slide 168 text

Only allow access if hash key matches cognito sub

Slide 169

Slide 169 text

Not for the feint hearted…

Slide 170

Slide 170 text

When not to use this:

Slide 171

Slide 171 text

High risk, high reward!

Slide 172

Slide 172 text

https://theburningmonk.com/2023/12/direct-access-for-frontend-apps-to-aws-services

Slide 173

Slide 173 text

1. Billing alarms 2. Keeping logging cost under control 3. Right-sizing Lambda functions 4. No Lambda-to-Lambda calls 5. Caching 6. Route53 TTL 7. Avoid CORS 8. Choosing the right service 9. Simplify your architecture 10.Function URLs 11. Functionless 12. Direct client access to AWS

Slide 174

Slide 174 text

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

Slide 175

Slide 175 text

Questions?