1
1
package localstack_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
6
+ "encoding/json"
5
7
"fmt"
6
8
"io"
9
+ "net/http"
7
10
"path/filepath"
11
+ "strings"
8
12
"time"
9
13
10
14
"github.com/testcontainers/testcontainers-go"
@@ -162,8 +166,8 @@ func ExampleRunContainer_usingLambdas() {
162
166
163
167
// the three commands below are doing the following:
164
168
// 1. create a lambda function
165
- // 2. wait for the lambda function to be active
166
- // 3. invoke the lambda function with a payload, writing the result to the output.txt file
169
+ // 2. create the URL function configuration for the lambda function
170
+ // 3. wait for the lambda function to be active
167
171
lambdaCommands := [][]string {
168
172
{
169
173
"awslocal" , "lambda" ,
@@ -174,28 +178,75 @@ func ExampleRunContainer_usingLambdas() {
174
178
"--handler" , "index.handler" ,
175
179
"--role" , "arn:aws:iam::000000000000:role/lambda-role" ,
176
180
},
181
+ {"awslocal" , "lambda" , "create-function-url-config" , "--function-name" , lambdaName , "--auth-type" , "NONE" },
177
182
{"awslocal" , "lambda" , "wait" , "function-active-v2" , "--function-name" , lambdaName },
178
- {"awslocal" , "lambda" , "invoke" , "--function-name" , lambdaName , "--payload" , `{"body": "{\"num1\": \"10\", \"num2\": \"10\"}" }` , "output.txt" },
179
183
}
180
184
for _ , cmd := range lambdaCommands {
181
- _ , _ , err = container .Exec (ctx , cmd )
185
+ _ , _ , err : = container .Exec (ctx , cmd )
182
186
if err != nil {
183
187
panic (err )
184
188
}
185
189
}
186
190
187
- // the output.txt file lives in the WORKDIR of the localstack container
188
- _ , reader , err := container .Exec (ctx , []string {"cat" , "output.txt" }, exec .Multiplexed ())
191
+ // 4. get the URL for the lambda function
192
+ cmd := []string {
193
+ "awslocal" , "lambda" , "list-function-url-configs" , "--function-name" , lambdaName ,
194
+ }
195
+ _ , reader , err := container .Exec (ctx , cmd , exec .Multiplexed ())
189
196
if err != nil {
190
197
panic (err )
191
198
}
192
199
193
- content , err := io .ReadAll (reader )
200
+ buf := new (bytes.Buffer )
201
+ _ , err = buf .ReadFrom (reader )
194
202
if err != nil {
195
203
panic (err )
196
204
}
197
- fmt .Println (string (content ))
205
+
206
+ content := buf .Bytes ()
207
+
208
+ type FunctionURLConfig struct {
209
+ FunctionURLConfigs []struct {
210
+ FunctionURL string `json:"FunctionUrl"`
211
+ FunctionArn string `json:"FunctionArn"`
212
+ CreationTime string `json:"CreationTime"`
213
+ LastModifiedTime string `json:"LastModifiedTime"`
214
+ AuthType string `json:"AuthType"`
215
+ } `json:"FunctionUrlConfigs"`
216
+ }
217
+
218
+ v := & FunctionURLConfig {}
219
+ err = json .Unmarshal (content , v )
220
+ if err != nil {
221
+ panic (err )
222
+ }
223
+
224
+ httpClient := http.Client {
225
+ Timeout : 5 * time .Second ,
226
+ }
227
+
228
+ functionURL := v .FunctionURLConfigs [0 ].FunctionURL
229
+ // replace the port with the one exposed by the container
230
+
231
+ mappedPort , err := container .MappedPort (ctx , "4566/tcp" )
232
+ if err != nil {
233
+ panic (err )
234
+ }
235
+
236
+ functionURL = strings .ReplaceAll (functionURL , "4566" , mappedPort .Port ())
237
+
238
+ resp , err := httpClient .Post (functionURL , "application/json" , bytes .NewBufferString (`{"num1": "10", "num2": "10"}` ))
239
+ if err != nil {
240
+ panic (err )
241
+ }
242
+
243
+ jsonResponse , err := io .ReadAll (resp .Body )
244
+ if err != nil {
245
+ panic (err )
246
+ }
247
+
248
+ fmt .Println (string (jsonResponse ))
198
249
199
250
// Output:
200
- // {"statusCode":200,"body":" The product of 10 and 10 is 100"}
251
+ // The product of 10 and 10 is 100
201
252
}
0 commit comments