Learning to make use of AWS Lambda function: Cron Jobs (plus Pub-Sub pattern)

import "source-map-support/register";import config from "./app/config";import app from "./app";import cron from "node-cron";import { sendMonthlyReport } from "./app/jobs";if (config.app.env === "production") {cron.schedule("0 10 28 * *", sendMonthlyReport); // run at 28th day every month}app.listen(config.app.port, () => console.log(`App listening on port ${config.app.port}`));
// handler.ts
import "source-map-support/register";
import { sendMonthlyReport } from "./functions"
export const send_monthly_report = sendMonthlyReport
// sendMonthlyReport.ts
export const sendMonthlyReports: APIGatewayProxyHandler = async (_, context) => {
try {
// query invested users
// loop through each user
// generate pdf report
// send email to each user with attached file
return { statusCode: 200, body: JSON.stringify({ message: "Success" })
};
...
// serverless.ymlFunctions:
send_monthly_reports:
handler: handler.send_monthly_reports
events:
- schedule:
name: ${self:custom.stage}-send-monthly-reports
rate: cron(0 3 1 * ? *) # vn time 10AM 1st month
  • Query users who invested in the month & get trading data
  • Loop through each user: Generate pdf report with headless Chrome & Send email with Sendgrid
  • Query users who invested in the month & get trading data
  • Loop through each user: send necessary data as a message to a SNS topic (i.e. 1 message for 1 user)
  • Another Lambda function will subscribe to that topic then consume the message to carry out task
// sendMonthlyReport.ts
export const sendMonthlyReports: APIGatewayProxyHandler = async (_, context) => {
try {
const userIds = await getInvestedUserIds();
for (const userId of userIds) {
await publishSNS({userId}, "SendUserReportTopic")
}
...// subscriberReport.ts
export const subscriberReport: SNSHandler = async (event, context) => {
let fileName = "monthlyWealthReport";try {logger(`[SNS] Message received: ${event.Records[0].Sns.MessageId}`);const message: { userId: string } = event.Records[0].Sns.Message && JSON.parse(event.Records[0].Sns.Message); // logic to generate report, upload to S3, send email, etc....// serverless.ymlFunctions:
send_monthly_reports:
handler: handler.send_monthly_reports
events:
- schedule:
name: ${self:custom.stage}-send-monthly-reports
rate: cron(0 3 1 * ? *) # vn time 10AM 1st month
sub_send_monthly_report_by_user_id:
handler: handler.sub_send_monthly_report_by_user_id # send wealth report for user - Subscriber
events:
- sns: ${self:custom.stage}-SendEmailWealthReport

--

--

--

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating an High Availability Architecture With AWS CLI — including Webserver Configuration EBS…

Continuous Profiling for Python Applications

Pay-as-you-go Licensing for Power Apps

Re-thinking Enterprise Application Development and Delivery.

How can you develop a mobile app with APIs?

How to insert source code to medium articles

KICKSTART YOUR DAPP FRONTEND DEVELOPMENT WITH CREATE-ETH-APP

Why Kubernetes: a basic idea of Kubernetes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hung Tran Nguyen

Hung Tran Nguyen

From translator -> Manual QC Engineer -> Automation QA Engineer -> NodeJS Developer -> yearning to learn more

More from Medium

Using AWS SAM to build and deploy an application with SNS, SQS and Lambda services

AWS Machine Learning Use Case using Serverless…

AWS Weekly #14

Creating alarms using aws lambda and slack commands