AWS Lambda:一个Lambda函数可以包含Kinesis和DynamoDB Streams的事件源吗?

时间:2018-02-10 17:54:45

标签: amazon-web-services aws-lambda amazon-kinesis amazon-dynamodb-streams

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

我看过,但我找不到任何文档说我可以或不可以为同一个AWS Lambda函数提供不同类型的事件源。

1 个答案:

答案 0 :(得分:1)

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

示例输入:

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

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