Skip to content

Commit 434d1a6

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 703935c commit 434d1a6

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

Diff for: 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

Diff for: 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) {

Diff for: test/integration/local_lambda/end-to-end-test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_lambda_function_arn_exists_with_defining_custom_name(self):
8080
# sleep 1s to give enough time for the endpoint to be up to curl
8181
time.sleep(SLEEP_TIME)
8282

83-
r = requests.post("http://localhost:9000/2015-03-31/functions/function/invocations", json={})
83+
r = requests.post("http://localhost:9000/2015-03-31/functions/MyCoolName/invocations", json={})
8484
self.assertEqual(b'"My lambda ran succesfully"', r.content)
8585

8686

@@ -190,7 +190,7 @@ def test_function_name_is_overriden(self):
190190
# sleep 1s to give enough time for the endpoint to be up to curl
191191
time.sleep(SLEEP_TIME)
192192

193-
r = requests.post("http://localhost:9009/2015-03-31/functions/function/invocations", json={})
193+
r = requests.post("http://localhost:9009/2015-03-31/functions/MyCoolName/invocations", json={})
194194
self.assertEqual(b'"My lambda ran succesfully"', r.content)
195195

196196

0 commit comments

Comments
 (0)