I am trying to run a kotlin based lambda with GraalVM native image. The lambda executes indefinitely. I am attaching my bootstrap file along with the code and the response from AWS Lambda execution. I am able to receive the input and send back response but its not stoping execution after getting the response
Bootstrap file
#!/bin/sh
set -euo pipefail
echo "Bootstrap script started" >&2
# Function to log messages
log_message() {
echo "$(date): $1" >&2
}
# Function to handle errors
handle_error() {
local exit_status=$1
local error_message=$2
local request_id=$3
log_message "Error occurred: $error_message (Exit status: $exit_status)"
ERROR_URL="http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$request_id/error"
ERROR="{"errorMessage": "$error_message", "errorType": "RuntimeError", "stackTrace": ["Exit status: $exit_status"]}"
curl -s -X POST "$ERROR_URL" -d "$ERROR" --header "Lambda-Runtime-Function-Error-Type: RuntimeError"
}
RUNTIME_API="http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime"
while true; do
log_message "Waiting for next invocation"
NEXT_INVOCATION_URL="$RUNTIME_API/invocation/next"
# Fetch headers and body separately
HEADERS=$(curl -sI "$NEXT_INVOCATION_URL")
EVENT_DATA=$(curl -s "$NEXT_INVOCATION_URL")
# Parse Request ID more robustly
REQUEST_ID=$(echo "$HEADERS" | grep -i Lambda-Runtime-Aws-Request-Id | cut -d' ' -f2 | tr -d 'rn' || echo "Unknown")
log_message "Received event data: $EVENT_DATA"
log_message "Request ID: $REQUEST_ID"
# Check if AWS-Lambda-Kotlin exists and is executable
if [ ! -x ./AWS-Lambda-Kotlin ]; then
handle_error 1 "AWS-Lambda-Kotlin not found or not executable" "$REQUEST_ID"
continue
fi
# Execute AWS-Lambda-Kotlin with error handling
log_message "Executing AWS-Lambda-Kotlin"
if ! RESPONSE=$(./AWS-Lambda-Kotlin "$EVENT_DATA" 2>&1); then
EXIT_STATUS=$?
handle_error $EXIT_STATUS "AWS-Lambda-Kotlin execution failed: $RESPONSE" "$REQUEST_ID"
continue
fi
log_message "AWS-Lambda-Kotlin execution completed successfully"
log_message "Response: $RESPONSE"
# Send the response back to Lambda
RESPONSE_URL="$RUNTIME_API/invocation/$REQUEST_ID/response"
log_message "Sending success response"
CURL_RESPONSE=$(curl -s -X POST "$RESPONSE_URL" -d "$RESPONSE" -w "%{http_code}")
log_message "Response sent, HTTP status: $CURL_RESPONSE"
if [ "$CURL_RESPONSE" != "202" ]; then
log_message "Warning: Unexpected status code when sending response"
fi
done
EOF
chmod +x build/native/nativeCompile/bootstrap
cd build/native/nativeCompile
chmod +x AWS-Lambda-Kotlin
My kotlin function
fun main(args: Array<String>) {
try {
println("Kotlin application started")
if (args.isEmpty()) {
println("No event data received")
return
}
val eventData = args[0]
println("Received event data: $eventData")
// Parse the event data
val gson = Gson()
val jsonEvent = gson.fromJson(eventData, JsonObject::class.java)
// Process the event (replace this with your actual processing logic)
val result = JsonObject()
result.addProperty("message", "Event processed successfully")
result.add("input", jsonEvent)
// Convert the result to a JSON string
val jsonResponse = gson.toJson(result)
// Print the JSON response (this will be captured by the bootstrap script)
println(jsonResponse)
} catch (e: Exception) {
val errorResponse = JsonObject()
errorResponse.addProperty("errorMessage", e.message)
errorResponse.addProperty("errorType", e.javaClass.simpleName)
val gson = Gson()
println(gson.toJson(errorResponse))
}
}
output from AWS Lambda
ed successfully","input":{"key1":"value1","key2":"value2","key3":"value3"}}
Sun Sep 8 13:36:23 UTC 2024: Sending success response
Sun Sep 8 13:36:23 UTC 2024: Response sent, HTTP status: {"errorMessage":"Invalid request ID","errorType":"InvalidRequestID"}
400
Sun Sep 8 13:36:23 UTC 2024: Warning: Unexpected status code when sending response
Sun Sep 8 13:36:23 UTC 2024: Waiting for next invocation
Sun Sep 8 13:36:23 UTC 2024: Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
Sun Sep 8 13:36:23 UTC 2024: Request ID: Unknown
Sun Sep 8 13:36:23 UTC 2024: Executing AWS-Lambda-Kotlin
Sun Sep 8 13:36:23 UTC 2024: AWS-Lambda-Kotlin execution completed successfully
Sun Sep 8 13:36:23 UTC 2024: Response: Kotlin application started
Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
{
"message": "Event processed successfully",
"input": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
Sun Sep 8 13:36:23 UTC 2024: Sending success response
Sun Sep 8 13:36:23 UTC 2024: Response sent, HTTP status: {"errorMessage":"Invalid request ID","errorType":"InvalidRequestID"}
400
Sun Sep 8 13:36:23 UTC 2024: Warning: Unexpected status code when sending response
Sun Sep 8 13:36:23 UTC 2024: Waiting for next invocation
Sun Sep 8 13:36:23 UTC 2024: Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
Sun Sep 8 13:36:23 UTC 2024: Request ID: Unknown
Sun Sep 8 13:36:23 UTC 2024: Executing AWS-Lambda-Kotlin
Sun Sep 8 13:36:23 UTC 2024: AWS-Lambda-Kotlin execution completed successfully
Sun Sep 8 13:36:23 UTC 2024: Response: Kotlin application started
Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
{
"message": "Event processed successfully",
"input": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
Sun Sep 8 13:36:23 UTC 2024: Sending success response
Sun Sep 8 13:36:23 UTC 2024: Response sent, HTTP status: {"errorMessage":"Invalid request ID","errorType":"InvalidRequestID"}
400
Sun Sep 8 13:36:23 UTC 2024: Warning: Unexpected status code when sending response
Sun Sep 8 13:36:23 UTC 2024: Waiting for next invocation
Sun Sep 8 13:36:23 UTC 2024: Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
Sun Sep 8 13:36:23 UTC 2024: Request ID: Unknown
Sun Sep 8 13:36:23 UTC 2024: Executing AWS-Lambda-Kotlin
Sun Sep 8 13:36:23 UTC 2024: AWS-Lambda-Kotlin execution completed successfully
Sun Sep 8 13:36:23 UTC 2024: Response: Kotlin application started
Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
{
"message": "Event processed successfully",
"input": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
Sun Sep 8 13:36:23 UTC 2024: Sending success response
Sun Sep 8 13:36:23 UTC 2024: Response sent, HTTP status: {"errorMessage":"Invalid request ID","errorType":"InvalidRequestID"}
400
Sun Sep 8 13:36:23 UTC 2024: Warning: Unexpected status code when sending response
Sun Sep 8 13:36:23 UTC 2024: Waiting for next invocation
Sun Sep 8 13:36:23 UTC 2024: Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
Sun Sep 8 13:36:23 UTC 2024: Request ID: Unknown
Sun Sep 8 13:36:23 UTC 2024: Executing AWS-Lambda-Kotlin
Sun Sep 8 13:36:23 UTC 2024: AWS-Lambda-Kotlin execution completed successfully
Sun Sep 8 13:36:23 UTC 2024: Response: Kotlin application started
Received event data: {"key1":"value1","key2":"value2","key3":"value3"}
{
"message": "Event processed successfully",
"input": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
Sun Sep 8 13:36:23 UTC 2024: Sending success response
Sun Sep 8 13:36:23 UTC 2024: Response sent, HTTP status: {"errorMessage":"Invalid request ID","errorType":"InvalidRequestID"}
400
Sun Sep 8 13:36:23 UTC 2024: Warning: Unexpected status code when sending response
2024-09-08T13:36:23.310Z b0ae1e27-cc7d-45eb-bba2-f686edd70929 Task timed out after 60.06 seconds
END RequestId: b0ae1e27-cc7d-45eb-bba2-f686edd70929
REPORT RequestId: b0ae1e27-cc7d-45eb-bba2-f686edd70929 Duration: 60064.15 ms Billed Duration: 60054 ms Memory Size: 1080 MB Max Memory Used: 40 MB Init Duration: 53.31 ms
2
You have
while true; do
So even though it is sending a response, the loop never terminates. All the log entries come from the statements that are within that loop, so either you need to remove the loop or make it be based on some condition that won’t always be true.
Check this for detail on a bash while loop. Also see:
- https://www.cyberciti.biz/faq/bash-infinite-loop/
Also, if your lambda is in a VPC, make sure it has network connectivity to the outside. The logs seem to show a 400 response, so check this article for details on enabling internet access for VPC-connected lambdas if that applies to your case.
2