AWS Lambda Java功能成功但超时

时间:2017-06-16 14:59:39

标签: java amazon-web-services amazon-s3 aws-lambda

我创建了一个基本的AWS Lambda java函数来将xml消息转换为json。该功能由S3事件触发(消息在不同的S3存储桶中转换和丢弃)。它似乎是成功的,我可以看到cloudwatch中的步骤,转换后的消息在S3目标桶中。但是,我在云监视日志中看到超时警告(将超时设置为15秒)。我必须遗漏一些东西,当谈到Lambda时肯定是个新手。我是否需要提供完成或成功的上下文?任何建议或提示将不胜感激。

代码段:

    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());
    }
}

1 个答案:

答案 0 :(得分:2)

事实证明,这只是暂停时间太短。 JVM的冷启动时间必须比我想象的要长。我只是假设我的代码中有另一个问题。将内存转移到192并超时到45秒。希望这有助于其他人。

真的很不幸我被一个指向错误信息(NodeJS)而不是Java的人标记了。