Slide 1

Slide 1 text

Serverless Anna Su Send Email and update status

Slide 2

Slide 2 text

Why not VM 即使 server 沒有收到任何 request ,還是會被收取費⽤用 必須設定與持續管理理 Server 需要經常更更新 Server,避免安全性問題 當我們使⽤用量量變⼤大時,需要管理理 server 的 scaling up
 如果使⽤用量量變低時,也需要 scaling down

Slide 3

Slide 3 text

Why Serverless ⾃自動擴展 依照使⽤用量量付費 開發速度更更快 零管理理

Slide 4

Slide 4 text

You don’t have to think about those servers. You just focus on code.

Slide 5

Slide 5 text

What’s Serverless Serverless is an execution model where the cloud provider is responsible for executing a piece of code by dynamically allocating the resources.

Slide 6

Slide 6 text

What’s Serverless The code is typically run inside stateless containers. Can be triggered by a variety of events including http requests, database events, monitoring alerts, file uploads, scheduled events, etc

Slide 7

Slide 7 text

How Serverless Works @aws Upload your code to AWS Lambda Set up your code to trigger from other AWS services, HTTP endpoints, or in-app activity Lambda runs your code only when triggered, using only the compute resources needed Pay just for the compute time you use

Slide 8

Slide 8 text

Install Serverless npm install -g serverless

Slide 9

Slide 9 text

Deploy a Node.js function to AWS Lambda

Slide 10

Slide 10 text

sls

Slide 11

Slide 11 text

sls create -t aws-nodejs

Slide 12

Slide 12 text

brew install awscli

Slide 13

Slide 13 text

aws configure

Slide 14

Slide 14 text

module.exports.hello = async (event) => { return { statusCode: 200, body: JSON.stringify({ message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }), }; }; handler.js

Slide 15

Slide 15 text

service: aws-nodejs-demo
 provider: name: aws runtime: nodejs8.10
 functions: hello: handler: handler.hello events: - http: path: / method: get serverless.yml

Slide 16

Slide 16 text

sls deploy

Slide 17

Slide 17 text

Serverless Project for Email

Slide 18

Slide 18 text

HASURA SendGrid AWS Lambda Using

Slide 19

Slide 19 text

HASURA build GraphQL apps backed by Postgres

Slide 20

Slide 20 text

HASURA Event trigger

Slide 21

Slide 21 text

SendGrid email service trusted by developers and marketers

Slide 22

Slide 22 text

AWS Lambda run code without thinking about servers

Slide 23

Slide 23 text

Send Email HASURA event trigger webhook Serverless AWS Lambda

Slide 24

Slide 24 text

Update Email Status notification webhook Serverless AWS Lambda

Slide 25

Slide 25 text

Email Flow HASURA Realtime Serverless Send email HASURA Event SendGrid Event Notification SendGrid Serverless Update email

Slide 26

Slide 26 text

Send Email Flow HASURA Realtime Serverless Send email HASURA Event SendGrid

Slide 27

Slide 27 text

Create HASURA Table

Slide 28

Slide 28 text

Creating an event-trigger to listen to a database change on Postgres

Slide 29

Slide 29 text

Create Serverless (Send Email) const sgMail = require('@sendgrid/mail'); module.exports.run = async event => { try { let { table: { name: table_name, schema }, event: { data: { new: { to, from, subject, content: html, id }, }, }, } = JSON.parse(event.body); sgMail.setApiKey(process.env.SENDGRID_API_KEY);

Slide 30

Slide 30 text

SendGrid Settings

Slide 31

Slide 31 text

AWS Lambda Settings

Slide 32

Slide 32 text

Update Email Status Flow HASURA Realtime Serverless Send email HASURA Event SendGrid Event Notification SendGrid Serverless Update email

Slide 33

Slide 33 text

Create Serverless (Update Email) const axios = require('axios'); module.exports.run = async event => { try { const body = JSON.parse(event.body); const { id, event: status, schema, table_name } = body[body.length - 1]; await axios({ method: 'post', headers: { 'x-hasura-admin-secret': process.env.HASURA_SECRET, }, url: process.env.HASURA_URL, data: { variables: {

Slide 34

Slide 34 text

AWS Lambda Settings

Slide 35

Slide 35 text

SendGrid Settings

Slide 36

Slide 36 text

provider: functions: sendEmailFromHasura: handler: sendEmailFromHasura.run events: - http: path: send-email-from-hasura method: post updateHasuraEmailStatus: handler: updateHasuraEmailStatus.run events: - http: path: update-hasura-email-status method: post plugins: - serverless-plugin-monorepo serverless.yml

Slide 37

Slide 37 text

sls deploy

Slide 38

Slide 38 text

AWS Lambda function

Slide 39

Slide 39 text

Reference https://serverless.com/ https://aws.amazon.com/tw/lambda/ Develop a Serveerless Backend using Node.js on AWS Lambda What is Serverless ? Serverless overview What is Serverless Architecture? https://www.flaticon.com

Slide 40

Slide 40 text

Thanks Anna Su