【问题标题】:How to process a DynamoDBEvent in a C# Lambda Function?如何在 C# Lambda 函数中处理 DynamoDBEvent?
【发布时间】:2018-10-09 11:17:07
【问题描述】:

我创建了一个由 DynamoDB 流触发的 C# Lambda 函数。它执行得很好。但是,NewImage 值的 StreamRecord 不返回任何值。计数为 0。我做错了什么?我已经检查了所有 AWS 文档,但这似乎越来越像一个错误。我下面的 lambda 函数应该可以工作,并且在我的示例中它应该返回至少 1 个 StreamRecord。 attributeMap.Count 总是返回 0 但它应该返回 1。

    public void FunctionHandler(DynamoDBEvent dynamoDbEvent, ILambdaContext context)
    {
        Console.WriteLine($"Beginning to process {dynamoDbEvent.Records.Count} records...");

        foreach (var record in dynamoDbEvent.Records)
        {
            Console.WriteLine($"Event ID: {record.EventID}");
            Console.WriteLine($"Event Name: {record.EventName}");

            var attributeMap = record.Dynamodb.NewImage;
            if (attributeMap.Count > 0) // If item does not exist, attributeMap.Count will be 0
            {
                Console.WriteLine(attributeMap["AccountId"].S);
            }
        }

        Console.WriteLine("Stream processing complete.");
    }

【问题讨论】:

    标签: c# aws-lambda amazon-dynamodb amazon-dynamodb-streams


    【解决方案1】:

    更新:2018 年 10 月 4 日。我不再使用管理应用程序。我现在专门使用 CloudFormation 来创建和维护所有内容,包括使用 CodePipelines 的完整 CI/CD 管道。这包括 .NET Core 2.1 中的所有无服务器 Lamba 函数。

    我想通了。糟糕的是,AWS Docs 对此只字未提。发现这一点很痛苦。对于此处可能需要此信息的其他任何人,您必须在为表创建 DynamoDB 流时设置流视图类型。这是 AWS 控制台的图片:

    但是,由于我通过管理控制台(在 C# Core 2.0 中)设置了所有表,因此我设置了表的设置方法,包括流规范和对 lambda 函数的事件源映射请求:

            var request = new CreateTableRequest
            {
                TableName = TABLE_CREATE_ACCOUNT,
                AttributeDefinitions = new List<AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "CommandId",
                        AttributeType = ScalarAttributeType.S
                    }
                },
                KeySchema = new List<KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "CommandId",
                        KeyType = KeyType.HASH
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits = 1,
                    WriteCapacityUnits = 1
                },
                StreamSpecification = new StreamSpecification
                {
                    StreamEnabled = true,
                    StreamViewType = StreamViewType.NEW_IMAGE
                }
            };
    
            try
            {
                var response = _db.CreateTableAsync(request);
                var tableDescription = response.Result.TableDescription;
                Console.WriteLine("{1}: {0} ReadCapacityUnits: {2} WriteCapacityUnits: {3}",
                    tableDescription.TableStatus,
                    tableDescription.TableName,
                    tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                    tableDescription.ProvisionedThroughput.WriteCapacityUnits);
                string status = tableDescription.TableStatus;
                Console.WriteLine(TABLE_CREATE_ACCOUNT + " - " + status);
                WaitUntilTableReady(TABLE_CREATE_ACCOUNT);
    
                // This connects the DynamoDB stream to a lambda function
                Console.WriteLine("Creating event source mapping between table stream '"+ TABLE_CREATE_ACCOUNT + "' and lambda 'ProcessCreateAccount'");
                var req = new CreateEventSourceMappingRequest
                {
                    BatchSize = 100,
                    Enabled = true,
                    EventSourceArn = tableDescription.LatestStreamArn,
                    FunctionName = "ProcessCreateAccount",
                    StartingPosition = EventSourcePosition.LATEST
                };
                var reqResponse =_lambda.CreateEventSourceMappingAsync(req);
                Console.WriteLine("Event source mapping state: " + reqResponse.Result.State);
            }
            catch (AmazonDynamoDBException e)
            {
                Console.WriteLine("Error creating table '" + TABLE_CREATE_ACCOUNT + "'");
                Console.WriteLine("Amazon error code: {0}", string.IsNullOrEmpty(e.ErrorCode) ? "None" : e.ErrorCode);
                Console.WriteLine("Exception message: {0}", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error creating table '" + TABLE_CREATE_ACCOUNT + "'");
                Console.WriteLine("Exception message: {0}", e.Message);
            }
    

    关键是

    StreamViewType = StreamViewType.NEW_IMAGE

    就是这样。

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2018-11-14
      • 2019-01-27
      • 1970-01-01
      相关资源
      最近更新 更多