$30 off During Our Annual Pro Sale. View Details »

Best practices for serverless on AWS

Best practices for serverless on AWS

As presented to the Richmond AWS User Group on Wednesday, 13 May 2020.

Rob Sutter

May 13, 2020
Tweet

More Decks by Rob Sutter

Other Decks in Technology

Transcript

  1. © 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
  2. © 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
  3. © 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
  4. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. What is serverless?
  5. © 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
  6. © 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
  7. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

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

    rights reserved. Serverless applications Function Node.js Python Java C# Go Ruby Runtime API
  10. © 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
  11. © 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
  12. © 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)
  13. © 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
  14. © 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
  15. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Pricing and resource allocation
  16. © 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.
  17. © 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.
  18. © 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
  19. © 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
  20. © 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
  21. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

    rights reserved. Function Services Event source On events
  23. © 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
  24. © 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
  25. © 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.
  26. © 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:
  27. © 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
  28. © 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
  29. © 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
  30. © 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
  31. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

    rights reserved. Start with a framework AWS Chalice
  33. © 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
  34. © 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
  35. © 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
  36. © 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
  37. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How can we measure this? Photo by Chris Munns
  38. © 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
  39. © 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
  40. © 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();
  41. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

    rights reserved. AWS X-Ray Analytics Example
  43. © 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
  44. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

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

    rights reserved. Thank you! Rob Sutter Email: [email protected] Twitch: /robsutter Twitter: @rts_rob