66
77 "github.com/gruntwork-io/terratest/modules/aws"
88 "github.com/gruntwork-io/terratest/modules/random"
9+ "github.com/gruntwork-io/terratest/modules/shell"
910 "github.com/gruntwork-io/terratest/modules/terraform"
1011 test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
1112 "github.com/stretchr/testify/assert"
@@ -20,6 +21,9 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
2021 // against the same terraform module.
2122 exampleFolder := test_structure .CopyTerraformFolderToTemp (t , "../" , "examples/terraform-aws-lambda-example" )
2223
24+ err := buildLambdaBinary (t , exampleFolder )
25+ require .NoError (t , err )
26+
2327 // Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
2428 // in your AWS account
2529 functionName := fmt .Sprintf ("terratest-aws-lambda-example-%s" , random .UniqueId ())
@@ -53,14 +57,34 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
5357 assert .Equal (t , `"hi!"` , string (response ))
5458
5559 // Invoke the function, this time causing it to error and capturing the error
56- _ , err : = aws .InvokeFunctionE (t , awsRegion , functionName , ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" })
60+ _ , err = aws .InvokeFunctionE (t , awsRegion , functionName , ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" })
5761
5862 // Function-specific errors have their own special return
5963 functionError , ok := err .(* aws.FunctionError )
6064 require .True (t , ok )
6165
6266 // Make sure the function-specific error comes back
63- assert .Contains (t , string (functionError .Payload ), "Failed to handle" )
67+ assert .Contains (t , string (functionError .Payload ), "failed to handle" )
68+ }
69+
70+ func buildLambdaBinary (t * testing.T , tempDir string ) error {
71+ cmd := shell.Command {
72+ Command : "go" ,
73+ Args : []string {
74+ "build" ,
75+ "-o" ,
76+ tempDir + "/src/bootstrap" ,
77+ tempDir + "/src/bootstrap.go" ,
78+ },
79+ Env : map [string ]string {
80+ "GOOS" : "linux" ,
81+ "GOARCH" : "amd64" ,
82+ "CGO_ENABLED" : "0" ,
83+ },
84+ }
85+
86+ _ , err := shell .RunCommandAndGetOutputE (t , cmd )
87+ return err
6488}
6589
6690// Another example of how to test the Terraform module in
@@ -73,6 +97,9 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
7397 // against the same terraform module.
7498 exampleFolder := test_structure .CopyTerraformFolderToTemp (t , "../" , "examples/terraform-aws-lambda-example" )
7599
100+ err := buildLambdaBinary (t , exampleFolder )
101+ require .NoError (t , err )
102+
76103 // Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
77104 // in your AWS account
78105 functionName := fmt .Sprintf ("terratest-aws-lambda-withparams-example-%s" , random .UniqueId ())
@@ -118,13 +145,13 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
118145 InvocationType : & invocationType ,
119146 Payload : ExampleFunctionPayload {ShouldFail : true , Echo : "hi!" },
120147 }
121- out , err : = aws .InvokeFunctionWithParamsE (t , awsRegion , functionName , input )
148+ out , err = aws .InvokeFunctionWithParamsE (t , awsRegion , functionName , input )
122149
123150 // The Lambda executed, but should have failed.
124151 assert .Error (t , err , "Unhandled" )
125152
126153 // Make sure the function-specific error comes back
127- assert .Contains (t , string (out .Payload ), "Failed to handle" )
154+ assert .Contains (t , string (out .Payload ), "failed to handle" )
128155
129156 // Call InvokeFunctionWithParamsE with a LambdaOptions struct that has
130157 // an unsupported InvocationType. The function should fail.
0 commit comments