Conversation
Merge changes for v1.2 release
Merge changes for v1.3 release
Merge changes for v1.8 release
Merge changes for v1.9 release
Merge changes for v1.11 release
There was a problem hiding this comment.
Update readme:
- Set
AWS_LAMBDA_SERVER_MAX_INVOCATIONSto 1 to exit server after one invocation API_ACCESS_KEYis token used to access apiforward-responseis header to forward response to
bin/lambda-entrypoint.sh
Outdated
| # Set API_ACCESS_KEY if api_access_key exists | ||
| if [ -f "${LAMBDA_TASK_ROOT}/.api_access_key" ]; then | ||
| export API_ACCESS_KEY=$(cat ${LAMBDA_TASK_ROOT}/.api_access_key) | ||
| rm -f ${LAMBDA_TASK_ROOT}/.api_access_key | ||
| fi |
There was a problem hiding this comment.
lambda-entrypoint.sh is called as the ENTRYPOINT, which is executed when the image is started.
Added this section, where it reads the .api_access_key file into the API_ACCESS_KEY environment variable. Then removes the file
| // Fix CORS error: https://github.com/aws/aws-lambda-runtime-interface-emulator/pull/84/files#diff-2c1a36d379bd5ed6e893749912ac8473cc46dddf5765024d46b44da2eb56348d | ||
| if origin := r.Header.Get("Origin"); origin != "" { | ||
| w.Header().Set("Access-Control-Allow-Origin", GetenvWithDefault("ACCESS_CONTROL_ALLOW_ORIGIN", origin)) | ||
| w.Header().Set("Access-Control-Allow-Methods", GetenvWithDefault("ACCESS_CONTROL_ALLOW_METHODS", "POST, OPTIONS")) | ||
| w.Header().Set("Access-Control-Allow-Headers", GetenvWithDefault("ACCESS_CONTROL_ALLOW_HEADERS", "*")) | ||
| w.Header().Set("Access-Control-Allow-Credentials", GetenvWithDefault("ACCESS_CONTROL_ALLOW_CREDENTIALS", "true")) | ||
| } | ||
| if r.Method == "OPTIONS" { | ||
| w.WriteHeader(200) | ||
| return | ||
| } |
There was a problem hiding this comment.
Fixes CORS error, allows all origin
| w.Write(invokeResp.Body) | ||
| time.Sleep(100 * time.Millisecond) | ||
| //initDone = false | ||
| doneCallback(invokeResp) // Call done after printEndReports |
| maxInvocations := -1 // -1 means unlimited invocations | ||
| // Get max invocations from environment variable | ||
| maxInvocationsStr := os.Getenv("AWS_LAMBDA_SERVER_MAX_INVOCATIONS") | ||
| if maxInvocationsStr != "" { | ||
| if maxInvocationsInt, err := strconv.Atoi(maxInvocationsStr); err == nil { | ||
| maxInvocations = maxInvocationsInt | ||
| } else { | ||
| log.Panicf("Invalid value for AWS_LAMBDA_SERVER_MAX_INVOCATIONS: %s", maxInvocationsStr) | ||
| } | ||
| } |
There was a problem hiding this comment.
Read the AWS_LAMBDA_SERVER_MAX_INVOCATIONS environment variable into the maxInvocations variable.
| } | ||
|
|
||
| // Channel to signal server shutdown | ||
| shutdownChan := make(chan struct{}) |
There was a problem hiding this comment.
Golang uses these channels to block code until the shutdownChan is triggered
| // Pass a channel | ||
| http.HandleFunc("/2015-03-31/functions/function/invocations", func(w http.ResponseWriter, r *http.Request) { | ||
| InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs) | ||
| InvokeHandler(w, r, sandbox.LambdaInvokeAPI(), bs, func(invokeResp *ResponseWriterProxy){ |
There was a problem hiding this comment.
This function is called at the end of the response
|
|
||
| // Create an API request to the URL in the "forward-response" header | ||
| client := &http.Client{} | ||
| req, err := http.NewRequest("POST", forwardURL, bytes.NewReader(apiPayloadJSON)) |
There was a problem hiding this comment.
if forward-response exists in the headers, send a POST request to the URL at the forward-response
| maxInvocations-- | ||
| if maxInvocations == 0 { | ||
| close(shutdownChan) | ||
| } |
There was a problem hiding this comment.
Decrement the maxInvocations, if ==0, then shut down the server
| go func() { | ||
| <-shutdownChan | ||
| log.Printf("Maximum invocations (%s) reached. Shutting down the server", maxInvocationsStr) | ||
| if err := srv.Shutdown(nil); err != nil { | ||
| log.Panic(err) | ||
| } | ||
| }() |
There was a problem hiding this comment.
if shutdownChan is closed, then srv.Shutdown(nil) is called to shut down the server
|
@PointlessUser please review |
No description provided.