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

serverless - send email and update status

Anna Su
March 22, 2019

serverless - send email and update status

2019/3/22
@ branch8 公司內部技術分享

Anna Su

March 22, 2019
Tweet

More Decks by Anna Su

Other Decks in Technology

Transcript

  1. Why not VM 即使 server 沒有收到任何 request ,還是會被收取費⽤用 必須設定與持續管理理 Server

    需要經常更更新 Server,避免安全性問題 當我們使⽤用量量變⼤大時,需要管理理 server 的 scaling up
 如果使⽤用量量變低時,也需要 scaling down
  2. 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.
  3. 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
  4. 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
  5. sls

  6. module.exports.hello = async (event) => { return { statusCode: 200,

    body: JSON.stringify({ message: 'Go Serverless v1.0! Your function executed successfully!', input: event, }), }; }; handler.js
  7. service: aws-nodejs-demo
 provider: name: aws runtime: nodejs8.10
 functions: hello: handler:

    handler.hello events: - http: path: / method: get serverless.yml
  8. Email Flow HASURA Realtime Serverless Send email HASURA Event SendGrid

    Event Notification SendGrid Serverless Update email
  9. 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);
  10. Update Email Status Flow HASURA Realtime Serverless Send email HASURA

    Event SendGrid Event Notification SendGrid Serverless Update email
  11. 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: {
  12. 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
  13. 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