Skip to content

Commit

Permalink
Remove FunctionError field from LambdaOutput
Browse files Browse the repository at this point in the history
Restore original InvokeFunctionE().
  • Loading branch information
kbalk committed Apr 1, 2021
1 parent c10bd38 commit a2e7ae2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
55 changes: 39 additions & 16 deletions modules/aws/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ type LambdaOptions struct {
// fields may or may not have a value depending on the invocation type and
// whether an error occurred or not.
type LambdaOutput struct {
// If present, indicates that an error occurred during function execution.
// Error details are included in the response payload.
FunctionError *string

// The response from the function, or an error object.
Payload []byte

Expand All @@ -75,8 +71,26 @@ func InvokeFunction(t testing.TestingT, region, functionName string, payload int

// InvokeFunctionE invokes a lambda function.
func InvokeFunctionE(t testing.TestingT, region, functionName string, payload interface{}) ([]byte, error) {
input := &LambdaOptions{Payload: &payload}
out, err := InvokeFunctionWithParamsE(t, region, functionName, input)
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
}
Expand All @@ -98,8 +112,10 @@ func InvokeFunctionWithParams(t testing.TestingT, region, functionName string, i
}

// InvokeFunctionWithParamsE invokes a lambda function using parameters
// supplied in the LambdaOptions struct and returns values in a LambdaOutput
// struct and the error.
// 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 {
Expand All @@ -111,8 +127,7 @@ func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string,
// "RequestResponse".
invocationType, err := input.InvocationType.Value()
if err != nil {
msg := err.Error()
return &LambdaOutput{FunctionError: &msg}, err
return nil, err
}

invokeInput := &lambda.InvokeInput{
Expand All @@ -129,15 +144,23 @@ func InvokeFunctionWithParamsE(t testing.TestingT, region, functionName string,
}

out, err := lambdaClient.Invoke(invokeInput)
if err != nil {
return nil, err
}

// As this function supports different invocation types, so it must
// support different combinations of output.
// As this function supports different invocation types, it must
// then support different combinations of output other than just
// payload.
lambdaOutput := LambdaOutput{
FunctionError: out.FunctionError,
Payload: out.Payload,
StatusCode: out.StatusCode,
Payload: out.Payload,
StatusCode: out.StatusCode,
}
return &lambdaOutput, err

if out.FunctionError != nil {
return &lambdaOutput, errors.New(*out.FunctionError)
}

return &lambdaOutput, nil
}

type FunctionError struct {
Expand Down
9 changes: 4 additions & 5 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
}
out, err := aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)

// No error in the invocation as Lambda was found and executed.
require.Nil(t, err)
// The Lambda executed, but should have failed.
require.NotNil(t, err)
assert.Contains(t, err.Error(), "Unhandled")
assert.Equal(t, int(*out.StatusCode), 200)

// Make sure the function-specific error comes back
Expand All @@ -135,9 +136,7 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
}
out, err = aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)
require.NotNil(t, err)
msg := "LambdaOptions.InvocationType, if specified, must either be \"RequestResponse\" or \"DryRun\""
assert.Contains(t, err.Error(), msg)
assert.Contains(t, *out.FunctionError, msg)
assert.Contains(t, err.Error(), "LambdaOptions.InvocationType, if specified, must either be \"RequestResponse\" or \"DryRun\"")
}

type ExampleFunctionPayload struct {
Expand Down

0 comments on commit a2e7ae2

Please sign in to comment.