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

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

    View Slide

  2. @AWSStartups
    #AWSLoft

    View Slide

  3. What is Serverless?

    View Slide

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

    View Slide

  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

    View Slide

  6. Lambda
    Function
    You can “chain” events to build
    Event-Driven Applications
    S3
    Bucket
    DynamoDB
    Table
    Lambda
    Function

    View Slide

  7. View Slide

  8. View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. View Slide

  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

    View Slide

  14. Let’s build a Web Chat…

    View Slide

  15. Web
    Browser

    View Slide

  16. Web
    Browser
    CloudFront
    Edge Location
    S3
    Bucket
    HTTPS
    Static Content
    (HTML, CSS, JS)

    View Slide

  17. Web
    Browser
    CloudFront
    Edge Location
    S3
    Bucket
    HTTPS
    Amazon
    Cognito
    Authentication
    (AWS Temporary
    Credentials)
    Static Content
    (HTML, CSS, JS)

    View Slide

  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)

    View Slide

  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)

    View Slide

  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)

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  25. What about Topics & Rules?

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  29. So how does the flow look like?

    View Slide

  30. View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. View Slide

  36. View Slide

  37. Demo
    https://chat.danilop.net/SFLoft
    Code
    https://github.com/danilop/serverless-chat

    View Slide

  38. What next?

    View Slide

  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

    View Slide

  40. © 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Danilo Poccia, Technical Evangelist
    @danilop
    Thank You!

    View Slide