Skip to content

Commit f0cee55

Browse files
committed
chore: add an example of running localstack with Lambdas
1 parent fa51914 commit f0cee55

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

modules/localstack/examples_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package localstack_test
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"path/filepath"
68
"time"
79

810
"github.com/testcontainers/testcontainers-go"
11+
"github.com/testcontainers/testcontainers-go/exec"
912
"github.com/testcontainers/testcontainers-go/modules/localstack"
1013
"github.com/testcontainers/testcontainers-go/wait"
1114
)
@@ -113,3 +116,83 @@ func ExampleRunContainer_legacyMode() {
113116
// Output:
114117
// version=localstack/localstack:0.10.0. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0
115118
}
119+
120+
func ExampleRunContainer_UsingLambdas() {
121+
ctx := context.Background()
122+
123+
flagsFn := func() string {
124+
labels := testcontainers.GenericLabels()
125+
126+
flags := ""
127+
for k, v := range labels {
128+
flags = fmt.Sprintf("%s -l %s=%s", flags, k, v)
129+
}
130+
131+
return flags
132+
}
133+
134+
lambdaName := "localstack-lambda-url-example"
135+
136+
container, err := localstack.RunContainer(ctx,
137+
testcontainers.WithImage("localstack/localstack:2.3.0"),
138+
testcontainers.CustomizeRequest(testcontainers.GenericContainerRequest{
139+
ContainerRequest: testcontainers.ContainerRequest{
140+
Env: map[string]string{
141+
"SERVICES": "lambda",
142+
"LAMBDA_EXECUTOR": "docker",
143+
"PROVIDER_OVERRIDE_LAMBDA": "asf",
144+
"LAMBDA_DOCKER_FLAGS": flagsFn(),
145+
},
146+
Files: []testcontainers.ContainerFile{
147+
{
148+
HostFilePath: filepath.Join("testdata", "function.zip"),
149+
ContainerFilePath: "/tmp/function.zip",
150+
},
151+
},
152+
},
153+
}),
154+
)
155+
if err != nil {
156+
panic(err)
157+
}
158+
defer container.Terminate(ctx)
159+
160+
// the three commands below are doing the following:
161+
// 1. create a lambda function
162+
// 2. wait for the lambda function to be active
163+
// 3. invoke the lambda function with a payload, writing the result to the output.txt file
164+
lambdaCommands := [][]string{
165+
{
166+
"awslocal", "lambda",
167+
"create-function", "--function-name", lambdaName,
168+
"--runtime", "nodejs18.x",
169+
"--zip-file",
170+
"fileb:///tmp/function.zip",
171+
"--handler", "index.handler",
172+
"--role", "arn:aws:iam::000000000000:role/lambda-role",
173+
},
174+
{"awslocal", "lambda", "wait", "function-active-v2", "--function-name", lambdaName},
175+
{"awslocal", "lambda", "invoke", "--function-name", lambdaName, "--payload", `{"body": "{\"num1\": \"10\", \"num2\": \"10\"}" }`, "output.txt"},
176+
}
177+
for _, cmd := range lambdaCommands {
178+
_, _, err = container.Exec(ctx, cmd)
179+
if err != nil {
180+
panic(err)
181+
}
182+
}
183+
184+
// the output.txt file lives in the WORKDIR of the localstack container
185+
_, reader, err := container.Exec(ctx, []string{"cat", "output.txt"}, exec.Multiplexed())
186+
if err != nil {
187+
panic(err)
188+
}
189+
190+
content, err := io.ReadAll(reader)
191+
if err != nil {
192+
panic(err)
193+
}
194+
fmt.Println(string(content))
195+
196+
// Output:
197+
// {"statusCode":200,"body":"The product of 10 and 10 is 100"}
198+
}

modules/localstack/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/aws/aws-sdk-go-v2/config v1.18.44
99
github.com/aws/aws-sdk-go-v2/credentials v1.13.42
1010
github.com/aws/aws-sdk-go-v2/service/s3 v1.40.1
11+
github.com/docker/docker v24.0.6+incompatible
1112
github.com/docker/go-connections v0.4.0
1213
github.com/stretchr/testify v1.8.4
1314
github.com/testcontainers/testcontainers-go v0.25.0
@@ -38,7 +39,6 @@ require (
3839
github.com/cpuguy83/dockercfg v0.3.1 // indirect
3940
github.com/davecgh/go-spew v1.1.1 // indirect
4041
github.com/docker/distribution v2.8.2+incompatible // indirect
41-
github.com/docker/docker v24.0.6+incompatible // indirect
4242
github.com/docker/go-units v0.5.0 // indirect
4343
github.com/go-ole/go-ole v1.2.6 // indirect
4444
github.com/gogo/protobuf v1.3.2 // indirect
337 Bytes
Binary file not shown.

modules/localstack/testdata/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
exports.handler = async (event) => {
2+
let body = JSON.parse(event.body)
3+
const product = body.num1 * body.num2;
4+
const response = {
5+
statusCode: 200,
6+
body: "The product of " + body.num1 + " and " + body.num2 + " is " + product,
7+
};
8+
return response;
9+
};

0 commit comments

Comments
 (0)