Skip to content

Commit 2397b00

Browse files
committed
add doc readme
1 parent f5d6888 commit 2397b00

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

readme.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,49 @@ try await runtime.run()
294294

295295
You can learn how to deploy and invoke this function in [the background tasks example README file](Examples/BackgroundTasks/README.md).
296296

297+
## Testing Locally
298+
299+
Before deploying your code to AWS Lambda, you can test it locally by running the executable target on your local machine. It will look like this on CLI:
300+
301+
```sh
302+
swift run
303+
```
304+
305+
When not running inside a Lambda execution environment, it starts a local HTTP server listening on port 7000. You can invoke your local Lambda function by sending an HTTP POST request to `http://127.0.0.1:7000/invoke`.
306+
307+
The request must include the JSON payload expected as an `event` by your function. You can create a text file with the JSON payload documented by AWS or captured from a trace. In this example, we used [the APIGatewayv2 JSON payload from the documentation](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-example-event), saved as `events/create-session.json` text file.
308+
309+
Then we use curl to invoke the local endpoint with the test JSON payload.
310+
311+
```sh
312+
curl -v --header "Content-Type:\ application/json" --data @events/create-session.json http://127.0.0.1:7000/invoke
313+
* Trying 127.0.0.1:7000...
314+
* Connected to 127.0.0.1 (127.0.0.1) port 7000
315+
> POST /invoke HTTP/1.1
316+
> Host: 127.0.0.1:7000
317+
> User-Agent: curl/8.4.0
318+
> Accept: */*
319+
> Content-Type:\ application/json
320+
> Content-Length: 1160
321+
>
322+
< HTTP/1.1 200 OK
323+
< content-length: 247
324+
<
325+
* Connection #0 to host 127.0.0.1 left intact
326+
{"statusCode":200,"isBase64Encoded":false,"body":"...","headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application\/json; charset=utf-8","Access-Control-Allow-Headers":"*"}}
327+
```
328+
### Modifying the local endpoint
329+
330+
By default, when using the local Lambda server, it listens on the `/invoke` endpoint.
331+
332+
Some testing tools, such as the [AWS Lambda runtime interface emulator](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html), require a different endpoint. In that case, you can use the `LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT` environment variable to force the runtime to listen on a different endpoint.
333+
334+
Example:
335+
336+
```sh
337+
LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT=/2015-03-31/functions/function/invocations swift run
338+
```
339+
297340
## Deploying your Swift Lambda functions
298341

299342

0 commit comments

Comments
 (0)