简体   繁体   中英

AWS Lambda Java Function is successful but times out

I created a basic AWS Lambda java function to convert an xml message to json. The function is triggered off of an S3 event (message is converted and dropped in different S3 bucket). It appears to be successful, I can see the steps in cloudwatch and the converted message is in the S3 destination bucket. However, I see the timeout warning in the cloud watch logs (set timeout to 15 seconds). I must be missing something, definitely a newby when it comes to Lambda. Do I need to provide the context with a done or success? Any suggestions or tips would be greatly appreciated.

Code Snippet:

    public void msgConvertToJson(S3Event s3event, Context context) {

    final String key = s3event.getRecords().get(0).getS3().getObject().getKey();
    final String bucketName = s3event.getRecords().get(0).getS3().getBucket().getName();
    log.info("key: " + key + " bucketName:" + bucketName);
    String action = "";


    try {

        //Attempt to retrieve the message from S3 on notification
        log.info("attempting to get message");
        action = "get";
        final String message = s3Client.getObjectAsString(bucketName, key);

        //Attempt to parse and convert the message to JSON
        log.info("attempting to parse message");
        String msgJson = new MessageParser(message).getMessageJson();

        //Attempt to write the converted message to a new S3 bucket
        final String parsedBucket = "parsed-" + bucketName;
        final String newKey = key.replace(".xml",".json");
        log.info("newKey: " + newKey + " parsedBucketName:" + parsedBucket);
        log.info("attempting to put message");
        action = "put";

        s3Client.putObject(parsedBucket, newKey, msgJson );

    } catch (AmazonServiceException ase) {
        log.error("Caught an AmazonServiceException trying to " + action +  " file " + key + ", which " +
                "means your request made it " +
                "to Amazon S3, but was rejected with an error response" +
                " for some reason.");
        log.error("Error Message:    " + ase.getMessage());
        log.error("HTTP Status Code: " + ase.getStatusCode());
        log.error("AWS Error Code:   " + ase.getErrorCode());
        log.error("Error Type:       " + ase.getErrorType());
        log.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        log.error("Caught an AmazonClientException while trying to " + action +  " file " + key + ", which " +
                "means the client encountered " +
                "an internal error while trying to " +
                "communicate with S3, " +
                "such as not being able to access the network.");
        log.error("Error Message: " + ace.getMessage());
    }
}

It actually turned out that it was just the timeout was too short. The cold start for the JVM, must be taking longer than I would have thought. I was just assuming I had another issue in my code. Bumped memory to 192 and timeout to 45 seconds. Hope this helps someone else.

Really unfortunate I was marked down by someone that was pointing me at the wrong information (NodeJS) instead of Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM