AWS Lambda with GraalVM Bootstrap executes indefinitely

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật