Skip to content

Commit 4e2cf4b

Browse files
author
AntoniaSzecsi
committed
Add local testing feature with RIE
1 parent 92a80b4 commit 4e2cf4b

File tree

8 files changed

+106
-2
lines changed

8 files changed

+106
-2
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
target/debug
2+
target/*/debug
3+
.git
4+
.github
5+
examples
6+
lambda-integration-tests
7+
*.md
8+
.gitignore
9+
.rustfmt.toml

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ members = [
99
"lambda-events",
1010
]
1111

12-
exclude = ["examples"]
12+
exclude = ["examples", "test-handler"]
1313

1414
[workspace.dependencies]
1515
base64 = "0.22"

Dockerfile.rie

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM public.ecr.aws/lambda/provided:al2023
2+
3+
RUN dnf install -y gcc
4+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
5+
ENV PATH="/root/.cargo/bin:${PATH}"
6+
7+
ADD https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie /usr/local/bin/aws-lambda-rie
8+
RUN chmod +x /usr/local/bin/aws-lambda-rie
9+
10+
COPY . /build
11+
WORKDIR /build/test-handler
12+
RUN cargo build --release
13+
RUN cp target/release/test-handler ${LAMBDA_RUNTIME_DIR}/bootstrap
14+
15+
ENTRYPOINT []
16+
CMD [ "/usr/local/bin/aws-lambda-rie", "/var/runtime/bootstrap" ]

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,7 @@ check-event-features:
108108
cargo test --package aws_lambda_events --no-default-features --features streams
109109

110110
fmt:
111-
cargo +nightly fmt --all
111+
cargo +nightly fmt --all
112+
113+
test-rie:
114+
./scripts/test-rie.sh

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,30 @@ curl -v -X POST \
392392
393393
You can read more about how [cargo lambda watch](https://www.cargo-lambda.info/commands/watch.html) and [cargo lambda invoke](https://www.cargo-lambda.info/commands/invoke.html) work on the project's [documentation page](https://www.cargo-lambda.info).
394394

395+
### Local testing with Runtime Interface Emulator (RIE)
396+
397+
For testing with the official AWS Lambda Runtime Interface Emulator, use the provided RIE testing infrastructure:
398+
399+
```bash
400+
make test-rie
401+
```
402+
403+
This command will:
404+
1. Build a Docker image with Rust toolchain and RIE
405+
2. Compile the test handler inside the Linux container
406+
3. Start the RIE container on port 9000
407+
4. Display the curl command to test your function
408+
409+
Once running, test the function from another terminal:
410+
411+
```bash
412+
curl -XPOST 'http://localhost:9000/2015-03-31/functions/function/invocations' \
413+
-d '{"command": "test from RIE"}' \
414+
-H 'Content-Type: application/json'
415+
```
416+
417+
This provides automated testing with Docker and RIE, ensuring your Lambda functions work in a Linux environment identical to AWS Lambda.
418+
395419
### Lambda Debug Proxy
396420

397421
Lambdas can be run and debugged locally using a special [Lambda debug proxy](https://github.com/rimutaka/lambda-debug-proxy) (a non-AWS repo maintained by @rimutaka), which is a Lambda function that forwards incoming requests to one AWS SQS queue and reads responses from another queue. A local proxy running on your development computer reads the queue, calls your Lambda locally and sends back the response. This approach allows debugging of Lambda functions locally while being part of your AWS workflow. The Lambda handler code does not need to be modified between the local and AWS versions.

scripts/test-rie.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
echo "Building Docker image with RIE..."
5+
docker build -f Dockerfile.rie -t rust-lambda-rie-test .
6+
7+
echo "Starting RIE container on port 9000..."
8+
docker run -p 9000:8080 rust-lambda-rie-test
9+
10+
echo "Container started. Test with:"
11+
echo "curl -XPOST 'http://localhost:9000/2015-03-31/functions/function/invocations' -d '{\"command\": \"test from RIE\"}' -H 'Content-Type: application/json'"
12+
echo ""
13+
echo "Press Ctrl+C to stop the container."

test-handler/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "test-handler"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
lambda_runtime = { path = "../lambda-runtime" }
8+
serde = "1.0"
9+
tokio = { version = "1", features = ["macros"] }

test-handler/src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use lambda_runtime::{service_fn, tracing, Error, LambdaEvent};
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Deserialize)]
5+
struct Request {
6+
command: String,
7+
}
8+
9+
#[derive(Serialize)]
10+
struct Response {
11+
message: String,
12+
status: String,
13+
}
14+
15+
#[tokio::main]
16+
async fn main() -> Result<(), Error> {
17+
tracing::init_default_subscriber();
18+
let func = service_fn(handler);
19+
lambda_runtime::run(func).await?;
20+
Ok(())
21+
}
22+
23+
async fn handler(event: LambdaEvent<Request>) -> Result<Response, Error> {
24+
let command = event.payload.command;
25+
26+
Ok(Response {
27+
message: format!("Echo: {}", command),
28+
status: "success".to_string(),
29+
})
30+
}

0 commit comments

Comments
 (0)