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

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).

Danilo Poccia

July 28, 2017
Tweet

More Decks by Danilo Poccia

Other Decks in Programming

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. 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
  3. { "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
  4. Web Browser CloudFront Edge Location S3 Bucket HTTPS Amazon Cognito

    Authentication (AWS Temporary Credentials) Static Content (HTML, CSS, JS)
  5. 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)
  6. 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)
  7. 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)
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. © 2016, Amazon Web Services, Inc. or its Affiliates. All

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