forked from cloudquery/cloudquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.go
More file actions
61 lines (51 loc) · 1.47 KB
/
lambda.go
File metadata and controls
61 lines (51 loc) · 1.47 KB
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
51
52
53
54
55
56
57
58
59
60
61
package deploy
import (
"context"
"fmt"
"github.com/cloudquery/cloudquery/cloudqueryclient"
"os"
"log"
)
type Request struct {
TaskName string `json:"taskName"`
}
func LambdaHandler(ctx context.Context, req Request) (string, error) {
return TaskExecutor(req.TaskName)
}
func TaskExecutor(taskName string) (string, error) {
driver := os.Getenv("CQ_DRIVER")
dsn := os.Getenv("CQ_DSN")
switch taskName {
case "fetch":
Fetch(driver, dsn, false)
case "policy":
Policy(driver, dsn, false)
default:
return fmt.Sprintf("Unknown task: %s", taskName), fmt.Errorf("Unkown task: %s", taskName)
}
return fmt.Sprintf("Completed task %s", taskName), nil
}
// Fetches resources from a cloud provider and saves them in the configured database
func Fetch(driver, dsn string, verbose bool) {
client, err := cloudqueryclient.New(driver, dsn, verbose)
if err != nil {
log.Fatalf("Unable to initialize client: %s", err)
}
err = client.Run("config.yml")
if err != nil {
log.Fatalf("Error fetching resources: %s", err)
}
}
// Runs a policy SQL statement and returns results
func Policy(driver, dsn string, verbose bool) {
outputPath := "/tmp/result.json"
queryPath := os.Getenv("CQ_QUERY_PATH") // TODO: if path is an S3 URI, pull file down
client, err := cloudqueryclient.New(driver, dsn, verbose)
if err != nil {
log.Fatalf("Unable to initialize client: %s", err)
}
err = client.RunQuery(queryPath, outputPath)
if err != nil {
log.Fatalf("Error running query: %s", err)
}
}