Skip to content

Commit 831cf66

Browse files
committed
document alternative workflow for local testing
1 parent f0dcd6b commit 831cf66

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

README.md

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# AWS Lambda [Rust](https://www.rust-lang.org/) docker builder 🐑 🐳 🦀 [![Build Status](https://github.com/softprops/lambda-rust/workflows/Main/badge.svg)](https://github.com/softprops/lambda-rust/actions)
1+
# AWS Lambda [Rust](https://www.rust-lang.org/) docker builder 🐑 🦀 🐳 [![Build Status](https://github.com/softprops/lambda-rust/workflows/Main/badge.svg)](https://github.com/softprops/lambda-rust/actions)
22

33

44
## 🤔 about
55

66
This docker image extends [lambda ci `provided`](https://github.com/lambci/docker-lambda#documentation) builder docker image, a faithful reproduction of the actual AWS "**provided**" Lambda runtime environment,
77
and installs [rustup](https://rustup.rs/) and the *stable* rust toolchain.
88

9+
This provides a build environment, consistent with your target execution environment for predicable results.
10+
911
## 📦 install
1012

1113
Tags for this docker image follow the naming convention `softprops/lambda-rust:{version}-rust-{rust-stable-version}`
@@ -66,13 +68,76 @@ The hooks' names are predefined and must be placed in a directory `.lambda-rust`
6668

6769
You can take a look at an example [here](./tests/test-func-with-hooks).
6870

71+
## 🔬 local testing
72+
73+
Once you've built a Rust lambda function artifact, the `provided` runtime expects
74+
deployments of that artifact to be named "**bootstrap**". The `lambda-rust` docker image
75+
builds a zip file, named after the binary, containing your binary files renamed to "bootstrap" for you.
76+
77+
You can invoke this bootstap executable with the lambda-ci docker image for the `provided` AWS lambda runtime with a one off container.
78+
79+
```sh
80+
# start a one-off docker container replicating the "provided" lambda runtime
81+
# awaiting an event to be provided via stdin
82+
$ unzip -o \
83+
target/lambda/release/{your-binary-name}.zip \
84+
-d /tmp/lambda && \
85+
docker run \
86+
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
87+
--rm \
88+
-v /tmp/lambda:/var/task:ro,delegated \
89+
lambci/lambda:provided
90+
91+
# provide an event payload via stdin (typically a json blob)
92+
93+
# Ctrl-D to yield control back to your function
94+
```
95+
96+
If you may find the one-off container less than ideal for if you wish to trigger your lambda multiple times. For these cases try using the "stay open" mode of execution.
97+
98+
```sh
99+
# start a long running docker container replicating the "provided" lambda runtime
100+
# listening on port 9001
101+
$ unzip -o \
102+
target/lambda/release/{your-binary-name}.zip \
103+
-d /tmp/lambda && \
104+
docker run \
105+
--rm \
106+
-v /tmp/lambda:/var/task:ro,delegated \
107+
-e DOCKER_LAMBDA_STAY_OPEN=1 \
108+
-p 9001:9001 \
109+
lambci/lambda:provided
110+
```
111+
112+
In a separate terminal, you can invoke your function with `curl`
113+
114+
The `-d` flag is a means of providing your function's input.
115+
116+
```sh
117+
curl -d '{}' \
118+
http://localhost:9001/2015-03-31/functions/myfunction/invocations
119+
```
120+
121+
You can also the `aws` cli to invoke your function locally. The `--payload` is a means of providing your function's input.
122+
123+
```sh
124+
aws lambda invoke \
125+
--endpoint http://localhost:9001 \
126+
--cli-binary-format raw-in-base64-out \
127+
--no-sign-request \
128+
--function-name myfunction \
129+
--payload '{}' out.json \
130+
&& cat out.json \
131+
&& rm -f out.json
132+
```
133+
69134
## 🤸🤸 usage via cargo aws-lambda subcommand
70135

71-
If you want to set up ad hoc lambda functions or have another reason to not to go with full blown devops orchestration tools,
72-
there's a cargo subcommand to compile your code into a zip file and deploy it to an existing function. This comes with only
136+
A third party cargo subcommand exists to compile your code into a zip file and deploy it. This comes with only
73137
rust and docker as dependencies.
74138

75139
Setup
140+
76141
```sh
77142
$ cargo install cargo-aws-lambda
78143
```
@@ -89,29 +154,5 @@ $ cargo aws-lambda --help
89154
90155
More instructions can be found [here](https://github.com/vvilhonen/cargo-aws-lambda).
91156
92-
## 🔬 local testing
93-
94-
Once you've built a Rust lambda function artifact, the `provided` runtime expects
95-
deployments of that artifact to be named "**bootstrap**". The `lambda-rust` docker image
96-
builds a zip file, named after the binary, containing your binary files renamed to "bootstrap"
97-
98-
You can invoke this bootstap executable with the lambda-ci docker image for the `provided` AWS lambda runtime.
99-
100-
```sh
101-
# start a docker container replicating the "provided" lambda runtime
102-
# awaiting an event to be provided via stdin
103-
$ unzip -o \
104-
target/lambda/release/{your-binary-name}.zip \
105-
-d /tmp/lambda && \
106-
docker run \
107-
-i -e DOCKER_LAMBDA_USE_STDIN=1 \
108-
--rm \
109-
-v /tmp/lambda:/var/task \
110-
lambci/lambda:provided
111-
112-
# provide an event payload via stdin (typically a json blob)
113-
114-
# Ctrl-D to yield control back to your function
115-
```
116157
117158
Doug Tangren (softprops) 2020

0 commit comments

Comments
 (0)