@@ -3,9 +3,12 @@ package localstack_test
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io"
7
+ "path/filepath"
6
8
"time"
7
9
8
10
"github.com/testcontainers/testcontainers-go"
11
+ "github.com/testcontainers/testcontainers-go/exec"
9
12
"github.com/testcontainers/testcontainers-go/modules/localstack"
10
13
"github.com/testcontainers/testcontainers-go/wait"
11
14
)
@@ -113,3 +116,83 @@ func ExampleRunContainer_legacyMode() {
113
116
// Output:
114
117
// version=localstack/localstack:0.10.0. Testcontainers for Go does not support running LocalStack in legacy mode. Please use a version >= 0.11.0
115
118
}
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
+ }
0 commit comments