Skip to content

Commit fea5a14

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 46983db commit fea5a14

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

cmd/aws-lambda-rie/http.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ 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-
// Pass a channel
18-
http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) {
19-
InvokeHandler(w, r, sandbox)
20-
})
17+
var functions = []string{funcName}
18+
if funcName != "function" {
19+
functions = []string{"function", funcName}
20+
}
21+
for _, funcName := range functions {
22+
// Pass a channel
23+
http.HandleFunc("/2015-03-31/functions/"+funcName+"/invocations", func(w http.ResponseWriter, r *http.Request) {
24+
InvokeHandler(w, r, sandbox)
25+
})
26+
}
2127

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

cmd/aws-lambda-rie/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ func main() {
4646
go sandbox.Create()
4747

4848
testAPIipport := "0.0.0.0:8080"
49-
startHTTPServer(testAPIipport, sandbox)
49+
funcName := GetenvWithDefault("AWS_LAMBDA_FUNCTION_NAME", "function")
50+
startHTTPServer(testAPIipport, sandbox, funcName)
5051
}
5152

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

0 commit comments

Comments
 (0)