Slide 1

Slide 1 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Rob Sutter, Sr. Developer Advocate, Serverless May 13th, 2020 Getting Started with Serverless Best practices for serverless on AWS Richmond AWS User Group

Slide 2

Slide 2 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Agenda Getting started with serverless • What is serverless? • Serverless concepts • Execution models • Pricing and resource allocation • Permissions • Amazon API Gateway • Tooling • Observability

Slide 3

Slide 3 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Who am I? Rob Sutter - [email protected] • Senior Developer Advocate - Serverless • Gopher and Scala type • Previously: • Co-founded WorkFone, a SaaS startup • Infrastructure at an e-commerce startup • Consulting, government, odd jobs here and there • The Florida State University, Management Information Systems ’05 • Twitch: /robsutter • Twitter: @rts_rob

Slide 4

Slide 4 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What is serverless?

Slide 5

Slide 5 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. What is serverless? No infrastructure provisioning, no management Automatic scaling Pay for value Highly available and secure

Slide 6

Slide 6 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless means:

Slide 7

Slide 7 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless means: Greater agility Less operations More product focus Faster time to market Cost that grows with your business

Slide 8

Slide 8 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Today’s focus:

Slide 9

Slide 9 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda handles:

Slide 10

Slide 10 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless concepts

Slide 11

Slide 11 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless applications AWS Lambda

Slide 12

Slide 12 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless applications Function Node.js Python Java C# Go Ruby Runtime API

Slide 13

Slide 13 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless applications Event source Function Node.js Python Java C# Go Ruby Runtime API Changes in data state Requests to endpoints Changes in Resource state

Slide 14

Slide 14 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Serverless applications Event source Services Changes in data state Requests to endpoints Changes in Resource state Function Node.js Python Java C# Go Ruby Runtime API

Slide 15

