从AWS Lambda发布到SNS时超时
我正在尝试通过Lambda函数调用将一些数据发布到SNS,但似乎不起作用.我的功能代码是-
I'm trying to publish some data to SNS from a Lambda function call, but it doesn't seem to be working. 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;
}
}
此调用导致超时(10秒),并且lambda调用失败.如果我注释掉SNS发布部分,即,如果我仅记录从DynamoDB接收到的数据,则可以正常工作.添加SNS发布代码后,它就会超时.
This call results in a timeout (10 seconds) and the lambda invocation fails. If I comment out the SNS publishing part, i.e., if I just log the data received from DynamoDB, it works just fine. As soon as the SNS publishing code is added, it times out.
CloudWatch中记录的超时消息为-
The timeout message logged in CloudWatch is -
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
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
我拥有所有适当的权限,并且仅使用在PC上运行的以下代码即可发布到SNS-
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 -
PublishRequest publishRequest = new PublishRequest(SNS_TOPIC_ARN, data);
PublishResult publishResult = snsClient.publish(publishRequest);
System.out.println("Publish Successful " + publishResult.getMessageId());
我还尝试使用AmazonSNSAsyncClient
代替AmazonSNSClient
,它给出的结果相同.
I have also tried using AmazonSNSAsyncClient
instead of AmazonSNSClient
, it gives the same result.
我在这里想念什么?
好吧,因为对我的问题发表评论的人都没有回答,所以我自己回答.
Well, since none of the people who commented on my question are answering, I'll answer it myself.
将内存使用量增加到256 MB,超时增加到30秒似乎已经解决了这个问题.
Increasing the memory usage to 256 MB and timeout to 30 seconds seems to have solved the issue.