diff --git a/README.md b/README.md index e51b9ba..c17db85 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A lightweight Docker image to emulate **AWS Lambda Function URLs** locally. It w - Automatically forwards HTTP requests to a locally running Lambda function to work with the AWS Lambda Runtime Interface Emulator. - Supports `APIGatewayProxyEventV2` for HTTP API requests. - Handles `isBase64Encoded` for binary data. +- Supports enabling CORS via an environment variable, allowing cross-origin requests. ## Getting Started @@ -49,9 +50,10 @@ RIE_ENDPOINT=http://custom-host:9000/2015-03-31/functions/function/invocations a ## Environment Variables -| Variable | Description | Default Value | -| -------------- | --------------------------------------------------- | ----------------------------------------------------------------- | -| `RIE_ENDPOINT` | URL for the Lambda Runtime Interface Emulator (RIE) | `http://localhost:9000/2015-03-31/functions/function/invocations` | +| Variable | Description | Default Value | +| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------- | +| `RIE_ENDPOINT` | URL for the Lambda Runtime Interface Emulator (RIE) | `http://localhost:9000/2015-03-31/functions/function/invocations` | +| `ENABLE_CORS` | Set this to `"true"` to enable CORS for all origins, methods, and headers.
**Warning:** Please be aware that this options may have security implications. | `"false"` | ## License diff --git a/go.mod b/go.mod index 852cf1b..43efb9a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/daido1976/aws-lambda-function-url-emulator go 1.23.3 -require github.com/aws/aws-lambda-go v1.47.0 // indirect +require ( + github.com/aws/aws-lambda-go v1.47.0 // indirect + github.com/rs/cors v1.11.1 // indirect +) diff --git a/go.sum b/go.sum index 23a2b40..72654d5 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI= github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= +github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA= +github.com/rs/cors v1.11.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= diff --git a/main.go b/main.go index 8ef7a2b..429ddd8 100644 --- a/main.go +++ b/main.go @@ -12,10 +12,12 @@ import ( "time" "github.com/aws/aws-lambda-go/events" + "github.com/rs/cors" ) var port = getEnv("PORT", "8080") var rieEndpoint = getEnv("RIE_ENDPOINT", "http://localhost:9000/2015-03-31/functions/function/invocations") +var enableCors = getEnv("ENABLE_CORS", "false") func getEnv(key, fallback string) string { if value, exists := os.LookupEnv(key); exists { @@ -25,12 +27,21 @@ func getEnv(key, fallback string) string { } func main() { - http.HandleFunc("/", handler) + var rootHandler http.Handler + + if enableCors == "true" { + rootHandler = cors.AllowAll().Handler(http.HandlerFunc(lambdaUrlProxyHandler)) + log.Println("[Lambda URL Proxy] CORS enabled") + } else { + rootHandler = http.HandlerFunc(lambdaUrlProxyHandler) + } + + http.Handle("/", rootHandler) log.Printf("[Lambda URL Proxy] Listening on http://localhost:%s\n", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) } -func handler(w http.ResponseWriter, r *http.Request) { +func lambdaUrlProxyHandler(w http.ResponseWriter, r *http.Request) { // Log the incoming request log.Printf("[Lambda URL Proxy] %s %s\n", r.Method, r.URL.String()) diff --git a/main_test.go b/main_test.go index dbaf571..eb4983d 100644 --- a/main_test.go +++ b/main_test.go @@ -37,7 +37,7 @@ func TestHandler(t *testing.T) { rieEndpoint = mockRieServer.URL - server := httptest.NewServer(http.HandlerFunc(handler)) + server := httptest.NewServer(http.HandlerFunc(lambdaUrlProxyHandler)) defer server.Close() req, _ := http.NewRequest("POST", server.URL+"/foo/bar?testkey=testvalue", strings.NewReader("test body")) diff --git a/test/compose.yaml b/test/compose.yaml index 1ba11f8..4451221 100644 --- a/test/compose.yaml +++ b/test/compose.yaml @@ -7,6 +7,8 @@ services: - "8080:8080" environment: RIE_ENDPOINT: "http://test-lambda-rie:8080/2015-03-31/functions/function/invocations" + # Uncomment out when testing for CORS. + # ENABLE_CORS: "true" test-lambda-rie: build: context: ./lambda