From 434d1a6a2749180283749a932889fe594490f100 Mon Sep 17 00:00:00 2001 From: Dave Hall Date: Sat, 4 Sep 2021 16:17:46 +1000 Subject: [PATCH] Make invocation URL dynamic The API endpoint in Amazon's implementation of Lambda used the path `/2015-03-31/functions/[func]/invocations` to invoke the function. `[func]` the name of the function and is set as the value of the `AWS_LAMBDA_FUNCTION_NAME` environment variable when the function is executing on AWS. The emulator uses a hard coded endpoint of `/2015-03-31/functions/function/invocations`. This is even the case when the `AWS_LAMBDA_FUNCTION_NAME` environment variable is set to another value. This patch changes the endpoint URL when the `AWS_LAMBDA_FUNCTION_NAME` environment variable is set. In this case the invocation URL will be `/2015-03-31/functions/${AWS_LAMBDA_FUNCTION_NAME}/invocations`. When the environment variable isn't set, the current behaviour persists and the function name is set to `function`. The end result is the emulator behaviour is closer to the real environment the functions execute in. This PR fixes #43 I raised a few weeks ago. --- cmd/aws-lambda-rie/http.go | 6 ++++-- cmd/aws-lambda-rie/main.go | 3 ++- test/integration/local_lambda/end-to-end-test.py | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/aws-lambda-rie/http.go b/cmd/aws-lambda-rie/http.go index be4002d..5c8696b 100644 --- a/cmd/aws-lambda-rie/http.go +++ b/cmd/aws-lambda-rie/http.go @@ -9,13 +9,15 @@ import ( log "github.com/sirupsen/logrus" ) -func startHTTPServer(ipport string, sandbox Sandbox) { +func startHTTPServer(ipport string, sandbox Sandbox, funcName string) { srv := &http.Server{ Addr: ipport, } + url := "/2015-03-31/functions/" + funcName + "/invocations" + // Pass a channel - http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { InvokeHandler(w, r, sandbox) }) diff --git a/cmd/aws-lambda-rie/main.go b/cmd/aws-lambda-rie/main.go index a151ae7..4abb840 100644 --- a/cmd/aws-lambda-rie/main.go +++ b/cmd/aws-lambda-rie/main.go @@ -41,7 +41,8 @@ func main() { go sandbox.Create() testAPIipport := "0.0.0.0:8080" - startHTTPServer(testAPIipport, sandbox) + funcName := GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "function") + startHTTPServer(testAPIipport, sandbox, funcName) } func getCLIArgs() (options, []string) { diff --git a/test/integration/local_lambda/end-to-end-test.py b/test/integration/local_lambda/end-to-end-test.py index 27d0e07..06cb85a 100644 --- a/test/integration/local_lambda/end-to-end-test.py +++ b/test/integration/local_lambda/end-to-end-test.py @@ -80,7 +80,7 @@ def test_lambda_function_arn_exists_with_defining_custom_name(self): # sleep 1s to give enough time for the endpoint to be up to curl time.sleep(SLEEP_TIME) - r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={}) + r = requests.post("http://localhost:9000/2015-03-31/functions/MyCoolName/invocations", json={}) self.assertEqual(b'"My lambda ran succesfully"', r.content) @@ -190,7 +190,7 @@ def test_function_name_is_overriden(self): # sleep 1s to give enough time for the endpoint to be up to curl time.sleep(SLEEP_TIME) - r = requests.post("http://localhost:9009/2015-03-31/functions/function/invocations", json={}) + r = requests.post("http://localhost:9009/2015-03-31/functions/MyCoolName/invocations", json={}) self.assertEqual(b'"My lambda ran succesfully"', r.content)