-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.go
111 lines (94 loc) · 2.83 KB
/
invoke.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package lambda
import (
"github.com/aws/aws-sdk-go/aws/session"
sdk "github.com/aws/aws-sdk-go/service/lambda"
"encoding/json"
"errors"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/matthisstenius/logger"
)
// PathParameters holds path params
type PathParameters map[string]interface{}
// QueryParameters holds query string params
type QueryParameters map[string]interface{}
// AuthParameters holds auth params that
type AuthParameters map[string]interface{}
// Input data for current invocation
type Input struct {
Service string
Resource string
Body interface{}
Method string
PathParams PathParameters
QueryParams QueryParameters
AuthParameters AuthParameters
}
// Invoke Lambda function with payload based on input
func Invoke(input Input, out interface{}) error {
sess, _ := session.NewSession()
client := sdk.New(sess)
payload, err := encodePayload(input)
if err != nil {
logger.WithFields(logger.Fields{"error": err}).Error("Lambda::Invoke() Could not encode payload ")
return errors.New("could not encode payload")
}
reqInput := sdk.InvokeInput{
FunctionName: aws.String(input.Service),
Payload: payload,
}
req, output := client.InvokeRequest(&reqInput)
if err := req.Send(); err != nil {
logger.WithFields(logger.Fields{"error": err}).Error("Lambda::Invoke() Lambda client error")
return errors.New("lambda client error")
}
var response struct {
StatusCode int
Body string
}
if err := json.Unmarshal(output.Payload, &response); err != nil {
logger.WithFields(logger.Fields{
"error": err,
"payload": string(output.Payload),
}).Error("Lambda::Invoke() error during unmarshal of output payload")
return errors.New("error during unmarshal")
}
if response.StatusCode != http.StatusOK {
logger.WithFields(logger.Fields{
"error": err,
"payload": string(output.Payload),
}).Warning("Lambda::Invoke() none 200 status code")
return errors.New("none 200 status code")
}
if err := json.Unmarshal([]byte(response.Body), out); err != nil {
logger.WithFields(logger.Fields{
"error": err,
"payload": string(response.Body),
}).Error("Lambda::Invoke() error during unmarshal of response body")
return errors.New("error during unmarshal")
}
return nil
}
func encodePayload(input Input) ([]byte, error) {
method := http.MethodGet
if input.Method != "" {
method = input.Method
}
payload := map[string]interface{}{
"resource": input.Resource,
"body": input.Body,
"httpMethod": method,
}
if len(input.AuthParameters) > 0 {
payload["requestContext"] = map[string]interface{}{
"authorizer": input.AuthParameters,
}
}
if len(input.PathParams) > 0 {
payload["pathParameters"] = input.PathParams
}
if len(input.QueryParams) > 0 {
payload["queryStringParameters"] = input.QueryParams
}
return json.Marshal(payload)
}