Slide 15 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Anatomy of a Lambda function let response; exports.handler = async (event, context) => { try { response = { 'statusCode': 200, 'body': JSON.stringify({ message: 'hello world’, }) } } catch (err) { console.log(err); return err; } return response }; Handler function Function to be executed upon invocation Event object Data sent during Lambda function Invocation Context object Methods available to interact with runtime information (request ID, log group, more)

Slide 16

Slide 16 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Execution models

Slide 17

Slide 17 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda API 1. Lambda directly invoked via invoke API SDK clients API provided by the Lambda service Used by all other services that invoke Lambda across all models Supports sync and async Can pass any event payload structure you want Client included in every SDK Lambda function

Slide 18

Slide 18 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda execution model Synchronous (push) Asynchronous (event) Stream (poll-based) Amazon DynamoDB Amazon SNS /order Amazon S3 reqs Amazon Kinesis changes AWS Lambda service Amazon API Gateway Lambda function Lambda function function

Slide 19

Slide 19 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Pricing and resource allocation

Slide 20

Slide 20 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Fine-grained pricing Buy compute time in 100ms increments Low request charge No hourly, daily, or monthly minimums No per-device fees Never pay for idle* Free Tier 1M requests and 400,000 GBs of compute. Every month, every customer.

Slide 21

Slide 21 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Tweak your function’s compute power Lambda exposes only a memory control, with the % of CPU core and network capacity allocated to a function proportionally Is your code CPU, Network or memory-bound? If so, it could be cheaper to choose more memory.

Slide 22

Slide 22 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Smart resource allocation Match resource allocation (up to 3 GB!) to logic Stats for Lambda function that calculates 1000 times all prime numbers <= 1000000 128 MB 11.722965sec $0.024628 256 MB 6.678945sec $0.028035 512 MB 3.194954sec $0.026830 1024 MB 1.465984sec $0.024638 Green==Best Red==Worst

Slide 23

Slide 23 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Smart resource allocation Match resource allocation (up to 3 GB!) to logic Stats for Lambda function that calculates 1000 times all prime numbers <= 1000000 128 MB 11.722965sec $0.024628 256 MB 6.678945sec $0.028035 512 MB 3.194954sec $0.026830 1024 MB 1.465984sec $0.024638 Green==Best Red==Worst +$0.00001 -10.256981sec

Slide 24

Slide 24 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Permissions

Slide 25

Slide 25 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Lambda permissions model Function policies: • “Actions on bucket X can invoke Lambda function Z" • Resource policies allow for cross account access • Used for sync and async invocations Execution role: • “Lambda function A can read from DynamoDB table users” • Define what AWS resources/API calls can this function access via IAM • Used in streaming invocations Event source Services Function

Slide 26

Slide 26 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon API Gateway

Slide 27

Slide 27 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Function Services Event source On events

Slide 28

Slide 28 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Amazon API Gateway Create a unified API frontend for multiple micro- services Authenticate and authorize requests to a backend DDoS protection and throttling for your backend Throttle, meter, and monetize API usage by third- party developers

Slide 29

Slide 29 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Type of APIs Available Amazon API Gateway API Gateway Cache Amazon CloudWatch Monitoring Fully-managed CloudFront Distribution Edge-Optimized Regional Private Edge-Optimized • Utilizes CloudFront to reduce TLS connection overhead (reduces roundtrip time) • Designed for a globally distributed set of clients Regional • Recommended API type for general use cases • Designed for building APIs for clients in the same region Private • Only accessible from within VPC (and networks connected to VPC) • Designed for building APIs used internally or by private microservices

Slide 30

Slide 30 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Reduce application costs by up to 67% Reduce application latency by up to 50% Configure HTTP APIs easier and faster than before HTTP APIs for Amazon API Gateway Achieve up to 67% cost reduction and 50% latency reduction compared to REST APIs. HTTP APIs are also easier to configure than REST APIs, allowing customers to focus more time on building applications.

Slide 31

Slide 31 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Amplify Console Amazon API Gateway Client AWS Lambda Dynamic API Calls over HTTPS Amazon DynamoDB Amazon Cognito Authenticate HTML, CSS, JavaScript, etc. 100% serverless stack The future of web applications:

Slide 32

Slide 32 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Business workflow is rarely sequential start to finish Multiple decision paths Need to handle failure Multiple step processes

Slide 33

Slide 33 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Step Functions + Lambda “Serverless” workflow management with zero administration: • Makes it easy to coordinate the components of distributed applications and microservices using visual workflows • Automatically triggers and tracks each step and retries when there are errors, so your application executes in order and as expected • Logs the state of each step, so when things do go wrong, you can diagnose and debug problems quickly Choice Start ExtractImageMetadata CheckJobStatus Rekognition ImageTypeCheck NotSupportedImageType End Thumbnail AddRekognizedTags Tasks Failure capture Parallel tasks

Slide 34

Slide 34 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Step Functions: Integrations Simplify building workloads such as order processing, report generation, and data analysis Write and maintain less code; add services in minutes More service integrations: AWS Step Functions Amazon Simple Notification Service Amazon Simple Queue Service Amazon SageMaker AWS Glue AWS Batch Amazon Elastic Container Service AWS Fargate Amazon EMR

Slide 35

Slide 35 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Simpler integration, less code With serverless polling With direct service integration Start End AWS Lambda functions Start End No Lambda functions

Slide 36

Slide 36 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Tooling

Slide 37

Slide 37 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Where do you ... https://secure.flickr.com/photos/stevendepolo/57491920 ?

Slide 38

Slide 38 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Start with a framework AWS Chalice

Slide 39

Slide 39 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Serverless Application Model (SAM) AWS CloudFormation extension optimized for serverless Special serverless resource types: functions, APIs, tables, Layers and Applications Supports anything AWS CloudFormation supports Open specification (Apache 2.0) https://aws.amazon.com/serverless/sam

Slide 40

Slide 40 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS SAM Template AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetProductsFunction: Type: AWS::Serverless::Function Properties: Handler: index.getProducts Runtime: nodejs12.x CodeUri: src/ Policies: - DynamoDBReadPolicy: TableName: !Ref ProductTable Events: GetResource: Type: Api Properties: Path: /products/{productId} Method: get ProductTable: Type: AWS::Serverless::SimpleTable Just 20 lines to create: • Lambda function • IAM role • API Gateway • DynamoDB table

Slide 41

Slide 41 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS Cloud AWS SAM Template Amazon API Gateway Lambda function Table Role === To become this Allowing this AWSTemplateFormatVersion: '2010-09-09’ Transform: AWS::Serverless-2016-10-31 Resources: GetProductsFunction: Type: AWS::Serverless::Function Properties: Handler: index.getProducts Runtime: nodejs12.x CodeUri: src/ Policies: - DynamoDBReadPolicy: TableName: !Ref ProductTable Events: GetResource: Type: Api Properties: Path: /products/{productId} Method: get ProductTable: Type: AWS::Serverless::SimpleTable

Slide 42

Slide 42 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS SAM Command Line Interface (AWS SAM CLI) CLI tool for local development, debugging, testing, deploying, and monitoring of serverless applications Supports API Gateway “proxy-style” and Lambda service API testing Response object and function logs available on your local machine Uses open source docker-lambda images to mimic Lambda’s execution environment such as timeout, memory limits, runtimes Can tail production logs from CloudWatch logs Can help you build in native dependencies https://aws.amazon.com/serverless/sam

Slide 43

Slide 43 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. DEMO!

Slide 44

Slide 44 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Slide 45

Slide 45 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Observability

Slide 46

Slide 46 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. How can we measure this? Photo by Chris Munns

Slide 47

Slide 47 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metrics, logging, and tracing are universal rights! CloudWatch Metrics: • 7 Built in metrics for Lambda • Invocation Count, Invocation duration, Invocation errors, Throttled Invocation, Iterator Age, DLQ Errors, Concurrency • Can call “put-metric-data” from your function code for custom metrics • 7 Built in metrics for API-Gateway • API Calls Count, Latency, 4XXs, 5XXs, Integration Latency, Cache Hit Count, Cache Miss Count • Error and Cache metrics support averages and percentiles

Slide 48

Slide 48 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Metrics, logging, and tracing are universal rights! CloudWatch Logs: • API Gateway Logging • 2 Levels of logging, ERROR and INFO • Optionally log method request/body content • Set globally in stage, or override per method • Lambda Logging • Basic logging directly from your code with your language’s equivalent of console.log() • Structured JSON logging available with Embedded Metrics format • Log Pivots • Build metrics based on log filters • Jump to logs that generated metrics • Export logs to AWS ElastiCache or S3 • Explore with Kibana or Athena/QuickSight

Slide 49

Slide 49 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS X-Ray Profile and troubleshoot serverless applications: • Lambda instruments incoming requests for all supported languages and can capture calls made in code • API Gateway inserts a tracing header into HTTP calls as well as reports data back to X-Ray itself var AWSXRay = require(‘aws-xray-sdk-core‘); var AWS = AWSXRay.captureAWS(require(‘aws-sdk’)); S3Client = AWS.S3();

Slide 50

Slide 50 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. X-Ray Trace Example

Slide 51

Slide 51 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. AWS X-Ray Analytics Example

Slide 52

Slide 52 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Closing

Slide 53

Slide 53 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Review Getting started with serverless • What is serverless? • Serverless concepts • Execution models • Pricing and resource allocation • Permissions • Amazon API Gateway • Tooling • Observability

Slide 54

Slide 54 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. aws.amazon.com/serverless

Slide 55

Slide 55 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. aws.amazon.com/serverless/sam

Slide 56

Slide 56 text

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Thank you! Rob Sutter Email: [email protected] Twitch: /robsutter Twitter: @rts_rob