Skip to content

Commit 136f4b9

Browse files
authored
Merge pull request #2 from daido1976/support-cors
Support CORS
2 parents f05fcdd + 20626c6 commit 136f4b9

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ A lightweight Docker image to emulate **AWS Lambda Function URLs** locally. It w
88
- Automatically forwards HTTP requests to a locally running Lambda function to work with the AWS Lambda Runtime Interface Emulator.
99
- Supports `APIGatewayProxyEventV2` for HTTP API requests.
1010
- Handles `isBase64Encoded` for binary data.
11+
- Supports enabling CORS via an environment variable, allowing cross-origin requests.
1112

1213
## Getting Started
1314

@@ -49,9 +50,10 @@ RIE_ENDPOINT=http://custom-host:9000/2015-03-31/functions/function/invocations a
4950

5051
## Environment Variables
5152

52-
| Variable | Description | Default Value |
53-
| -------------- | --------------------------------------------------- | ----------------------------------------------------------------- |
54-
| `RIE_ENDPOINT` | URL for the Lambda Runtime Interface Emulator (RIE) | `http://localhost:9000/2015-03-31/functions/function/invocations` |
53+
| Variable | Description | Default Value |
54+
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
55+
| `RIE_ENDPOINT` | URL for the Lambda Runtime Interface Emulator (RIE) | `http://localhost:9000/2015-03-31/functions/function/invocations` |
56+
| `ENABLE_CORS` | Set this to `"true"` to enable CORS for all origins, methods, and headers.<br>**Warning:** Please be aware that this options may have security implications. | `"false"` |
5557

5658
## License
5759

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/daido1976/aws-lambda-function-url-emulator
22

33
go 1.23.3
44

5-
require github.com/aws/aws-lambda-go v1.47.0 // indirect
5+
require (
6+
github.com/aws/aws-lambda-go v1.47.0 // indirect
7+
github.com/rs/cors v1.11.1 // indirect
8+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI=
22
github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A=
3+
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=
4+
github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=

main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"time"
1313

1414
"github.com/aws/aws-lambda-go/events"
15+
"github.com/rs/cors"
1516
)
1617

1718
var port = getEnv("PORT", "8080")
1819
var rieEndpoint = getEnv("RIE_ENDPOINT", "http://localhost:9000/2015-03-31/functions/function/invocations")
20+
var enableCors = getEnv("ENABLE_CORS", "false")
1921

2022
func getEnv(key, fallback string) string {
2123
if value, exists := os.LookupEnv(key); exists {
@@ -25,12 +27,21 @@ func getEnv(key, fallback string) string {
2527
}
2628

2729
func main() {
28-
http.HandleFunc("/", handler)
30+
var rootHandler http.Handler
31+
32+
if enableCors == "true" {
33+
rootHandler = cors.AllowAll().Handler(http.HandlerFunc(lambdaUrlProxyHandler))
34+
log.Println("[Lambda URL Proxy] CORS enabled")
35+
} else {
36+
rootHandler = http.HandlerFunc(lambdaUrlProxyHandler)
37+
}
38+
39+
http.Handle("/", rootHandler)
2940
log.Printf("[Lambda URL Proxy] Listening on http://localhost:%s\n", port)
3041
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
3142
}
3243

33-
func handler(w http.ResponseWriter, r *http.Request) {
44+
func lambdaUrlProxyHandler(w http.ResponseWriter, r *http.Request) {
3445
// Log the incoming request
3546
log.Printf("[Lambda URL Proxy] %s %s\n", r.Method, r.URL.String())
3647

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestHandler(t *testing.T) {
3737

3838
rieEndpoint = mockRieServer.URL
3939

40-
server := httptest.NewServer(http.HandlerFunc(handler))
40+
server := httptest.NewServer(http.HandlerFunc(lambdaUrlProxyHandler))
4141
defer server.Close()
4242

4343
req, _ := http.NewRequest("POST", server.URL+"/foo/bar?testkey=testvalue", strings.NewReader("test body"))

test/compose.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ services:
77
- "8080:8080"
88
environment:
99
RIE_ENDPOINT: "http://test-lambda-rie:8080/2015-03-31/functions/function/invocations"
10+
# Uncomment out when testing for CORS.
11+
# ENABLE_CORS: "true"
1012
test-lambda-rie:
1113
build:
1214
context: ./lambda

0 commit comments

Comments
 (0)