AWS Lambda:一个 Lambda 函数可以同时具有 Kinesis 和 DynamoDB Streams 的事件源吗?

AWS Lambda: can one Lambda function have Event Sources of both Kinesis and DynamoDB Streams?

一个 AWS Lambda 函数可以有两个事件源,一个 Kinesis 流和一个 DynamoDB 流吗?

我已经看过,但没有找到任何说明我可以或不能为同一 AWS Lambda 函数使用不同类型的事件源的文档。

是的,lambda 函数可以有不同类型的事件源,但您必须使用 com.amazonaws.services.lambda.runtime.RequestStreamHandler 才能正确反序列化输入数据。这是因为调用 com.amazonaws.services.lambda.runtime.RequestHandler 的内部 lambda 代码不会根据类型动态反序列化数据,然后调用具有正确类型的重载方法,而是似乎反射性地选择一个方法并调用它。

示例输入:

运动事件输入:

{
    "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 流记录:

{
    "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"
        }
    ]
}

示例代码:

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

示例日志:

2018-02-12 01:32:57 INFO (main) com.example.lambda.eventsourcetest.MultipleEventSourcesRequestHandler - In KinesisEvent handler with event of source: aws:dynamodb