Skip to content

Commit 8f229fd

Browse files
committed
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.
1 parent 3567179 commit 8f229fd

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

cmd/aws-lambda-rie/http.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
log "github.com/sirupsen/logrus"
1010
)
1111

12-
func startHTTPServer(ipport string, sandbox Sandbox) {
12+
func startHTTPServer(ipport string, sandbox Sandbox, funcName string) {
1313
srv := &http.Server{
1414
Addr: ipport,
1515
}
1616

17+
url := "/2015-03-31/functions/" + funcName + "/invocations"
18+
1719
// Pass a channel
18-
http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) {
20+
http.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
1921
InvokeHandler(w, r, sandbox)
2022
})
2123

cmd/aws-lambda-rie/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func main() {
4141
go sandbox.Create()
4242

4343
testAPIipport := "0.0.0.0:8080"
44-
startHTTPServer(testAPIipport, sandbox)
44+
funcName := GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "function")
45+
startHTTPServer(testAPIipport, sandbox, funcName)
4546
}
4647

4748
func getCLIArgs() (options, []string) {

test/integration/local_lambda/test_end_to_end.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def tearDownClass(cls):
6060
arch_tag = "" if arch == "" else f"-{arch}"
6161
cmd = f"docker rm -f {image}{arch_tag}"
6262
Popen(cmd.split(" ")).communicate()
63-
63+
6464
for arch in ARCHS:
6565
arch_tag = "" if arch == "" else f"-{arch}"
6666
Popen(f"docker rmi {cls.image_name}{arch_tag}".split(" ")).communicate()
@@ -136,7 +136,7 @@ def test_lambda_function_arn_exists_with_defining_custom_name(self, arch, port):
136136
time.sleep(SLEEP_TIME)
137137

138138
r = requests.post(
139-
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
139+
f"http://localhost:{port}/2015-03-31/functions/MyCoolName/invocations", json={}
140140
)
141141
self.assertEqual(b'"My lambda ran succesfully"', r.content)
142142

@@ -260,7 +260,7 @@ def test_function_name_is_overriden(self, arch, port):
260260
time.sleep(SLEEP_TIME)
261261

262262
r = requests.post(
263-
f"http://localhost:{port}/2015-03-31/functions/function/invocations", json={}
263+
f"http://localhost:{port}/2015-03-31/functions/MyCoolName/invocations", json={}
264264
)
265265
self.assertEqual(b'"My lambda ran succesfully"', r.content)
266266

0 commit comments

Comments
 (0)