Skip to content

Commit 52e8441

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 42ccdb2 commit 52e8441

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: cmd/aws-lambda-rie/http.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ import (
1111
"go.amzn.com/lambda/rapidcore"
1212
)
1313

14-
func startHTTPServer(ipport string, sandbox *rapidcore.SandboxBuilder, bs interop.Bootstrap) {
14+
func startHTTPServer(ipport string, sandbox *rapidcore.SandboxBuilder, bs interop.Bootstrap, funcName string) {
1515
srv := &http.Server{
1616
Addr: ipport,
1717
}
1818

19-
// Pass a channel
20-
http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) {
21-
InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs)
22-
})
19+
var functions = []string{funcName}
20+
if funcName != "function" {
21+
functions = []string{"function", funcName}
22+
}
23+
for _, funcName := range functions {
24+
// Pass a channel
25+
http.HandleFunc("/2015-03-31/functions/"+funcName+"/invocations", func(w http.ResponseWriter, r *http.Request) {
26+
InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs)
27+
})
28+
}
2329

2430
// go routine (main thread waits)
2531
if err := srv.ListenAndServe(); err != nil {

Diff for: cmd/aws-lambda-rie/main.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ func main() {
7878
sandbox.SetRuntimeAPIAddress(opts.RuntimeAPIAddress)
7979
}
8080

81+
funcName := GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "function")
82+
8183
sandboxContext, internalStateFn := sandbox.Create()
8284
// Since we have not specified a custom interop server for standalone, we can
8385
// directly reference the default interop server, which is a concrete type
8486
sandbox.DefaultInteropServer().SetSandboxContext(sandboxContext)
8587
sandbox.DefaultInteropServer().SetInternalStateGetter(internalStateFn)
8688

87-
startHTTPServer(opts.RuntimeInterfaceEmulatorAddress, sandbox, bootstrap)
89+
startHTTPServer(opts.RuntimeInterfaceEmulatorAddress, sandbox, bootstrap, funcName)
8890
}
8991

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

0 commit comments

Comments
 (0)