Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting Started with Serverless

Getting Started with Serverless

In this tech talk, learn how to get started with serverless applications with AWS Lambda, which lets you run code without provisioning or managing servers. We will introduce you to the basics of building with Lambda and its event-driven model, the various event types and sources that exist, and functionality that will allow you to build performant and well secured applications. Expect to leave with a solid understanding of the tools available to help you build your first serverless application successfully.

Learning Objectives:
*Understand the benefits of serverless computing, architectures, and applications
*Learn basic patterns and architectures for building apps with Lambda
*Know where to start to build serverless applications with best practices

***To learn more about the services featured in this talk, please visit: https://aws.amazon.com/lambda/

Rob Sutter

April 22, 2020
Tweet

More Decks by Rob Sutter

Other Decks in Programming

Transcript

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

    rights reserved. Rob Sutter, Sr. Developer Advocate, Serverless April 22, 2020 Getting Started with Serverless
  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. Event-driven compute Functions as a service Serverless FaaS
  8. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

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

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

    rights reserved. Common AWS Lambda use cases Web Apps Backends Data Processing Chatbots Amazon Alexa IT Automation • Static websites • Complex web apps • Packages for Flask and Express • Apps & services • Mobile • IoT • Real time • MapReduce • Batch • Powering chatbot logic • Powering voice- enabled apps • Alexa Skills Kit • Policy engines • Extending AWS services • Infrastructure management
  17. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

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

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

    rights reserved. API Architecture Websites Services Amazon API Gateway API Gateway Cache Public Endpoints on Amazon EC2 Amazon CloudWatch Monitoring All publicly accessible endpoints Lambda Functions Endpoints in VPC Applications & Services in VPC Any other AWS service Fully-managed CloudFront Distribution Edge-Optimized Regional Private Applications & Services in the same AWS Region AWS Direct Connect On-premises HTTPS Mobile client Customer-managed CloudFront Distribution
  28. © 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.
  29. © 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:
  30. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Serverless architectures: more than just web apps 1. File put into bucket 2. Lambda invoked 2. Lambda invoked 1. Data published to a topic Data 1. Message inserted into to a queue 3. Function removes message from queue 2. Lambda polls queue and invokes function Message Object Amazon SNS Amazon EventBridge Amazon S3 AWS Lambda AWS Lambda AWS Lambda Amazon SQS
  31. © 2020, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Serverless architectures: more than just web apps 2. Lambda polls stream 1. Data published to a stream 3. Amazon Kinesis returns stream data Data 2. Lambda invoked 1. Chatbot conversation needs “fulfillment” Chatbot 1. Scheduled time occurs 2. Lambda invoked Event (time-based) AWS Lambda AWS Lambda AWS Lambda Amazon Kinesis Data Streams Amazon Lex
  32. © 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
  33. © 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
  34. © 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
  35. © 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
  36. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

    rights reserved. Start with a framework AWS Chalice
  38. © 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
  39. © 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
  40. © 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
  41. © 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
  42. © 2020, Amazon Web Services, Inc. or its Affiliates. All

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

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

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

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

    rights reserved. aws.amazon.com/serverless/sam