Skip to content

Commit

Permalink
Make invocation URL dynamic
Browse files Browse the repository at this point in the history
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 aws#43 I raised a few weeks ago.
  • Loading branch information
skwashd committed Oct 4, 2021
1 parent 3567179 commit 8f229fd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 4 additions & 2 deletions cmd/aws-lambda-rie/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand Down
3 changes: 2 additions & 1 deletion cmd/aws-lambda-rie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions test/integration/local_lambda/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def tearDownClass(cls):
arch_tag = "" if arch == "" else f"-{arch}"
cmd = f"docker rm -f {image}{arch_tag}"
Popen(cmd.split(" ")).communicate()

for arch in ARCHS:
arch_tag = "" if arch == "" else f"-{arch}"
Popen(f"docker rmi {cls.image_name}{arch_tag}".split(" ")).communicate()
Expand Down Expand Up @@ -136,7 +136,7 @@ def test_lambda_function_arn_exists_with_defining_custom_name(self, arch, port):
time.sleep(SLEEP_TIME)

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

Expand Down Expand Up @@ -260,7 +260,7 @@ def test_function_name_is_overriden(self, arch, port):
time.sleep(SLEEP_TIME)

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

Expand Down

0 comments on commit 8f229fd

Please sign in to comment.