Skip to content

Commit 64a56fa

Browse files
committed
feat: lambda emulator and test directory
1 parent 7e63fc6 commit 64a56fa

File tree

11 files changed

+126
-0
lines changed

11 files changed

+126
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*.crt
99
*.key
1010
settings.test
11+
__pycache__/*
12+
*.pyc
1113

1214
# User-specific stuff
1315
.idea/**/workspace.xml

docs/img/lambda_emulator.png

33.4 KB
Loading

tests/integration/.gitkeep

Whitespace-only changes.

tests/lambda-emulator/Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM public.ecr.aws/lambda/python:3.8
2+
3+
# Install the function's dependencies using file requirements.txt
4+
# from your project folder.
5+
6+
COPY requirements.txt .
7+
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
8+
9+
# Copy function code
10+
COPY app/lambda_function.py ${LAMBDA_TASK_ROOT}
11+
12+
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
13+
CMD [ "lambda_function.lambda_handler" ]

tests/lambda-emulator/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
start:
2+
docker-compose up -d
3+
4+
ps:
5+
docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Names}}"
6+
7+
watch:
8+
watch 'docker ps --format "table {{.ID}}\t{{.Image}}\t{{.Ports}}\t{{.Names}}"'
9+
10+
down:
11+
docker-compose down
12+
13+
clean:
14+
docker kill $$(docker ps -q) 2> /dev/null || true
15+
docker system prune -a
16+
docker volume rm $(docker volume ls -qf dangling=true)

tests/lambda-emulator/README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# AWS Lambda Runtime Interface Emulator
2+
3+
The AWS Lambda Runtime Interface Emulator (RIE) is a proxy for the Lambda Runtime API that allows you to locally test your Lambda function packaged as a container image. The emulator is a lightweight web server that converts HTTP requests into JSON events to pass to the Lambda function in the container image.
4+
5+
> Note: The RIE does not emulate **Lambda’s security and authentication configurations**, or Lambda orchestration.
6+
7+
8+
## Getting Started
9+
10+
- **Implement a Lambda Function:**
11+
12+
e.g., [`app/lambda_function.py`](./app/lambda_function.py):
13+
14+
```python
15+
import json
16+
17+
def lambda_handler(event, context):
18+
return {
19+
'statusCode': 200,
20+
'body': {
21+
'request': event,
22+
'response': 'Hello from NGINX Lambda Gateway Emulator!'
23+
}
24+
}
25+
```
26+
27+
- **Start a Lambda Emulator:**
28+
```bash
29+
make start
30+
```
31+
32+
- **Check if Lambda Emulator is started:**
33+
```bash
34+
make watch
35+
```
36+
![](../../docs/img/lambda_emulator.png)
37+
38+
- **Test a function via Lambda Emulator:**
39+
```bash
40+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"message": "This is to test a Lambda Function ARN."}'
41+
42+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
43+
```
44+
45+
- **Stop a Lambda Emulator:**
46+
```bash
47+
make down
48+
```
49+
50+
- **Clean Lambda Emulator Container Image:**
51+
```bash
52+
make clean
53+
```
54+
55+
## References
56+
- [Testing Lambda container images locally](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html)
57+
- [Using AWS Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
3+
def lambda_handler(event, context):
4+
return {
5+
'statusCode': 200,
6+
'body': {
7+
'request': event,
8+
'response': 'Hello from NGINX Lambda Gateway Emulator - /foo!'
9+
}
10+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3'
2+
services:
3+
4+
lambda_emulator:
5+
hostname: lambda_emulator
6+
container_name: lambda_emulator
7+
image: lambda_emulator
8+
build:
9+
context: ./
10+
dockerfile: Dockerfile
11+
env_file:
12+
- settings.env
13+
volumes:
14+
- type: bind
15+
source: ./app
16+
target: /var/task/
17+
ports:
18+
- "9000:8080"
19+
networks:
20+
- mynetwork
21+
22+
networks:
23+
mynetwork:

tests/lambda-emulator/requirements.txt

Whitespace-only changes.

tests/lambda-emulator/settings.env

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
AWS_ACCESS_KEY_ID=aaaaa
2+
AWS_SECRET_ACCESS_KEY=bbbbb
3+
AWS_SESSION_TOKEN=ccccc
4+
AWS_REGION=us-east-2
5+
AWS_LAMBDA_FUNCTION_NAME=foo

tests/unit/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)