Skip to content

Commit

Permalink
Merge pull request gruntwork-io#1470 from wakeful/bump-aws-lambda-pkg…
Browse files Browse the repository at this point in the history
…-rm-bin-from-dvcs

fix gruntwork-io#1363 build lambda bin from src instead using a blob + bump ext pkg.
  • Loading branch information
yhakbar authored Nov 19, 2024
2 parents 33c3c13 + 0a3656a commit ce008dc
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
Binary file removed examples/terraform-aws-lambda-example/src/bootstrap
Binary file not shown.
4 changes: 2 additions & 2 deletions examples/terraform-aws-lambda-example/src/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type Event struct {
Echo string `json:"Echo"`
}

// Fails if ShouldFail is `true`, otherwise echos the input.
// HandleRequest Fails if ShouldFail is `true`, otherwise echos the input.
func HandleRequest(ctx context.Context, evnt *Event) (string, error) {
if evnt == nil {
return "", fmt.Errorf("received nil event")
}
if evnt.ShouldFail {
return "", fmt.Errorf("Failed to handle %#v", evnt)
return "", fmt.Errorf("failed to handle %#v", evnt)
}
return evnt.Echo, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/Azure/go-autorest/autorest/azure/auth v0.5.8
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/aws/aws-lambda-go v1.13.3
github.com/aws/aws-lambda-go v1.47.0
github.com/aws/aws-sdk-go v1.44.122
github.com/ghodss/yaml v1.0.0
github.com/go-errors/errors v1.0.2-0.20180813162953-d98b870cc4e0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aws/aws-lambda-go v1.13.3 h1:SuCy7H3NLyp+1Mrfp+m80jcbi9KYWAs9/BXwppwRDzY=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
github.com/aws/aws-sdk-go v1.44.122 h1:p6mw01WBaNpbdP2xrisz5tIkcNwzj/HysobNoaAHjgo=
github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
Expand Down Expand Up @@ -649,7 +649,6 @@ github.com/tmccombs/hcl2json v0.6.4 h1:/FWnzS9JCuyZ4MNwrG4vMrFrzRgsWEOVi+1AyYUVL
github.com/tmccombs/hcl2json v0.6.4/go.mod h1:+ppKlIW3H5nsAsZddXPy2iMyvld3SHxyjswOZhavRDk=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.12/go.mod h1:sSBEIC79qR6OvcmsD4U3KABeOTxDqQtdDnaFuUN30b8=
github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ=
Expand Down
35 changes: 31 additions & 4 deletions test/terraform_aws_lambda_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/shell"
"github.com/gruntwork-io/terratest/modules/terraform"
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
Expand All @@ -20,6 +21,9 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
// against the same terraform module.
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-aws-lambda-example")

err := buildLambdaBinary(t, exampleFolder)
require.NoError(t, err)

// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
// in your AWS account
functionName := fmt.Sprintf("terratest-aws-lambda-example-%s", random.UniqueId())
Expand Down Expand Up @@ -53,14 +57,34 @@ func TestTerraformAwsLambdaExample(t *testing.T) {
assert.Equal(t, `"hi!"`, string(response))

// Invoke the function, this time causing it to error and capturing the error
_, err := aws.InvokeFunctionE(t, awsRegion, functionName, ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"})
_, err = aws.InvokeFunctionE(t, awsRegion, functionName, ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"})

// Function-specific errors have their own special return
functionError, ok := err.(*aws.FunctionError)
require.True(t, ok)

// Make sure the function-specific error comes back
assert.Contains(t, string(functionError.Payload), "Failed to handle")
assert.Contains(t, string(functionError.Payload), "failed to handle")
}

func buildLambdaBinary(t *testing.T, tempDir string) error {
cmd := shell.Command{
Command: "go",
Args: []string{
"build",
"-o",
tempDir + "/src/bootstrap",
tempDir + "/src/bootstrap.go",
},
Env: map[string]string{
"GOOS": "linux",
"GOARCH": "amd64",
"CGO_ENABLED": "0",
},
}

_, err := shell.RunCommandAndGetOutputE(t, cmd)
return err
}

// Another example of how to test the Terraform module in
Expand All @@ -73,6 +97,9 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
// against the same terraform module.
exampleFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-aws-lambda-example")

err := buildLambdaBinary(t, exampleFolder)
require.NoError(t, err)

// Give this lambda function a unique ID for a name so we can distinguish it from any other lambdas
// in your AWS account
functionName := fmt.Sprintf("terratest-aws-lambda-withparams-example-%s", random.UniqueId())
Expand Down Expand Up @@ -118,13 +145,13 @@ func TestTerraformAwsLambdaWithParamsExample(t *testing.T) {
InvocationType: &invocationType,
Payload: ExampleFunctionPayload{ShouldFail: true, Echo: "hi!"},
}
out, err := aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)
out, err = aws.InvokeFunctionWithParamsE(t, awsRegion, functionName, input)

// The Lambda executed, but should have failed.
assert.Error(t, err, "Unhandled")

// Make sure the function-specific error comes back
assert.Contains(t, string(out.Payload), "Failed to handle")
assert.Contains(t, string(out.Payload), "failed to handle")

// Call InvokeFunctionWithParamsE with a LambdaOptions struct that has
// an unsupported InvocationType. The function should fail.
Expand Down

0 comments on commit ce008dc

Please sign in to comment.