Skip to content

Support CORS #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.<br>**Warning:** Please be aware that this options may have security implications. | `"false"` |

## License

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Copy link
Owner Author

@daido1976 daido1976 Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first I tried to use cors.Default(), but it does not allow PUT/DELETE method, etc.

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())

Expand Down
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 2 additions & 0 deletions test/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Comment on lines +10 to +11
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not written any tests, only operation checks, so I would like to write tests once the specifications are finalized.

test-lambda-rie:
build:
context: ./lambda
Expand Down
Loading