[英]AWS Lambda: can one Lambda function have Event Sources of both Kinesis and DynamoDB Streams?
Can one AWS Lambda function have two Event Sources both of one Kinesis stream and one DynamoDB Stream?一个 AWS Lambda 函数是否可以有两个事件源,一个是 Kinesis 流,另一个是 DynamoDB 流?
I've looked but I haven't found any documentation which says that I can or cannot have different types of event sources for the same AWS Lambda function.我查看过,但没有找到任何文档说明我可以或不能为同一个 AWS Lambda 函数使用不同类型的事件源。
Yes a lambda functions can have event sources of different types but you must use the com.amazonaws.services.lambda.runtime.RequestStreamHandler
to be able to correctly deserialize the input data.是的,lambda 函数可以具有不同类型的事件源,但您必须使用com.amazonaws.services.lambda.runtime.RequestStreamHandler
才能正确反序列化输入数据。 This is because the internal lambda code that invokes com.amazonaws.services.lambda.runtime.RequestHandler
won't deserialize the data dynamically based on types then invoke an overloaded method with correct type, but instead seems to reflectively pick a method and invoke it.这是因为调用com.amazonaws.services.lambda.runtime.RequestHandler
的内部 lambda 代码不会根据类型动态反序列化数据,然后调用具有正确类型的重载方法,而是似乎反射性地选择一个方法并调用它.
Sample inputs:示例输入:
Kinesis Event input: Kinesis 事件输入:
{
"Records": [
{
"kinesis": {
"kinesisSchemaVersion": "1.0",
"partitionKey": "1",
"sequenceNumber": "11111111111111111111111111111111111111111111111111111111",
"data": "e30=",
"approximateArrivalTimestamp": 1518397399.55
},
"eventSource": "aws:kinesis",
"eventVersion": "1.0",
"eventID": "shardId-000000000000:11111111111111111111111111111111111111111111111111111111",
"eventName": "aws:kinesis:record",
"invokeIdentityArn": "arn:aws:iam::111111111111:role/lambda_test-lambda-multipe-sources",
"awsRegion": "us-east-1",
"eventSourceARN": "arn:aws:kinesis:us-east-1:111111111111:stream/test-lambda-multipe-sources"
}
]
}
DynamoDb Stream Record: DynamoDb 流记录:
{
"Records": [
{
"eventID": "11111111111111111111111111111111",
"eventName": "INSERT",
"eventVersion": "1.1",
"eventSource": "aws:dynamodb",
"awsRegion": "us-east-1",
"dynamodb": {
"ApproximateCreationDateTime": 1518397440,
"Keys": {
"key": {
"S": "asdf"
}
},
"NewImage": {
"key": {
"S": "asdf"
}
},
"SequenceNumber": "111111111111111111111111",
"SizeBytes": 14,
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
"eventSourceARN": "arn:aws:dynamodb:us-east-1:111111111111:table/test-lambda-multipe-sources/stream/2018-02-11T18:57:44.017"
}
]
}
Sample code:示例代码:
public final class MultipleEventSourcesRequestHandler
implements RequestHandler<KinesisEvent, Void>
// implements RequestStreamHandler
{
private static final Logger LOG = LoggerFactory.getLogger(MultipleEventSourcesRequestHandler.class);
public Void handleRequest(final DynamodbEvent input, final Context context)
{
LOG.info("In DynamodbEvent handler with event of source: " + input.getRecords().get(0).getEventSource());
return null;
}
// public final void handleRequest(final InputStream input, final OutputStream output, final Context context)
// throws IOException
// {
// final byte[] serializedSpeechletRequest = IOUtils.toByteArray(input);
// LOG.info("In Stream handler. Request bytes: " + new String(serializedSpeechletRequest, StandardCharsets.UTF_8));
// output.close();
// }
public Void handleRequest(final KinesisEvent input, final Context context)
{
LOG.info("In KinesisEvent handler with event of source: " + input.getRecords().get(0).getEventSource());
return null;
}
}
Sample logs:示例日志:
2018-02-12 01:32:57 INFO (main) com.example.lambda.eventsourcetest.MultipleEventSourcesRequestHandler - In KinesisEvent handler with event of source: aws:dynamodb
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.