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

Building Serverless Interactive Applications on...

Building Serverless Interactive Applications on AWS

The quicker you can get from an idea to first results, the more you can experiment and innovate with your data, perform ad-hoc analysis, and drive answers to new business questions. In this talk, I will also show how to efficiently build serverless applications, including the use of IoT, Streaming Data and AI services.

Adrian Hornsby

June 20, 2018
Tweet

More Decks by Adrian Hornsby

Other Decks in Technology

Transcript

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

    rights reserved. Adrian Hornsby, Cloud Architecture Evangelist Building Serverless Interactive Apps @adhorn
  2. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Real-Time Analytics Internet of Things Machine Learning
  3. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Real-time streaming
  4. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Stream New Data in Seconds Get actionable insights quickly Streaming Ingest video & data as it’s generated Process data on the fly Real-time analytics/ML, alerts, actions
  5. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Producers Consumers Shard 1 Shard 2 Shard n Shard 3 … … Write: 1MB Read: 2MB ** A shard is a group of data records in a stream Amazon Kinesis
  6. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Streaming data in the cloud with Kinesis
  7. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Clash Royal: Getting relevant value for the players
  8. Real-time analytics Amazon Kinesis Stream Amazon Kinesis Analytics Amazon Cognito

    Amazon Kinesis Stream Amazon DynamoDB Amazon Lambda Amazon S3 JavaScript SDK
  9. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Data Input Source JSON Data • Once per second, using JavaScript SDK: • Unique Cognito ID (anonymous user) • OS • Quadrant • Data sent to Kinesis Stream Amazon Kinesis Stream Amazon Cognito Amazon S3 JavaScript SDK { "recordTime": 1486505943.204, "cognitoId": "us-east-1:3626e211-d2a3-447b-8231-e1f4e0486f44", "os": "Android", "quadrant": "A”, … }
  10. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How is raw data mapped to a schema? Amazon Kinesis stream Amazon Kinesis Analytics cognitoID os quadrant <guid1> Android A <guid2> iOS B Source Data for Kinesis Analytics { "recordTime": 1486505943.204, "cognitoId": "us-east-1:<guid>", "os": "Android", "quadrant": "A" }
  11. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How is streaming data accessed with SQL? STREAM • Analogous to a TABLE • Represents continuous data flow CREATE OR REPLACE STREAM DISTINCT_USER_STREAM( COGNITO_ID VARCHAR(64), DEVICE VARCHAR(32), OS VARCHAR(32), QUADRANT char(1), DT TIMESTAMP);
  12. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How is streaming data accessed with SQL? PUMP • Continuous INSERT query • Inserts data from one in-application stream to another CREATE OR REPLACE PUMP "DISTINCT_USER_PUMP" AS INSERT INTO "DISTINCT_USER_STREAM" SELECT STREAM DISTINCT "cognitoId", ...
  13. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How do we model our data? DISTINCT_USERS_STREAM •COGNITO_ID •OS •QUADRANT •DT DESTINATION_SQL_STREAM •UNIQUE_USER_COUNT •ANDROID_COUNT •IOS_COUNT •OTHER_OS_COUNT •QUADRANT_A_COUNT •QUADRANT_B_COUNT •QUADRANT_C_COUNT •QUADRANT_C_COUNT SOURCE_STREAM •cognitoID •os •quadrant Kinesis stream Kinesis Stream Pump Kinesis Analytics Application
  14. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How do we get distinct user records? Use PUMP to insert distinct records into in-app STREAM CREATE OR REPLACE PUMP "DISTINCT_USER_PUMP" AS INSERT INTO "DISTINCT_USER_STREAM" SELECT STREAM DISTINCT "cognitoId", "device", "os", "quadrant", FLOOR(s.ROWTIME TO SECOND) FROM "SOURCE_SQL_STREAM_001" s; DISTINCT_USERS_STREAM •COGNITO_ID •OS •QUADRANT •DT SOURCE_STREAM •cognitoID •os •quadrant
  15. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. How do we aggregate per second? • Tumbling window, group by time period CREATE OR REPLACE PUMP "OUTPUT_PUMP" AS INSERT INTO "DESTINATION_SQL_STREAM" SELECT STREAM COUNT(dus.COGNITO_ID) AS UNIQUE_USER_COUNT, COUNT((CASE WHEN dus.OS = 'Android' THEN COGNITO_ID ELSE null END)) AS ANDROID_COUNT, COUNT((CASE WHEN dus.OS = 'iOS' THEN COGNITO_ID ELSE null END)) AS IOS_COUNT, COUNT((CASE WHEN dus.OS = 'Windows Phone' THEN COGNITO_ID ELSE null END)) AS WINDOWS_PHONE_COUNT, COUNT((CASE WHEN dus.OS = 'other' THEN COGNITO_ID ELSE null END)) AS OTHER_OS_COUNT, COUNT((CASE WHEN dus.QUADRANT = 'A' THEN COGNITO_ID ELSE null END)) AS QUADRANT_A_COUNT, COUNT((CASE WHEN dus.QUADRANT = 'B' THEN COGNITO_ID ELSE null END)) AS QUADRANT_B_COUNT, COUNT((CASE WHEN dus.QUADRANT = 'C' THEN COGNITO_ID ELSE null END)) AS QUADRANT_C_COUNT, COUNT((CASE WHEN dus.QUADRANT = 'D' THEN COGNITO_ID ELSE null END)) AS QUADRANT_D_COUNT, ROWTIME FROM "DISTINCT_USER_STREAM" dus GROUP BY FLOOR(dus.ROWTIME TO SECOND);
  16. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Comparing Types of Windows • Output created at the end of the window • The output of the window will be single event based on the aggregate function used Tumbling window Aggregate per time interval Sliding window Windows constantly re-evaluated
  17. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Output to Kinesis Stream MENTION_COUNT_STREAM •UNIQUE_USER_COUNT •ANDROID_COUNT •… Amazon Kinesis Stream { "unique_user_count": 96, "android_count": 50, "ios_count": 46, "android_count": 50, "quadrant_a_count": 80, "quadrant_b_count ": 10, "quadrant_c_count ": 3, "quadrant_d_count ": 3 } 1 record, every second
  18. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Persist aggregated data in DynamoDB Kinesis Stream Lambda DynamoDB event.Records.forEach((record) => { const payload = new Buffer(record.kinesis.data, 'base64').toString('ascii'); var docClient = new AWS.DynamoDB.DocumentClient(); var table = "user-quadrant-data"; var data = JSON.parse(payload); var params = { TableName: table, Item:{ "dataType": "quadrantRollup", "windowtime": (new Date(data.WINDOW_TIME)).getTime(), "userCount": data.UNIQUE_USER_COUNT, "quadrantA": data.QUADRANT_A_COUNT, "quadrantB": data.QUADRANT_B_COUNT, ... } }; docClient.put(params, function(err, data) { ... Lambda event source mapping
  19. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Processing a Kinesis Streams with AWS Lambda Shard 1 Shard 2 Shard 3 Shard 4 Shard n Kinesis Stream . . . . . . • Single instance of Lambda function per shard • Polls shard once per second • Lambda function instances created and removed automatically as stream is scaled Gets Records 1x per sec 10k records
  20. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Internet of Things
  21. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Registry: Identity for devices Rules and Actions: Match patterns and take actions Shadows: Virtual representation of the device in the cloud. {Thing Name, Sensor Temp, , GetTemp(), Output LED} Rules Engine Shadow Registry Amazon S3, AWS Lambda, Kinesis DynamoDB SNS Elasticsearch Machine Learning Mobile App AWS IoT: Key features
  22. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Secure by Default: Connect securely via X509 Certs and TLS v1.2 Client Mutual Auth Multi-protocol Message Gateway: MQTT, HTTP or WebSockets. Elastic Pub Sub Broker: from 1 to 1-billion long-lived connections with zero provisioning Subscribers Publishers AWS IoT: Key features
  23. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Sensor data collection IoT rules IoT actions MQTT Amazon S3: Raw records Amazon Kinesis Firehose: Delivery stream Amazon S3: Batched records Amazon Kinesis Streams: Real-time stream AWS IoT: Data collection IoT Sensors Real-time analytics applications
  24. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. AWS IoT IoT shadow Amazon Cognito MQTT over WebSockets AWS Lambda Alexa Amazon S3 Interactive Serverless applications
  25. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2017, Amazon Web Services, Inc. or its Affiliates. All rights reserved. Machine Learning
  26. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. FRAMEWORKS AND INTERFACES PLATFORM SERVICES APPLICATION SERVICES Amazon Rekognition Amazon Polly Amazon Lex Machine Learning on AWS Amazon Rekognition Video Amazon Transcribe Amazon Comprehend Amazon SageMaker AWS DeepLens Amazon EMR Deep Learning AMI Amazon Translate
  27. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Amazon Rekognition Deep Learning-based image analysis service
  28. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Object & Scene Detection
  29. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Crowd Detection – up to 100 faces
  30. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Marinus Analytics uses facial recognition to stop human trafficking “Now with Traffic Jam’s FaceSearch, powered by Amazon Rekognition, investigators are able to take effective action by searching through millions of records in seconds to find victims.” http://www.marinusanalytics.com/articles/2017/10/17/amazon-rekognition-helps-marinus-analytics-fight-human-trafficking
  31. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Rekognition API example aws rekognition detect-labels –-image '{"S3Object":{"Bucket":"adhorn-reko","Name":"horse.jpg"}}' { "Labels": [ { "Confidence": 99.29136657714844, "Name": "Human" }, { "Confidence": 99.29136657714844, "Name": "People" }, { "Confidence": 99.29136657714844, "Name": "Person" }, ……
  32. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Rekognition API example aws rekognition detect-faces --image '{"S3Object":{"Bucket":"adhorn-reko","Name":"horse.jpg"}}' --attributes "ALL” { "FaceDetails": [ { "BoundingBox": { "Width": 0.05462963134050369, "Top": 0.2880098819732666, "Left": 0.4722222089767456, "Height": 0.07292954623699188 }, "Landmarks": [ { "Y": 0.31606796383857727, "X": 0.48852023482322693, "Type": "eyeLeft" ………
  33. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Amazon Polly Deep Learning-based text-to-speech service
  34. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Amazon Polly “What's the difference between a hippo and a zippo? One is really heavy, and the other is a little lighter.” Amazon Polly: Text In, Life-like Speech Out
  35. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. <speak xml:lang="en-US"> The price of this book is <prosody rate="60%">45€</prosody> </speak> A Focus On Voice Quality & Pronunciation Support for Speech Synthesis Markup Language (SSML) Version 1.0 https://www.w3.org/TR/speech-synthesis
  36. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Polly API example aws polly synthesize-speech --text "It was nice to live such a wonderful live show" --output-format mp3 --voice-id Joanna --text-type text johanna.mp3 aws polly synthesize-speech --text-type ssml --text file://ssml_polly --output-format mp3 --voice-id Joanna speech.mp3
  37. © 2017, Amazon Web Services, Inc. or its Affiliates. All

    rights reserved. Demo – ML services (CLI) + Poliko https://github.com/adhorn/poliko