serverless applications with aws sam —create …...serverless applications with aws sam —create...

44
Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and monitor serverless applications —manage deployments using AWS CloudFormation and AWS SAM —design applications to get the most out of this new type of architecture Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Upload: others

Post on 21-May-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Serverless Applications with AWS SAM

—create auto-scaling web APIs—handle background processes—secure APIs—inspect and monitor serverless applications—manage deployments using AWS CloudFormation

and AWS SAM—design applications to get the most out of this

new type of architectureCopyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 2: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Code/slides will be @ serverless.pub in a few days

Two-day coding workshop at Crisp 28-29 March (www.crisp.se/kurser)

[email protected] @gojkoadzic

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 3: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Why serverless?

—time to market—significant reduction for operational costs—good when throughput is more critical than

latency

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 4: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Why SAM?

—Rapidly maturing—Provided by Amazon directly—Integrated nicely with other Amazon dev tools—Easy to extend (just CloudFormation under the

hood)

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 5: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Billing actual usage,not reserved capacity

—$0.0000002 per request—$0.000000834 for 100ms @ 512MB—First 1 million requests per month are free

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 6: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Code with batteries included

—Scaling—Monitoring—Recovery—Versioning—Logging

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 7: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM Basics: initialise a new app

sam init --runtime java8 sam package ...sam deploy ...

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 8: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 9: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

"Time to recover"no longer important

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 10: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Multi-versioningis amazing

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 11: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

It's not stateless, butShare-nothing

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 12: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

CloudFormation basics: infrastructure as code

—YAML/JSON template + links to project code —package uploads project code to S3 and updates

deployment config—deploy using transformed config, or upload, or

give to CI...

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 13: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

AWS SAM: means two things

—Transform: AWS::Serverless-2016-10-31 —sam command line tool

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 14: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Transform: AWS::Serverless-2016-10-31

—adds new resources to CloudFormation—implicitly creates IAM roles and event wiring—reduces boilerplate code significantly

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 15: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM command line tool

—test locally using docker—convenient templates for apps and events—aliases/wrappers for common CloudFormation

commands

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 16: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

CF basics: create a deployable template

aws cloudformation package --template-file <input template> --output-template-file <deployable template> --s3-bucket <asset bucket>

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 17: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extra: bundle source and dependencies cleanly

sam build—for nodejs, python, go... (not yet Java)

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 18: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extras: pack either main or built template

sam package --output-template-file <deployable template> --s3-bucket <asset bucket> # not necessary --template-file <input>

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 19: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 20: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extras: gradual deployment

DeploymentPreference: Type: Canary10Percent10Minutes Alarms: - !Ref CheckForDropInSales - !Ref CheckForDropInConversion Hooks: PreTraffic: !Ref ClearStatisticsLambda PostTraffic: !Ref NotifyAdminsLambda Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 21: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

gradual deployment options

—Canary10Percent30Minutes—Canary10Percent5Minutes—Canary10Percent10Minutes—Canary10Percent15Minutes—Linear10PercentEvery10Minutes—Linear10PercentEvery1Minute—Linear10PercentEvery2Minutes—Linear10PercentEvery3MinutesCopyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 22: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

CF basics: get stack resources

aws cloudformation describe-stack-resources --stack-name <stack name>

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 23: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

CF basics: get stack outputs

aws cloudformation describe-stacks --stack-name <stack name> --query 'Stacks[].Outputs[]' --output table

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 24: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extras: run with API locally

sam local start-api

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 25: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extras: read logs

sam logs -n <LAMBDA_FUNCTION_NAME>

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 26: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 27: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Talking to other AWS services

—set up IAM access policies—use AWS SDK APIs with implicit authentication

from Lambda—use environment vars to pass references to

resources—use context.awsRequestId for unique-per-

request values—consider timeoutsCopyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 28: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM extras: generate sample events

sam local generate-event apigateway aws-proxy

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 29: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Give the platformtraditional server roles

—Gatekeeper ➤ Distributed Auth—Scaling point ➤ Containers—Orchestration ➤ Client or workflow engines

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 30: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Serverless authentication

—IAM: individual named (internal) services and users

—SIG V4: temporary request grants, using your credentials

—Cognito: anonymous and named (external) users, with own IAM policies

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 31: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 32: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Triggering lambdas from other sources

Events: FileUpload: Type: S3 Properties: Bucket: !Ref UploadBucket Events: s3:ObjectCreated:*

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 33: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 34: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Two types of calls

—Synchronous: errors reported back—Asynchronous: retry 3 times

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 35: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Dead-letter queues

—fallback when Lambda gives up retryingDeadLetterQueue: Type: SNS TargetArn: !Ref NotifyAdmins

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 36: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Service integration patterns

—SNS: transient, all consumers get everything, Lambdas auto-scaled

—Kinesis: persistent, sequential, guaranteed max one Lambda per shard

—SQS: persistent, compete with other consumers, Lambdas auto-scaled

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 37: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Lambda limits

—Max 15 minutes—No way to keep open connections—No sticky sessions

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 38: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Delegate for better latency/length

—Fargate (run autoscale containers but pay per usage)

—Step functions (run programmable workflows for up to 1 year)

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 39: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

How to protect against abuse?

—set usage alerts with Cloudwatch—set API usage plans (with keys)—set Lambda concurrency limits (per function/per

account)

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 40: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM Benefits

—Atomic deployments for multiple resources—Version control for infrastructure/wiring—Integration with AWS code deployment services—One-click deploy once it's polished—Local docker-based testing

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 41: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

SAM Downsides

—Very fiddly with templates/transformes—"Magic" YAML—No knowledge about platform packaging (NPM)—No knowledge of language-specific validation —Good for complex stuff, but painful for simple

tasks

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 42: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Strengths

—Time to deploy minimal—Time to recover irrelevant—Multi-versioned—Forces small, isolated code modules—Fine-grained, transparent, cost of operation—Use readily-available services built for massive

scale

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 43: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Weaknesses

—Non-deterministic Latency—"Only" 99.95% SLA—No way to keep open connections—Requires complete rethink on many common

practices—Configuration becomes a challenge

Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]

Page 44: Serverless Applications with AWS SAM —create …...Serverless Applications with AWS SAM —create auto-scaling web APIs —handle background processes —secure APIs —inspect and

Opportunities

—Skip a generation of technology/process upgrades

—Rethink architectural and operational "best practices"

—Change billing models—Marketplaces for digital services—Fine-grained monitoring and optimisation—A/B testing throughout Copyright: Gojko Adzic 2019, https://gojko.net, [email protected]