serverless gardens · a serverless architecture is an event driven system that utilises and other...

50
Serverless Gardens IoT + Serverless twitter.com/@johncmckim johncmckim.me medium.com/@johncmckim

Upload: others

Post on 06-Jul-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Serverless GardensIoT + Serverless

twitter.com/@johncmckim

johncmckim.me

medium.com/@johncmckim

Page 2: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Software Engineer at A Cloud Guru

John McKim

@johncmckim

Contribute to Serverless Framework

Page 3: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

brisbanejs.acloud.guru

Page 4: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Serverless Framework

serverless.com

Page 5: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Agenda

What is ServerlessWhy I built this projectOverall ArchitectureDesign of each MicroserviceGraphQL + LambdaWhat I learntQuestions

Page 6: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

What is Serverless?

Page 7: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

ServerlessFaaS + The Herd

Page 8: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

What is Serverless?

A Serverless Architecture is an event driven systemthat utilises and other fully managed

services for logic and persistence.FaaS

Page 9: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Why choose Serverless?Benefits

Easier Operations MangementReduced Operational CostReduced Development Time / CostHighly ScalableLoosely Coupled systems

Page 10: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Why build this?For fun and learning

Page 11: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

The ProblemCaring for my Garden

Page 12: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Serverless Garden

Page 13: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

IoT Service

Page 14: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

AWS IoT ServiceHow It works

Page 15: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Device GatwayProtocols

MQTT  - devicesMQTT over Web Sockets  - browsersHTTP  - last resort

Page 16: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Device GatwayAuthentication

X.509 Certificates - Mutual TLSIAM - Signed RequestsCognito - tokens

Page 17: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

DeviceFake Device

const awsIot = require('aws-iot-device-sdk');

const device = awsIot.device({ 'keyPath': './certificates/private.pem.key', 'certPath': './certificates/certificate.pem.crt', 'caPath': './certificates/verisign-ca.pem', 'clientId': 'garden-aid-client-test-js', 'region': 'ap-southeast-2'});

device .on('connect', function() { const topic = 'garden/soil/moisture'; const message = JSON.stringify({ DeviceId: 'test-js-device', Recorded: (new Date()).toISOString(), Level: level });

device.publish(topic, message, {}); });

Page 18: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

DemoFake Device

Page 19: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Rules EngineMessage Selection & Transformation

SQL Statement

FROM — MQTT topicSELECT — transforms the dataWHERE (optional)

SELECT DeviceId, Recorded, Level FROM'garden/soil/moisture'

Page 20: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Rules EngineActions

LambdaDynamoDBElasticSearchSNSSQSKinesisCloudWatchRepublish to another MQTT topic.

Page 21: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Rules EngineIoT Rule in serverless.yml

SensorThingRule: Type: AWS::IoT::TopicRule Properties: TopicRulePayload: RuleDisabled: false Sql: "SELECT DeviceId, Recorded, Level FROM '${{opt:stage}}/garden/soil/moisture'" Actions: - DynamoDB: TableName: { Ref: MoistureData } HashKeyField: "ClientId" HashKeyValue: "${clientId()}" RangeKeyField: "Timestamp" RangeKeyValue: "${timestamp()}" PayloadField: "Data" RoleArn: { Fn::GetAtt: [ IotThingRole, Arn ] } - Lambda: FunctionArn: { Fn::GetAtt: [ checkMoistureLevel, Arn ] }

Page 22: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Notifications Service

Single purpose functionsHigh cohesionLoose coupling

Page 23: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Messaging OptionsAmazon Simple Queue Service (SQS)

Benefits

Dead letter queuesReliable

Drawbacks

No integration with LambdaDifficult to build scalableprocessorSingle processor / queue

Fully Managed message queuing service.

Page 24: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Messaging OptionsAmazon Kinesis Streams

Benefits

Integrates with LambdaBatched messagesOrdered messages

Drawbacks

Single lambda / shardScale per shardLog jamsMessages expire

Capture and store streaming data.

Page 25: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Messaging OptionsAmazon Simple Notification Service (SNS)

Benefits

Integrates with LambdaFan out multipleLambdas

Drawbacks

Small message size3-5 retry's then dropmessage

Full managed messaging and Pub/Subservice

Page 26: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Notification ServiceCheck Level

const AWS = require('aws-sdk');const sns = new AWS.SNS();

const publish = (msg, topicArn, cb) => { sns.publish({ Message: JSON.stringify({ message: msg }), TopicArn: topicArn }, cb);};

module.exports.checkLevel = (event, context, cb) => { if(event.Level < 2.5) { const msg = 'Moisture level has dropped to ' + event.Level;

const topicArn = process.env.mositureNotifyTopic;

publish(msg, topicArn, cb); cb(null, { message: msg, event: event }); return; }

cb(null, { message: 'No message to publish', event: event });}

Page 27: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Notifications ServiceSlack Notifier

const BbPromise = require('bluebird');const rp = require('request-promise');const util = require('util');

const notify = (msg) => { return rp({ method: 'POST', uri: process.env.slackWebHookUrl, json: true, body: { text: msg, }, });}

