Skip to content

Commit 1c159d1

Browse files
committed
CORS support. Allow OPTIONS request. Support requests from @aws-sdk/client-lambda
Fixes aws#16 Sets response headers allowing requests from browsers that send preflight OPTIONS requests. Previously using @aws-sdk/client-lambda returned CORS errors. Now requests and responses can be sent. Test Plan: Build and run locally. Ensure curl -XPOST still works. Ensure requests from browsers now work and from @aws-sdk/client-lambda Test steps added to Contributing.md. Ensure `make integ-tests-and-compile` passes
1 parent 6c827ea commit 1c159d1

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

CONTRIBUTING.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ reported the issue. Please try to include as much information as you can. Detail
2121

2222

2323
## Contributing via Pull Requests
24-
This repository contains the source code and examples for the Runtime Interface Emulator. We will accept pull requests on documentation, examples, bug fixes and the Dockerfiles. We will also accept pull requests, issues and feedback on improvements to the Runtime Interface Emulator. However, our priority will be to maintain fidelity with AWS Lambda’s Runtime Interface on the cloud.
24+
This repository contains the source code and examples for the Runtime Interface Emulator. We will accept pull requests on documentation, examples, bug fixes and the Dockerfiles. We will also accept pull requests, issues and feedback on improvements to the Runtime Interface Emulator. However, our priority will be to maintain fidelity with AWS Lambda’s Runtime Interface on the cloud.
2525

2626
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:
2727

@@ -41,6 +41,86 @@ To send us a pull request, please:
4141
GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and
4242
[creating a pull request](https://help.github.com/articles/creating-a-pull-request/).
4343

44+
## How to Run Locally
45+
1. [Install go](https://go.dev/doc/install)
46+
2. Build
47+
```bash
48+
make compile-lambda-linux # Outputs binary to ./bin/aws-lambda-rie-x86_64
49+
```
50+
3. [Create an alternative base image](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html#images-create-from-alt)
51+
4. [Build RIE into this alternative base image](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html#images-test-alternative)
52+
- ./app/app.py
53+
```python
54+
import typing
55+
56+
def handler(event: typing.Any, context: typing.Any):
57+
print("Received event:", event, "content:", context)
58+
return "Test Complete"
59+
```
60+
- ./Dockerfile
61+
```
62+
# Define function directory
63+
ARG FUNCTION_DIR="/function"
64+
65+
FROM python:buster as build-image
66+
67+
# Install aws-lambda-cpp build dependencies
68+
RUN apt-get update && \
69+
apt-get install -y \
70+
g++ \
71+
make \
72+
cmake \
73+
unzip \
74+
libcurl4-openssl-dev
75+
76+
# Include global arg in this stage of the build
77+
ARG FUNCTION_DIR
78+
# Create function directory
79+
RUN mkdir -p ${FUNCTION_DIR}
80+
81+
# Copy function code
82+
COPY app/* ${FUNCTION_DIR}
83+
84+
# Install the runtime interface client
85+
RUN pip install \
86+
--target ${FUNCTION_DIR} \
87+
awslambdaric
88+
89+
# Multi-stage build: grab a fresh copy of the base image
90+
FROM python:buster
91+
92+
# Include global arg in this stage of the build
93+
ARG FUNCTION_DIR
94+
# Set working directory to function root directory
95+
WORKDIR ${FUNCTION_DIR}
96+
97+
# Copy in the build image dependencies
98+
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}
99+
100+
COPY ./entry_script.sh /entry_script.sh
101+
ADD bin/aws-lambda-rie-x86_64 /usr/local/bin/aws-lambda-rie
102+
ENTRYPOINT [ "/entry_script.sh" ]
103+
104+
CMD [ "app.handler" ]
105+
```
106+
- ./entry_script.sh
107+
```bash
108+
#!/bin/sh
109+
if [ -z "${AWS_LAMBDA_RUNTIME_API}" ]; then
110+
exec /usr/local/bin/aws-lambda-rie /usr/local/bin/python -m awslambdaric $@
111+
else
112+
exec /usr/local/bin/python -m awslambdaric $@
113+
fi
114+
```
115+
5. Build and run the image
116+
```bash
117+
docker build -t hello-world
118+
docker run -p 9000:8080 hello-world
119+
```
120+
6. Send a test request and see that your changes are hit
121+
```bash
122+
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
123+
```
44124

45125
## Finding contributions to work on
46126
Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start.

cmd/aws-lambda-rie/handlers.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,16 @@ func InvokeHandler(w http.ResponseWriter, r *http.Request, sandbox Sandbox) {
6565
w.WriteHeader(500)
6666
return
6767
}
68-
68+
if origin := r.Header.Get("Origin"); origin != "" {
69+
w.Header().Set("Access-Control-Allow-Origin", GetenvWithDefault("ACCESS_CONTROL_ALLOW_ORIGIN", origin))
70+
w.Header().Set("Access-Control-Allow-Methods", GetenvWithDefault("ACCESS_CONTROL_ALLOW_METHODS", "POST, OPTIONS"))
71+
w.Header().Set("Access-Control-Allow-Headers", GetenvWithDefault("ACCESS_CONTROL_ALLOW_HEADERS", "*"))
72+
w.Header().Set("Access-Control-Allow-Credentials", GetenvWithDefault("ACCESS_CONTROL_ALLOW_CREDENTIALS", "true"))
73+
}
74+
if r.Method == "OPTIONS" {
75+
w.WriteHeader(200)
76+
return
77+
}
6978
initDuration := ""
7079
inv := GetenvWithDefault("AWS_LAMBDA_FUNCTION_TIMEOUT", "300")
7180
timeoutDuration, _ := time.ParseDuration(inv + "s")

0 commit comments

Comments
 (0)