+ All Categories
Home > Documents > Serverless by Example: Building a Real-Time Chat System

Serverless by Example: Building a Real-Time Chat System

Date post: 21-Jan-2018
Category:
Upload: amazon-web-services
View: 342 times
Download: 3 times
Share this document with a friend
53
© 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
Transcript
Page 1: Serverless by Example: Building a Real-Time Chat System

© 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

@danilopdanilop

Page 2: Serverless by Example: Building a Real-Time Chat System

“Where” is Serverless?

Page 3: Serverless by Example: Building a Real-Time Chat System

Client Back End

AWS Lambda

Page 4: Serverless by Example: Building a Real-Time Chat System

CDNClient Back End Devices

Sensors

Actuators

AWS Lambda

Gateway

Page 5: Serverless by Example: Building a Real-Time Chat System

AWS Greengrass

Page 6: Serverless by Example: Building a Real-Time Chat System

Lambda Functions on a Raspberry Pi

AWS Greengrass

Lambda Function

Page 7: Serverless by Example: Building a Real-Time Chat System

AWS Snowball Edge100TB + Greengrass Core (≃ EC2 m4.4xlarge instance)

Page 8: Serverless by Example: Building a Real-Time Chat System

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

Page 9: Serverless by Example: Building a Real-Time Chat System

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

Page 10: Serverless by Example: Building a Real-Time Chat System

AWS Lambda@Edge

Customize content delivery while reducing load on the origin

Events / Triggers

Take serverless to your users

Page 11: Serverless by Example: Building a Real-Time Chat System

Lambda@Edge Use Cases

Demo build of a CloudFront+S3 distribution,

using Lambda@Edge to secure its HTTP headers

Page 12: Serverless by Example: Building a Real-Time Chat System

Lambda@Edge 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 Lambda@Edge) without provisioning or

managing servers.

Page 13: Serverless by Example: Building a Real-Time Chat System

Let’s build a Web Chat…

Page 14: Serverless by Example: Building a Real-Time Chat System

Web Browser

Page 15: Serverless by Example: Building a Real-Time Chat System

Web Browser

CloudFront Edge Location

S3 Bucket

HTTPS

Static Content (HTML, CSS, JS)

Page 16: Serverless by Example: Building a Real-Time Chat System

Web Browser

CloudFront Edge Location

S3 Bucket

HTTPS

Amazon Cognito

Authentication (AWS Temporary

Credentials)Static Content

(HTML, CSS, JS)

Page 17: Serverless by Example: Building a Real-Time Chat System

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)

Page 18: Serverless by Example: Building a Real-Time Chat System

Let’s make it more interactive…

Page 19: Serverless by Example: Building a Real-Time Chat System

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)

Page 20: Serverless by Example: Building a Real-Time Chat System

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)

Page 21: Serverless by Example: Building a Real-Time Chat System

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

Page 22: Serverless by Example: Building a Real-Time Chat System

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

Page 23: Serverless by Example: Building a Real-Time Chat System

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

Page 24: Serverless by Example: Building a Real-Time Chat System

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

Page 25: Serverless by Example: Building a Real-Time Chat System

What about Topics & Rules?

Page 26: Serverless by Example: Building a Real-Time Chat System

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

Page 27: Serverless by Example: Building a Real-Time Chat System

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

Page 28: Serverless by Example: Building a Real-Time Chat System

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

Page 29: Serverless by Example: Building a Real-Time Chat System

So how does the flow look like?

Page 30: Serverless by Example: Building a Real-Time Chat System
Page 31: Serverless by Example: Building a Real-Time Chat System
Page 32: Serverless by Example: Building a Real-Time Chat System
Page 33: Serverless by Example: Building a Real-Time Chat System
Page 34: Serverless by Example: Building a Real-Time Chat System
Page 35: Serverless by Example: Building a Real-Time Chat System
Page 36: Serverless by Example: Building a Real-Time Chat System
Page 37: Serverless by Example: Building a Real-Time Chat System

Demo

https://chat.danilop.net/websummit

Page 38: Serverless by Example: Building a Real-Time Chat System

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

Page 39: Serverless by Example: Building a Real-Time Chat System

From Event-Driven Architectures…

Page 40: Serverless by Example: Building a Real-Time Chat System

…To Functional Programming?

Page 41: Serverless by Example: Building a Real-Time Chat System

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 HughesThe 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 alsoto help functional programmers exploit its advantages to the full by making itclear what those advantages are.

Functional programming is so called because its fundamental operation isthe application of functions to arguments. A main program itself is written asa function that receives the program’s input as its argument and delivers theprogram’s output as its result. Typically the main function is defined in terms ofother functions, which in turn are defined in terms of still more functions, untilat the bottom level the functions are language primitives. All of these functionsare much like ordinary mathematical functions, and in this paper they will be

1An 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

Page 42: Serverless by Example: Building a Real-Time Chat System

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

images

Page 43: Serverless by Example: Building a Real-Time Chat System

getLabelsgroupBy

+ renderHtml

images

Amazon Rekognition

browser

Event-Driven ArchitectureLet’s build a “Tag Cloud” from Your Pictures

Page 44: Serverless by Example: Building a Real-Time Chat System

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

Functional ViewLet’s build a “Tag Cloud” from Your Pictures

Page 45: Serverless by Example: Building a Real-Time Chat System

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

Functional ViewLet’s build a “Tag Cloud” from Your Pictures

Page 46: Serverless by Example: Building a Real-Time Chat System

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

Functional ViewLet’s build a “Tag Cloud” from Your Pictures

Page 47: Serverless by Example: Building a Real-Time Chat System

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

Functional ViewLet’s build a “Tag Cloud” from Your Pictures

Page 48: Serverless by Example: Building a Real-Time Chat System

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}

Page 49: Serverless by Example: Building a Real-Time Chat System

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

renderHtml(count(map(getLabel, [images]))), htmlParams)

Reusable More reusable

Page 50: Serverless by Example: Building a Real-Time Chat System

Lambda Functions + Step Functions

Lambda Function

Input

State Machine

Outputtranslate create

executeTasks

State passing (Immutable Data)

Tasks are performed only if required (Lazy Evaluation)

A Lambda Function dynamically creating a State Machine executing the logic

Page 51: Serverless by Example: Building a Real-Time Chat System

Event-Driven Architectures +

Functional Programming +

Standard Serverless Library

Happy to get your feedback on this!

Page 52: Serverless by Example: Building a Real-Time Chat System

What is Serverless?

“Build and run applications

without thinking about servers”

Page 53: Serverless by Example: Building a Real-Time Chat System

© 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

@danilopdanilop


Recommended