8000 Compatibility w/ AWS Fargate by w3ichen · Pull Request #1 · MecSimCalc/aws-lambda-runtime-interface-emulator · GitHub
[go: up one dir, main page]

Skip to content

Compatibility w/ AWS Fargate #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 31 commits into
base: develop
Choose a base branch
from
Open

Compatibility w/ AWS Fargate #1

wants to merge 31 commits into from

Conversation

w3ichen
Copy link
@w3ichen w3ichen commented Aug 14, 2023

No description provided.

README.md Outdated
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update readme:

  • Set AWS_LAMBDA_SERVER_MAX_INVOCATIONS to 1 to exit server after one invocation
  • API_ACCESS_KEY is token used to access api
  • forward-response is header to forward response to

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Binary built files

Comment on lines 10 to 14
# 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +84 to +94
// 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
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes CORS error, allows all origin

time.Sleep(100 * time.Millisecond)
//initDone = false
doneCallback(invokeResp) // Call done after printEndReports
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end, call doneCallback

Comment on lines +26 to +35
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)
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read the AWS_LAMBDA_SERVER_MAX_INVOCATIONS environment variable into the maxInvocations variable.

}

// Channel to signal server shutdown
shutdownChan := make(chan struct{})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if forward-response exists in the headers, send a POST request to the URL at the forward-response

Comment on lines +84 to +87
maxInvocations--
if maxInvocations == 0 {
close(shutdownChan)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decrement the maxInvocations, if ==0, then shut down the server

Comment on lines +93 to +99
go func() {
<-shutdownChan
log.Printf("Maximum invocations (%s) reached. Shutting down the server", maxInvocationsStr)
if err := srv.Shutdown(nil); err != nil {
log.Panic(err)
}
}()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if shutdownChan is closed, then srv.Shutdown(nil) is called to shut down the server

@w3ichen w3ichen requested a review from PointlessUser August 14, 2023 23:08
@w3ichen
Copy link
Author
w3ichen commented Aug 14, 2023

@PointlessUser please review

Copy link
@PointlessUser PointlessUser left a comment

Ch F438 oose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0