Skip to content

Commit 66cd017

Browse files
author
AntoniaSzecsi
committed
Add local testing infrastructure with RIE
1 parent 7ac2274 commit 66cd017

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

.github/workflows/test-rie.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Test with RIE
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
push:
7+
branches: [ main ]
8+
9+
jobs:
10+
test-rie:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
example: [basic-lambda, basic-sqs]
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Build and test ${{ matrix.example }} with RIE
20+
run: |
21+
docker build -f Dockerfile.rie --build-arg EXAMPLE=${{ matrix.example }} -t rust-lambda-rie-test-${{ matrix.example }} .
22+
23+
# Start container in background
24+
docker run -d -p 9000:8080 --name rie-test-${{ matrix.example }} rust-lambda-rie-test-${{ matrix.example }}
25+
26+
# Wait for container to be ready
27+
sleep 5
28+
29+
# Test the function based on example type
30+
if [ "${{ matrix.example }}" = "basic-lambda" ]; then
31+
PAYLOAD='{"command": "test from CI"}'
32+
elif [ "${{ matrix.example }}" = "basic-sqs" ]; then
33+
PAYLOAD='{"Records": [{"body": "{\"id\": \"123\", \"text\": \"hello from SQS\"}", "messageId": "test-id", "receiptHandle": "test-handle", "attributes": {}, "messageAttributes": {}, "md5OfBody": "test-md5", "eventSource": "aws:sqs", "eventSourceARN": "arn:aws:sqs:us-east-1:123456789012:test-queue", "awsRegion": "us-east-1"}]}'
34+
fi
35+
36+
# Make request and verify response
37+
RESPONSE=$(curl -s -XPOST 'http://localhost:9000/2015-03-31/functions/function/invocations' \
38+
-d "$PAYLOAD" \
39+
-H 'Content-Type: application/json')
40+
41+
echo "Response: $RESPONSE"
42+
43+
# Basic validation that we got a response (not empty)
44+
if [ -z "$RESPONSE" ]; then
45+
echo "Error: Empty response"
46+
exit 1
47+
fi
48+
49+
# Stop container
50+
docker stop rie-test-${{ matrix.example }}

Dockerfile.rie

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
ARG EXAMPLE=basic-lambda
11+
12+
COPY Cargo.toml Cargo.lock /build/
13+
COPY lambda-runtime /build/lambda-runtime
14+
COPY lambda-runtime-api-client /build/lambda-runtime-api-client
15+
COPY lambda-events /build/lambda-events
16+
COPY lambda-http /build/lambda-http
17+
COPY lambda-extension /build/lambda-extension
18+
COPY examples/${EXAMPLE} /build/examples/${EXAMPLE}
19+
20+
WORKDIR /build/examples/${EXAMPLE}
21+
RUN cargo build --release
22+
RUN cp target/release/${EXAMPLE} ${LAMBDA_RUNTIME_DIR}/bootstrap
23+
24+
ENTRYPOINT []
25+
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 $(EXAMPLE)

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,31 @@ 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+
By default, this uses the `basic-lambda` example. To test a different example:
404+
405+
```bash
406+
make test-rie EXAMPLE=basic-sqs
407+
make test-rie EXAMPLE=http-basic-lambda
408+
```
409+
410+
This command will:
411+
1. Build a Docker image with Rust toolchain and RIE
412+
2. Compile the specified example inside the Linux container
413+
3. Start the RIE container on port 9000
414+
4. Display the appropriate curl command for testing
415+
416+
Different examples expect different payload formats. Check the example's source code in `examples/EXAMPLE_NAME/src/main.rs`
417+
418+
This provides automated testing with Docker and RIE, ensuring your Lambda functions work in a Linux environment identical to AWS Lambda.
419+
395420
### Lambda Debug Proxy
396421

397422
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
EXAMPLE=${1:-basic-lambda}
5+
6+
echo "Building Docker image with RIE for example: $EXAMPLE..."
7+
docker build -f Dockerfile.rie --build-arg EXAMPLE=$EXAMPLE -t rust-lambda-rie-test .
8+
9+
echo "Starting RIE container on port 9000..."
10+
docker run -p 9000:8080 rust-lambda-rie-test &
11+
CONTAINER_PID=$!
12+
13+
echo "Container started. Test with:"
14+
if [ "$EXAMPLE" = "basic-lambda" ]; then
15+
echo "curl -XPOST 'http://localhost:9000/2015-03-31/functions/function/invocations' -d '{\"command\": \"test from RIE\"}' -H 'Content-Type: application/json'"
16+
else
17+
echo "For example '$EXAMPLE', check examples/$EXAMPLE/src/main.rs for the expected payload format."
18+
fi
19+
echo ""
20+
echo "Press Ctrl+C to stop the container."
21+
22+
wait $CONTAINER_PID

0 commit comments

Comments
 (0)