module.exports.notify = (event, context, cb) => { console.log(util.inspect(event, false, 5));

const promises = [];

event.Records.forEach(function(record) { if(record.EventSource !== 'aws:sns') { console.warn('Recieved non sns event: ', record); return; }

const notification = JSON.parse(record.Sns.Message);

Page 28: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

DemoSlack Notifications

Page 29: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Web Services

Web Client

React SPAFirebase HostingAuth0 for authentication

Web Backend

GraphQL APIAPI Gateway + LambdaData in DynamoDBCustom authoriser

Page 30: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Web ServicesAPI Gateway

What is it?

HTTP Endpoint as a ServiceIntegrates with LambdaConvert HTTP Request to EventCan delegate Authorization

Page 31: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Web ServicesAuth0 Authentication

Page 32: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Web ServicesAuthentication with GraphQL

const networkInterface = createNetworkInterface(GRAPHQL_URL);

networkInterface.use([{

applyMiddleware(req, next) { if (!req.options.headers) { req.options.headers = {}; // Create the header object if needed. }

// get the authentication token from local storage if it exists const idToken = localStorage.getItem('idToken') || null; if (idToken) { req.options.headers.Authorization = `Bearer ${idToken}`; } next(); },}]);

Page 33: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Web ServicesCustom Authorizer

const utils = require('./auth/utils');const auth0 = require('./auth/auth0');const AuthenticationClient = require('auth0').AuthenticationClient;

const authClient = new AuthenticationClient({ domain: process.env.AUTH0_DOMAIN, clientId: process.env.AUTH0_CLIENT_ID,});

module.exports.handler = (event, context, cb) => { console.log('Received event', event);

const token = utils.getToken(event.authorizationToken);

if (!token) { return cb('Missing token from event'); }

const authInfo = utils.getAuthInfo(event.methodArn);

return authClient.tokens.getInfo(token) .then((userInfo) => { if (!userInfo || !userInfo.user_id) { throw new Error('No user_id returned from Auth0'); }

Page 34: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

DemoDashboard

Page 35: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

What is GraphQL?

type Project { name: String stars: Int contributors: [User]}

{ project(name: "GraphQL") { stars }}

{ "project": { "stars": 4462 }}

Schema ResultsQuery

Page 36: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Why GraphQL?

One endpoint (per service) to access your data

The client chooses the response format

No versioning *

Page 37: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

import gql from 'graphql-tag';import { connect } from 'react-apollo';import MoistureChart from '../../pres/Moisture/Chart';

export default connect({ mapQueriesToProps({ ownProps, state }) { return { moisture: { query: gql`{ moisture(hours: ${ownProps.hours}, clientId: "${ownProps.clientId}") { date, moisture } }`, variables: {}, pollInterval: 1000 * 30, // 30 seconds }, }; },})(MoistureChart);

GraphQL Query

Page 38: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

GraphQL Schemaconst graphql = require('graphql');const tablesFactory = require('./dynamodb/tables');const MoistureService = require('./services/moisture');

const tables = tablesFactory();const moistureService = MoistureService({ moistureTable: tables.Moisture });

const MoistureType = new graphql.GraphQLObjectType({ name: 'MoistureType', fields: { date: { type: graphql.GraphQLString }, moisture: { type: graphql.GraphQLFloat }, }});

const schema = new graphql.GraphQLSchema({ query: new graphql.GraphQLObjectType({ name: 'Root', description: 'Root of the Schema', fields: { moisture: name: 'MoistureQuery', description: 'Retrieve moisture levels', type: new graphql.GraphQLList(MoistureType), args: { clientId: { type: graphql.GraphQLString, }, hours: {

Page 39: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

AWS Lambdaconst graphql = require('graphql');

const schema = require('./schema');

module.exports.handler = function(event, context, cb) { console.log('Received event', event);

const query = event.body.query;

return graphql.query(schema, event.body.query) .then((response) => { cb(null, response) }) .catch((error) => { cb(error) });}

Page 40: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

DemoGraphQL Query

Page 41: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

GraphQL on AWS LambdaSingle Lambda Design

Page 42: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

GraphQL on AWS LambdaLambda Tree Design

Page 43: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Summary

Page 44: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Serverless + IoTMy Experiences

No server operationsCost - $0Use *aaS servicesFocus on developing functionalityIterate quickly & scale

Page 45: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Alternative OptionsIoT Service

Device Shadows

Stores Device StateGet current stateTrack state

Page 46: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Alternative OptionsNotifications Service

Monolithic Notification LambdaOther notification services

Facebook MessengerSms - Twillio, Nexmo

Page 47: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Alternative OptionsWeb Services

Front-end Framework

AngularVue

Elastic Search instead of DynamoDB Web Service own Data Store

Page 48: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

What did I learn?

Know your services well

Know what services exist

Selecting Boundaries is hard

Automation is always worth it

GraphQL is awesome

Many things

Page 49: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Resources

Code + Readinggithub.com/garden-aidserverless.zone

Frameworks & Toolsserverless.comAWSFirebaseAuth0

Page 50: Serverless Gardens · A Serverless Architecture is an event driven system that utilises and other fully managed services for logic and persistence. FaaS . Why choose Serverless? Benefits

Thanks for Listening!Questions?

brisbanejs.acloud.guru

twitter.com/@johncmckim

johncmckim.me

medium.com/@johncmckim