简体   繁体   English

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

[英]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.

相关问题 如果AWS Lambda函数具有来自多个Kinesis流的事件源,那么传入记录的批处理将来自单个Kinesis流还是混合源? - If a AWS Lambda function has event sources from multiple Kinesis streams, will the batch of incoming records be from a single Kinesis stream or a mix? 我们可以编写一个 AWS Lambda 函数来查询 Kinesis Streams - Can we write an AWS Lambda function to query Kinesis Streams AWS Kinesis + Lambda函数:我可以在一个分片中同时调用一个Lambda函数的多个实例吗? - AWS Kinesis + Lambda Function: can I invoke multiple instances of one Lambda Function concurrently in one shard? AWS使用DynamoDB Lambda配置Kinesis Stream - AWS Configure Kinesis Stream with DynamoDB Lambda 将AWS Lambda配置为Dynamodb流的并行计算 - Configuring AWS Lambda to a parallel computations for Dynamodb Streams AWS Lambda 目标未通过 DynamoDB Streams 触发 - AWS Lambda destination not triggering with DynamoDB Streams 我们可以将 DynamoDb 和 VPC 都用于 aws lambda 吗? - Can we use both DynamoDb and VPC to an aws lambda? 触发AWS Lambda以进行特定的Kinesis事件推送 - Trigger AWS lambda for specific Kinesis event push 用于 CLI 事件、AWS Kinesis + Lambda 的 Python 中的 UnicodeDecodeError - UnicodeDecodeError in Python for CLI event, AWS Kinesis + Lambda AWS Lambda 函数写入 DynamoDB - AWS Lambda function write to DynamoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM