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 #43 I raised a few weeks ago.
  • Loading branch information
skwashd committed Jul 9, 2024
1 parent e3c135c commit ff9e4f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 11 additions & 5 deletions cmd/aws-lambda-rie/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ import (
"go.amzn.com/lambda/rapidcore"
)

func startHTTPServer(ipport string, sandbox *rapidcore.SandboxBuilder, bs interop.Bootstrap) {
func startHTTPServer(ipport string, sandbox *rapidcore.SandboxBuilder, bs interop.Bootstrap, funcName string) {
srv := &http.Server{
Addr: ipport,
}

// Pass a channel
http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) {
InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs)
})
var functions = []string{funcName}
if funcName != "function" {
functions = []string{"function", funcName}
}
for _, funcName := range functions {
// Pass a channel
http.HandleFunc("/2015-03-31/functions/"+funcName+"/invocations", func(w http.ResponseWriter, r *http.Request) {
InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs)
})
}

// go routine (main thread waits)
if err := srv.ListenAndServe(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/aws-lambda-rie/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ func main() {
sandbox.SetRuntimeAPIAddress(opts.RuntimeAPIAddress)
}

funcName := GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "function")

sandboxContext, internalStateFn := sandbox.Create()
// Since we have not specified a custom interop server for standalone, we can
// directly reference the default interop server, which is a concrete type
sandbox.DefaultInteropServer().SetSandboxContext(sandboxContext)
sandbox.DefaultInteropServer().SetInternalStateGetter(internalStateFn)

startHTTPServer(opts.RuntimeInterfaceEmulatorAddress, sandbox, bootstrap)
startHTTPServer(opts.RuntimeInterfaceEmulatorAddress, sandbox, bootstrap, funcName)
}

func getCLIArgs() (options, []string) {
Expand Down

0 comments on commit ff9e4f1

Please sign in to comment.