/
lambda.go
191 lines (160 loc) · 5.53 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package aws
import (
"encoding/json"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)
type InvocationTypeOption string
const (
InvocationTypeRequestResponse InvocationTypeOption = "RequestResponse"
InvocationTypeDryRun = "DryRun"
)
func (itype *InvocationTypeOption) Value() (string, error) {
if itype != nil {
switch *itype {
case
InvocationTypeRequestResponse,
InvocationTypeDryRun:
return string(*itype), nil
default:
msg := fmt.Sprintf("LambdaOptions.InvocationType, if specified, must either be \"%s\" or \"%s\"",
InvocationTypeRequestResponse,
InvocationTypeDryRun)
return "", errors.New(msg)
}
}
return string(InvocationTypeRequestResponse), nil
}
// LambdaOptions contains additional parameters for InvokeFunctionWithParams().
// It contains a subset of the fields found in the lambda.InvokeInput struct.
type LambdaOptions struct {
// InvocationType can be one of InvocationTypeOption values:
// * InvocationTypeRequestResponse (default) - Invoke the function
// synchronously. Keep the connection open until the function
// returns a response or times out.
// * InvocationTypeDryRun - Validate parameter values and verify
// that the user or role has permission to invoke the function.
InvocationType *InvocationTypeOption
// Lambda function input; will be converted to JSON.
Payload interface{}
}
// LambdaOutput contains the output from InvokeFunctionWithParams(). The
// fields may or may not have a value depending on the invocation type and
// whether an error occurred or not.
type LambdaOutput struct {
// The response from the function, or an error object.
Payload []byte
// The HTTP status code for a successful request is in the 200 range.
// For RequestResponse invocation type, the status code is 200.
// For the DryRun invocation type, the status code is 204.
StatusCode *int64
}
// InvokeFunction invokes a lambda function.
func InvokeFunction(t testing.TestingT, region, functionName string, payload interface{}) []byte {
out, err := InvokeFunctionE(t, region, functionName, payload)
require.NoError(t, err)
return out
}
// InvokeFunctionE invokes a lambda function.
func InvokeFunctionE(t testing.TestingT, region, functionName string, payload interface{}) ([]byte, error) {
lambdaClient, err := NewLambdaClientE(t, region)
if err != nil {
return nil, err
}
invokeInput := &lambda.InvokeInput{
FunctionName: &functionName,
}
if payload != nil {
payloadJson, err := json.Marshal(payload)
if err != nil {
return nil, err
}
invokeInput.Payload = payloadJson
}
out, err := lambdaClient.Invoke(invokeInput)
require.NoError(t, err)
if err != nil {
return nil, err
}
if out.FunctionError != nil {
return out.Payload, &FunctionError{Message: *out.FunctionError, StatusCode: *out.StatusCode, Payload: out.Payload}
}
return out.Payload, nil
}
// InvokeFunctionWithParams invokes a lambda function using parameters
// supplied in the LambdaOptions struct and returns values in a LambdaOutput
// struct. Checks for failure using "require".
func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, input *LambdaOptions) *LambdaOutput {
out, err := InvokeFunctionWithParamsE(t, region, functionName, input)
require.NoError(t, err)
return out
}
// InvokeFunctionWithParamsE invokes a lambda function using parameters
// supplied in the LambdaOptions struct. Returns the status code and payload
// in a LambdaOutput struct and the error. A non-nil error will either reflect
// a problem with the parameters supplied to this function or an error returned
// by the Lambda.
func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string, input *LambdaOptions) (*LambdaOutput, error) {
lambdaClient, err := NewLambdaClientE(t, region)
if err != nil {
return nil, err
}
// Verify the InvocationType is one of the allowed values and report
// an error if it's not. By default the InvocationType will be
// "RequestResponse".
invocationType, err := input.InvocationType.Value()
if err != nil {
return nil, err
}
invokeInput := &lambda.InvokeInput{
FunctionName: &functionName,
InvocationType: &invocationType,
}
if input.Payload != nil {
payloadJson, err := json.Marshal(input.Payload)
if err != nil {
return nil, err
}
invokeInput.Payload = payloadJson
}
out, err := lambdaClient.Invoke(invokeInput)
if err != nil {
return nil, err
}
// As this function supports different invocation types, it must
// then support different combinations of output other than just
// payload.
lambdaOutput := LambdaOutput{
Payload: out.Payload,
StatusCode: out.StatusCode,
}
if out.FunctionError != nil {
return &lambdaOutput, errors.New(*out.FunctionError)
}
return &lambdaOutput, nil
}
type FunctionError struct {
Message string
StatusCode int64
Payload []byte
}
func (err *FunctionError) Error() string {
return fmt.Sprintf("%q error with status code %d invoking lambda function: %q", err.Message, err.StatusCode, err.Payload)
}
// NewLambdaClient creates a new Lambda client.
func NewLambdaClient(t testing.TestingT, region string) *lambda.Lambda {
client, err := NewLambdaClientE(t, region)
require.NoError(t, err)
return client
}
// NewLambdaClientE creates a new Lambda client.
func NewLambdaClientE(t testing.TestingT, region string) (*lambda.Lambda, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return lambda.New(sess), nil
}