简体   繁体   English

从AWS Lambda发布到SNS时超时

[英]Timeout when publishing from AWS Lambda to SNS

I'm trying to publish some data to SNS from a Lambda function call, but it doesn't seem to be working. 我正在尝试从Lambda函数调用向SNS发布一些数据,但它似乎不起作用。 My function code is - 我的功能代码是 -

public class Handler implements RequestHandler<DynamodbEvent, Void> {

    private static final String SNS_TOPIC_ARN = "arn:aws:sns:us-west-2:account_number:function_name";

    @Override
    public Void handleRequest(DynamodbEvent dynamodbEvent, Context context) {

        LambdaLogger logger = context.getLogger();

        AmazonSNSClient snsClient = new AmazonSNSClient(new DefaultAWSCredentialsProviderChain());
        snsClient.setRegion(Region.getRegion(Regions.US_WEST_2));

        for (DynamodbStreamRecord record : dynamodbEvent.getRecords()) {
            Map<String, AttributeValue> newImage = record.getDynamodb().getNewImage();

            if (newImage == null) {
                continue;
            }

            String sensorId = newImage.get("sensorID").getS();
            long timestamp = Long.parseLong(newImage.get("timestamp").getS());
            double temperature = Double.parseDouble(newImage.get("payload").getM().get("temp").getN());

            String data = sensorId + " " + timestamp + " " + temperature;

            logger.log(data);

            PublishRequest publishRequest = new PublishRequest(SNS_TOPIC_ARN, data);

            PublishResult publishResult = snsClient.publish(publishRequest);

            logger.log("Publish Successful " + publishResult.getMessageId());
        }

        snsClient.shutdown();

        return null;
    }
}

This call results in a timeout (10 seconds) and the lambda invocation fails. 此调用导致超时(10秒)并且lambda调用失败。 If I comment out the SNS publishing part, ie, if I just log the data received from DynamoDB, it works just fine. 如果我注释掉SNS发布部分,即,如果我只记录从DynamoDB收到的数据,它就可以正常工作。 As soon as the SNS publishing code is added, it times out. 一旦添加了SNS发布代码,它就会超时。

The timeout message logged in CloudWatch is - CloudWatch中记录的超时消息是 -

START RequestId: 8db74187-459b-42c5-8a06-b3a74873b236 Version: $LATEST END RequestId: 8db74187-459b-42c5-8a06-b3a74873b236 REPORT RequestId: 8db74187-459b-42c5-8a06-b3a74873b236 Duration: 10001.66 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 37 MB Task timed out after 10.00 seconds

I have all the proper permissions in place, and I'm able to publish to SNS using just the following code running on my PC - 我已经拥有所有适当的权限,并且我只能使用在我的PC上运行的以下代码发布到SNS -

PublishRequest publishRequest = new PublishRequest(SNS_TOPIC_ARN, data);
PublishResult publishResult = snsClient.publish(publishRequest);
System.out.println("Publish Successful " + publishResult.getMessageId());

I have also tried using AmazonSNSAsyncClient instead of AmazonSNSClient , it gives the same result. 我也尝试使用AmazonSNSAsyncClient而不是AmazonSNSClient ,它给出了相同的结果。

What am I missing here ? 我在这里错过了什么?

Well, since none of the people who commented on my question are answering, I'll answer it myself. 好吧,既然没有评论我的问题的人回答,我会自己回答。

Increasing the memory usage to 256 MB and timeout to 30 seconds seems to have solved the issue. 将内存使用量增加到256 MB并将超时时间增加到30秒似乎已经解决了这个问题。

增加为Lambda操作分配的内存量。

Increasing the memory allocated to the lambda function worked for me as well (this setting is in the lambda console under basic settings). 增加分配给lambda函数的内存也适用于我(此设置在基本设置下的lambda控制台中)。

This one was maddening for me - I was using the same JAR file for 2 different lambda functions with identical configurations, and the SNS call was working for one but timing out for the other. 这个对我来说很疯狂 - 我使用相同的JAR文件为2个不同的lambda函数配置相同的配置,并且SNS调用正在为一个工作,但是为另一个调整。 Maddening! 郁闷了!

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

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