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

Serverless by Example: Building a Real-Time Chat System

Serverless by Example: Building a Real-Time Chat System

Web Summit, Lisbon, November 9th, 2017

One of the great advantages of serverless development is the possibility to “chain” multiple functions and tools together. This session will show as an example how to build a scalable and highly available web chat for real-time communications.

Danilo Poccia

November 09, 2017
Tweet

More Decks by Danilo Poccia

Other Decks in Programming

Transcript

  1. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Danilo Poccia, Technical Evangelist
    [email protected]
    Serverless by Example:
    Building A Real-Time Chat System
    @danilop
    danilop

    View Slide

  2. “Where” is Serverless?

    View Slide

  3. Client Back End
    AWS
    Lambda

    View Slide

  4. CDN
    Client Back End Devices
    Sensors
    Actuators
    AWS
    Lambda
    Gateway

    View Slide

  5. AWS Greengrass

    View Slide

  6. Lambda Functions on a Raspberry Pi
    AWS
    Greengrass
    Lambda
    Function

    View Slide

  7. AWS Snowball Edge
    100TB + Greengrass Core (≃ EC2 m4.4xlarge instance)

    View Slide

  8. Snowball Edge Use Cases
    “Snowball Edge enables us to extend the
    innovative capabilities of HealthSuite, our cloud-
    enabled connected health ecosystem of devices,
    applications and digital tools supported by AWS,
    even when there is no network support.”
    Embedded Applications
    —Dale Wiggins,
    Business Leader, HealthSuite digital platform,
    Philips

    View Slide

  9. Snowball Edge Use Cases
    “With AWS Snowball Edge, we can now collect
    100 TB of data with no intermediate steps, and
    we can also analyze the images immediately
    using the onboard compute capabilities.”
    Remote Locations for data collection and analysis
    — Bob Cowen,
    Director of Hatfield Marine Research Center,
    Oregon State University

    View Slide

  10. AWS [email protected]
    Customize content delivery while reducing load on the origin
    Events / Triggers
    Take serverless to your users

    View Slide

  11. [email protected] Use Cases
    Demo build of a
    CloudFront+S3 distribution,
    using [email protected]
    to secure its HTTP headers

    View Slide

  12. [email protected] Use Cases
    These processes include applying
    transactional labels to purchases so
    Blockbuster can track customer activity, and
    providing personalized recommendations
    based on previous purchases.
    Blockbuster runs serverless compute
    processes across AWS Regions and
    Amazon CloudFront edge locations (using
    [email protected]) without provisioning or
    managing servers.

    View Slide

  13. Let’s build a Web Chat…

    View Slide

  14. Web
    Browser

    View Slide

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

    View Slide

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

    View Slide

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

  18. Let’s make it more interactive…

    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/websummit

    View Slide

  38. Code
    https://github.com/danilop/serverless-chat
    More Info
    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

  39. From Event-Driven Architectures…

    View Slide

  40. …To Functional Programming?

    View Slide

  41. Functional Programming
    Transforming Data
    Using Expressions (Functions)
    Without Side Effects
    Immutable Data
    Pure Functions
    Higher-Order Functions
    From “Research Topics in Functional Programming” ed. D. Turner, Addison-Wesley, 1990, pp 17–42.
    1
    Why
    Functional Programming
    Matters
    John Hughes
    The University, Glasgow
    Abstract
    As software becomes more and more complex, it is more and more
    important to structure it well. Well-structured software is easy to write
    and to debug, and provides a collection of modules that can be reused
    to reduce future programming costs. In this paper we show that two fea-
    tures of functional languages in particular, higher-order functions and lazy
    evaluation, can contribute significantly to modularity. As examples, we
    manipulate lists and trees, program several numerical algorithms, and im-
    plement the alpha-beta heuristic (an algorithm from Artificial Intelligence
    used in game-playing programs). We conclude that since modularity is the
    key to successful programming, functional programming o↵ers important
    advantages for software development.
    1 Introduction
    This paper is an attempt to demonstrate to the larger community of (non-
    functional) programmers the significance of functional programming, and also
    to help functional programmers exploit its advantages to the full by making it
    clear what those advantages are.
    Functional programming is so called because its fundamental operation is
    the application of functions to arguments. A main program itself is written as
    a function that receives the program’s input as its argument and delivers the
    program’s output as its result. Typically the main function is defined in terms of
    other functions, which in turn are defined in terms of still more functions, until
    at the bottom level the functions are language primitives. All of these functions
    are much like ordinary mathematical functions, and in this paper they will be
    1
    An earlier version of this paper appeared in the The Computer Journal, 32(2):98–107,
    April 1989. Copyright belongs to The British Computer Society, who grant permission to
    copy for educational purposes only without fee provided the copies are not made for direct
    commercial advantage and this BCS copyright notice appears.
    https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf

    View Slide

  42. Let’s build a “Tag Cloud” from Your Pictures
    images

    View Slide

  43. getLabels
    groupBy
    +
    renderHtml
    images
    Amazon
    Rekognition
    browser
    Event-Driven Architecture
    Let’s build a “Tag Cloud” from Your Pictures

    View Slide

  44. renderHtml(groupBy(getLabels([images])), htmlParams)
    Functional View
    Let’s build a “Tag Cloud” from Your Pictures

    View Slide

  45. renderHtml(groupBy(getLabels([images])), htmlParams)
    Functional View
    Let’s build a “Tag Cloud” from Your Pictures

    View Slide

  46. renderHtml(groupBy(getLabels([images])), htmlParams)
    Functional View
    Let’s build a “Tag Cloud” from Your Pictures

    View Slide

  47. renderHtml(groupBy(getLabels([images])), htmlParams)
    Functional View
    Let’s build a “Tag Cloud” from Your Pictures

    View Slide

  48. map(f, [x1, x2, …, xn]) = [f(x1), f(x2), …, f(xn)]
    Higher-Order
    Functions
    filter(f, [x1, x2, …, xn]) = [xi if f(xi) is true]
    count([a, b, a, c, a, b]) = {a: 3, b: 2, c: 1}

    View Slide

  49. renderHtml(groupBy(getLabels([images])), htmlParams)

    renderHtml(count(map(getLabel, [images]))), htmlParams)
    Reusable
    More
    reusable

    View Slide

  50. Lambda Functions + Step Functions
    Lambda
    Function
    Input
    State
    Machine
    Output
    translate
    create
    execute
    Tasks
    State passing (Immutable Data)
    Tasks are performed only if required (Lazy Evaluation)
    A Lambda Function dynamically creating a State Machine executing the logic

    View Slide

  51. Event-Driven Architectures
    +
    Functional Programming
    +
    Standard Serverless Library
    Happy to get your feedback on this!

    View Slide

  52. What is Serverless?
    “Build and run
    applications
    without thinking
    about servers”

    View Slide

  53. https://bit.ly/devworkshops

    View Slide

  54. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
    Danilo Poccia, Technical Evangelist
    [email protected]
    Serverless by Example:
    Building A Real-Time Chat System
    @danilop
    danilop

    View Slide