Serverless for Developers

Serverless for Developers

AWS Pop-up Loft, San Francisco, July 28th, 2017

Building a real-time web chat using AWS Lambda, AWS IoT (for WebSockets) and Amazon DynamoDB (with TTL and Auto Scaling).

7c9b8b368924556d8642bdaed3ded1f5?s=128

Danilo Poccia

July 28, 2017
Tweet

Transcript

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

    rights reserved. Danilo Poccia, Technical Evangelist @danilop Serverless for Developers Tips for Your Next App
  2. @AWSStartups #AWSLoft

  3. What is Serverless?

  4. Lambda Function Trigger Do whatever you want: It’s Your Code!

  5. Lambda Function Trigger Do whatever you want: It’s Your Code!

    S3 Bucket Amazon API Gateway DynamoDB Table AWS IoT Rule Much More! Amazon CloudWatch
  6. Lambda Function You can “chain” events to build Event-Driven Applications

    S3 Bucket DynamoDB Table Lambda Function
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. { "resource": "Resource path", "path": "Path parameter", "httpMethod": "Incoming request's

    method name" "headers": {Incoming request headers} "queryStringParameters": {query string parameters } "pathParameters": {path parameters} "stageVariables": {Applicable stage variables} "requestContext": {Request context, including authorizer-returned key-value pairs} "body": "A JSON string of the request payload." "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode" } { “statusCode": httpStatusCode, "headers": { "headerName": "headerValue", ... }, "body": “...", "isBase64Encoded": true|false } Output Format of a Lambda Function for Proxy Integration Input Format of a Lambda Function for Proxy Integration
  14. Let’s build a Web Chat…

  15. Web Browser

  16. Web Browser CloudFront Edge Location S3 Bucket HTTPS Static Content

    (HTML, CSS, JS)
  17. Web Browser CloudFront Edge Location S3 Bucket HTTPS Amazon Cognito

    Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS)
  18. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) HTTPS Dynamic Content Database Access Execute Functions Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS)
  19. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Execute Functions Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS)
  20. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) AWS IoT Rule(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Subscribe Republish Execute Functions Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS)
  21. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) AWS IoT Rule(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Subscribe Republish Execute Functions Execute Functions Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS) Publish
  22. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) AWS IoT Rule(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Subscribe Republish Execute Functions Write to DynamoDB Execute Functions Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS) Publish
  23. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) AWS IoT Rule(s) Kinesis Stream(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Subscribe Republish Execute Functions Streaming Data Write to DynamoDB Execute Functions Execute Functions (Micro-Batches) Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS) Publish
  24. Web Browser CloudFront Edge Location S3 Bucket Amazon API Gateway

    Lambda Function(s) DynamoDB Table(s) AWS IoT Topic(s) AWS IoT Rule(s) Kinesis Stream(s) HTTPS Dynamic Content Database Access MQTT over Secure WebSockets (Publish, Subscribe) Subscribe Republish Execute Functions Streaming Data Write to DynamoDB Execute Functions Execute Functions (Micro-Batches) Amazon Cognito Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS) Publish
  25. What about Topics & Rules?

  26. Web Browser AWS IoT Topics chat/in/${iot:ClientId} chat/out chat/pub/${room} chat/stream $aws/events/#

    Each client has a secure “input” topic to receive content from the back end All client can publish on an “output” topic, they are recognizable by their clientId All client can subscribe and receive from “public” topics Another “output” topic optimized for high volumes AWS IoT Lifecycle Events
  27. Web Browser AWS IoT Topics AWS IoT Rules chat/in/${iot:ClientId} chat/out

    chat/pub/${room} chat/stream $aws/events/# Each client has a secure “input” topic to receive content from the back end All client can publish on an “output” topic, they are recognizable by their clientId All client can subscribe and receive from “public” topics Another “output” topic optimized for high volumes AWS IoT Lifecycle Events Lambda function to process important messages, such as a new client connection Republish rule to send messages to the corresponding room public topic Lambda function to process high volume dreaming data Kinesis Stream to manage high volume streaming data Lambda function to process AWS IoT Lifecycle Events Store all messages in a database, to be sent to the client on connection
  28. Web Browser AWS IoT Topics AWS IoT Rules chat/in/${iot:ClientId} chat/out

    chat/pub/${room} chat/stream $aws/events/# Each client has a secure “input” topic to receive content from the back end All client can publish on an “output” topic, they are recognizable by their clientId All client can subscribe and receive from “public” topics Another “output” topic optimized for high volumes AWS IoT Lifecycle Events Lambda function to process important messages, such as a new client connection Republish rule to send messages to the corresponding room public topic Lambda function to process high volume dreaming data Kinesis Stream to manage high volume streaming data Lambda function to process AWS IoT Lifecycle Events Store all messages in a database, to be sent to the client on connection You can use this topic to inject JavaScript code in the browser
  29. So how does the flow look like?

  30. None
  31. None
  32. None
  33. None
  34. None
  35. None
  36. None
  37. Demo https://chat.danilop.net/SFLoft Code https://github.com/danilop/serverless-chat

  38. What next?

  39. https://aws.amazon.com/serverless http://docs.aws.amazon.com/iot/latest/developerguide/protocols.html http://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

  40. © 2016, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Danilo Poccia, Technical Evangelist @danilop Thank You!