简体   繁体   English

AWS Lambda Java功能成功但超时

[英]AWS Lambda Java Function is successful but times out

I created a basic AWS Lambda java function to convert an xml message to json. 我创建了一个基本的AWS Lambda java函数,将xml消息转换为json。 The function is triggered off of an S3 event (message is converted and dropped in different S3 bucket). 该功能是从S3事件触发的(消息已转换并放入不同的S3存储桶中)。 It appears to be successful, I can see the steps in cloudwatch and the converted message is in the S3 destination bucket. 看来成功了,我可以在cloudwatch中看到这些步骤,并且转换后的消息在S3目标存储桶中。 However, I see the timeout warning in the cloud watch logs (set timeout to 15 seconds). 但是,我在云监视日志中看到超时警告(将超时设置为15秒)。 I must be missing something, definitely a newby when it comes to Lambda. 我一定想念一些东西,绝对是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. JVM的冷启动时间必须比我想象的要长。 I was just assuming I had another issue in my code. 我只是假设我的代码中还有另一个问题。 Bumped memory to 192 and timeout to 45 seconds. 内存增加到192,超时达到45秒。 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. 真不幸,有人给我指的是错误的信息(NodeJS)而不是Java,这使我感到很沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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