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
rights reserved. Agenda Getting started with serverless • What is serverless? • Serverless concepts • Execution models • Pricing and resource allocation • Permissions • Amazon API Gateway • Tooling • Observability
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
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
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
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)
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
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
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.
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.
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
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
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
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
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
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.
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:
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
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
rights reserved. Simpler integration, less code With serverless polling With direct service integration Start End AWS Lambda functions Start End No Lambda functions
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
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
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
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
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();
rights reserved. Review Getting started with serverless • What is serverless? • Serverless concepts • Execution models • Pricing and resource allocation • Permissions • Amazon API Gateway • Tooling • Observability