Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add an example of using localstack alongside AWS lambdas #1790

Merged
merged 7 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"

"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/internal/testcontainerssession"
"github.com/testcontainers/testcontainers-go/wait"
)

Expand Down Expand Up @@ -204,3 +206,8 @@ type GenericProvider interface {
NetworkProvider
ImageProvider
}

// GenericLabels returns a map of labels that can be used to identify containers created by this library
func GenericLabels() map[string]string {
return testcontainersdocker.DefaultLabels(testcontainerssession.SessionID())
}
83 changes: 83 additions & 0 deletions modules/localstack/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import (
"context"
"fmt"
"io"
"path/filepath"
"time"

"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/exec"
"github.com/testcontainers/testcontainers-go/modules/localstack"
"github.com/testcontainers/testcontainers-go/wait"
)
Expand Down Expand Up @@ -113,3 +116,83 @@
// Output:
// version=localstack/localstack:0.10.0. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0
}

func ExampleRunContainer_UsingLambdas() {

Check failure on line 120 in modules/localstack/examples_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, localstack) / modules/localstack/ubuntu-latest/1.20.x

tests: ExampleRunContainer_UsingLambdas refers to unknown field or method: RunContainer.UsingLambdas (govet)
ctx := context.Background()

flagsFn := func() string {
labels := testcontainers.GenericLabels()

flags := ""
for k, v := range labels {
flags = fmt.Sprintf("%s -l %s=%s", flags, k, v)
}

return flags
}
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

lambdaName := "localstack-lambda-url-example"

container, err := localstack.RunContainer(ctx,
testcontainers.WithImage("localstack/localstack:2.3.0"),
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Env: map[string]string{
"SERVICES": "lambda",
"LAMBDA_EXECUTOR": "docker",
"PROVIDER_OVERRIDE_LAMBDA": "asf",
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved
"LAMBDA_DOCKER_FLAGS": flagsFn(),
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: filepath.Join("testdata", "function.zip"),
ContainerFilePath: "/tmp/function.zip",
},
},
},
}),
)
if err != nil {
panic(err)
}
defer container.Terminate(ctx)

Check failure on line 158 in modules/localstack/examples_test.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, localstack) / modules/localstack/ubuntu-latest/1.20.x

Error return value of `container.Terminate` is not checked (errcheck)

// the three commands below are doing the following:
// 1. create a lambda function
// 2. wait for the lambda function to be active
// 3. invoke the lambda function with a payload, writing the result to the output.txt file
lambdaCommands := [][]string{
{
"awslocal", "lambda",
"create-function", "--function-name", lambdaName,
"--runtime", "nodejs18.x",
"--zip-file",
"fileb:///tmp/function.zip",
"--handler", "index.handler",
"--role", "arn:aws:iam::000000000000:role/lambda-role",
},
{"awslocal", "lambda", "wait", "function-active-v2", "--function-name", lambdaName},
{"awslocal", "lambda", "invoke", "--function-name", lambdaName, "--payload", `{"body": "{\"num1\": \"10\", \"num2\": \"10\"}" }`, "output.txt"},
}
for _, cmd := range lambdaCommands {
_, _, err = container.Exec(ctx, cmd)
if err != nil {
panic(err)
}
}

// the output.txt file lives in the WORKDIR of the localstack container
_, reader, err := container.Exec(ctx, []string{"cat", "output.txt"}, exec.Multiplexed())
if err != nil {
panic(err)
}

content, err := io.ReadAll(reader)
if err != nil {
panic(err)
}
fmt.Println(string(content))
mdelapenya marked this conversation as resolved.
Show resolved Hide resolved

// Output:
// {"statusCode":200,"body":"The product of 10 and 10 is 100"}
}
2 changes: 1 addition & 1 deletion modules/localstack/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.18.44
github.com/aws/aws-sdk-go-v2/credentials v1.13.42
github.com/aws/aws-sdk-go-v2/service/s3 v1.40.1
github.com/docker/docker v24.0.6+incompatible
github.com/docker/go-connections v0.4.0
github.com/stretchr/testify v1.8.4
github.com/testcontainers/testcontainers-go v0.25.0
Expand Down Expand Up @@ -38,7 +39,6 @@ require (
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down
5 changes: 4 additions & 1 deletion modules/localstack/localstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"fmt"
"strings"
"time"

Check failure on line 8 in modules/localstack/localstack.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, localstack) / modules/localstack/ubuntu-latest/1.20.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)
"golang.org/x/mod/semver"

"github.com/docker/docker/api/types/container"

Check failure on line 11 in modules/localstack/localstack.go

View workflow job for this annotation

GitHub Actions / test-modules (1.20.x, ubuntu-latest, localstack) / modules/localstack/ubuntu-latest/1.20.x

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/testcontainers) (gci)
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/internal/testcontainersdocker"
"github.com/testcontainers/testcontainers-go/wait"
Expand Down Expand Up @@ -92,10 +93,12 @@

req := testcontainers.ContainerRequest{
Image: fmt.Sprintf("localstack/localstack:%s", defaultVersion),
Binds: []string{fmt.Sprintf("%s:/var/run/docker.sock", dockerHost)},
WaitingFor: wait.ForHTTP("/_localstack/health").WithPort("4566/tcp").WithStartupTimeout(120 * time.Second),
ExposedPorts: []string{fmt.Sprintf("%d/tcp", defaultPort)},
Env: map[string]string{},
HostConfigModifier: func(hostConfig *container.HostConfig) {
hostConfig.Binds = []string{fmt.Sprintf("%s:/var/run/docker.sock", dockerHost)}
},
}

localStackReq := LocalStackContainerRequest{
Expand Down
Binary file added modules/localstack/testdata/function.zip
Binary file not shown.
9 changes: 9 additions & 0 deletions modules/localstack/testdata/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.handler = async (event) => {
let body = JSON.parse(event.body)
const product = body.num1 * body.num2;
const response = {
statusCode: 200,
body: "The product of " + body.num1 + " and " + body.num2 + " is " + product,
};
return response;
};
Loading