-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda.go
65 lines (50 loc) · 1.52 KB
/
lambda.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
)
// AWS SDK session and Lambda service objects
var sess session.Session
var svc *lambda.Lambda
// AWS session and service objects are created during startup and
// reused across all Lambda invocations. If for any reason they turn unusable,
// there is no attempt to recreate them.
func init() {
sess, err := session.NewSession()
if err != nil {
log.WithError(err).Panic("unable to acquire AWS session")
}
svc = lambda.New(sess)
}
// Function lambdaCall - call Lambda using ApplicId of MQTM as function name
// and the entire MQTM as payload
func lambdaCall(lambdaName string, tm MQTM) error {
payload, err := json.Marshal(tm)
if err != nil {
log.WithError(err).Error("json.Marshal failed")
return err
}
log.WithFields(log.Fields{
"NAME": lambdaName,
"PAYLOAD": string(payload),
}).Debug("about to invoke Lambda")
params := &lambda.InvokeInput{
FunctionName: aws.String(lambdaName),
InvocationType: aws.String("Event"),
Payload: payload,
}
resp, err := svc.Invoke(params)
// There is no check for intermittent failures, nor is there any
// retry attempt. It is assumed that MQ will eventually retrigger the process
if err != nil {
log.WithError(err).Error("Lambda invocation failed")
return err
}
log.WithFields(log.Fields{
"RESPONSE": resp,
}).Debug("successful Lambda invocation")
return nil
}