-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (42 loc) · 1.42 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
a "./awshelpers"
t "./tablehelpers"
"context"
"github.com/aws/aws-lambda-go/lambda"
"log"
"os"
)
// Event must contain all the fields
type TriggerEvent struct {
Name string `json:"name"`
ServiceName string `json:"service_name"`
Env string `json:"env"`
Cron string `json:"cron"`
ModelNames []string `json:"model_names"`
RdsCreds string `json:"rds_creds"`
SnsTopicArn string `json:"sns_topic_arn"`
}
// Lambda handler function
// Connects to the RDS instance and the database mentioned
// Fetches the tables that are to be checked during this hour
// Checks the diff of the two
// If there is a mismatch, publish to the given SNS channel
func monitorTables(ctx context.Context, params TriggerEvent) {
log.Printf("Starting execution in %s :: %s", os.Getenv("AWS_REGION"), params.Env)
scheduledTables := t.FetchScheduledTables(params.ModelNames, params.Cron)
log.Print("Tables checked :: ", scheduledTables)
if len(scheduledTables) > 0 {
dbTables := a.FetchTablesFromDB(params.RdsCreds)
failedTables := t.SliceDifference(scheduledTables, dbTables)
log.Print("Tables in DB :: ", len(dbTables))
log.Print("Failed table creations :: ", failedTables)
if len(failedTables) > 0 {
a.PublishResultsToSns(params.Name, failedTables, params.Env, params.SnsTopicArn, params.ServiceName)
}
}
}
// Invokes the lambda handler
func main() {
lambda.Start(monitorTables)
}