|
| 1 | +# AWS Powertools for AWS Lambda .NET - Kafka Protobuf Example |
| 2 | + |
| 3 | +This project demonstrates how to use AWS Lambda Powertools for .NET with Amazon MSK (Managed Streaming for Kafka) to process events from Kafka topics. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This example showcases a Lambda functions that consume messages from Kafka topics with Protocol Buffers serialization format. |
| 8 | + |
| 9 | +It uses the `AWS.Lambda.Powertools.Kafka.Protobuf` NuGet package to easily deserialize and process Kafka records. |
| 10 | + |
| 11 | +## Project Structure |
| 12 | + |
| 13 | +```bash |
| 14 | +examples/Kafka/Protobuf/src/ |
| 15 | +├── Function.cs # Entry point for the Lambda function |
| 16 | +├── aws-lambda-tools-defaults.json # Default argument settings for AWS Lambda deployment |
| 17 | +├── template.yaml # AWS SAM template for deploying the function |
| 18 | +├── CustomerProfile.proto # Protocol Buffers definition file for the data structure used in the Kafka messages |
| 19 | +└── kafka-protobuf-event.json # Sample Protocol Buffers event to test the function |
| 20 | +``` |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- [Dotnet](https://dotnet.microsoft.com/en-us/download/dotnet) (dotnet8 or later) |
| 25 | +- [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) |
| 26 | +- [AWS CLI](https://aws.amazon.com/cli/) |
| 27 | +- An AWS account with appropriate permissions |
| 28 | +- [Amazon MSK](https://aws.amazon.com/msk/) cluster set up with a topic to consume messages from |
| 29 | +- [AWS.Lambda.Powertools.Kafka.Protobuf](https://www.nuget.org/packages/AWS.Lambda.Powertools.Kafka.Protobuf/) NuGet package installed in your project |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +1. Clone the repository: |
| 34 | + |
| 35 | + ```bash |
| 36 | + git clone https://github.com/aws-powertools/powertools-lambda-dotnet.git |
| 37 | + ``` |
| 38 | + |
| 39 | +2. Navigate to the project directory: |
| 40 | + |
| 41 | + ```bash |
| 42 | + cd powertools-lambda-dotnet/examples/Kafka/Protobuf/src |
| 43 | + ``` |
| 44 | + |
| 45 | +3. Build the project: |
| 46 | + |
| 47 | + ```bash |
| 48 | + dotnet build |
| 49 | + ``` |
| 50 | + |
| 51 | +## Deployment |
| 52 | + |
| 53 | +Deploy the application using the AWS SAM CLI: |
| 54 | + |
| 55 | +```bash |
| 56 | +sam build |
| 57 | +sam deploy --guided |
| 58 | +``` |
| 59 | + |
| 60 | +Follow the prompts to configure your deployment. |
| 61 | + |
| 62 | +## Protocol Buffers Format |
| 63 | + |
| 64 | +The Protobuf example handles messages serialized with Protocol Buffers. The schema is defined in a `.proto` file (which would need to be created), and the C# code is generated from that schema. |
| 65 | + |
| 66 | +This requires the `Grpc.Tools` package to deserialize the messages correctly. |
| 67 | + |
| 68 | +And update the `.csproj` file to include the `.proto` files. |
| 69 | + |
| 70 | +```xml |
| 71 | +<Protobuf Include="CustomerProfile.proto"> |
| 72 | + <GrpcServices>Client</GrpcServices> |
| 73 | + <Access>Public</Access> |
| 74 | + <ProtoCompile>True</ProtoCompile> |
| 75 | + <CompileOutputs>True</CompileOutputs> |
| 76 | + <OutputDir>obj\Debug/net8.0/</OutputDir> |
| 77 | + <Generator>MSBuild:Compile</Generator> |
| 78 | + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |
| 79 | +</Protobuf> |
| 80 | +``` |
| 81 | + |
| 82 | +## Usage Examples |
| 83 | + |
| 84 | +Once deployed, you can test the Lambda function by sending a sample Protocol Buffers event to the configured Kafka topic. |
| 85 | +You can use the `kafka-protobuf-event.json` file as a sample event to test the function. |
| 86 | + |
| 87 | +### Testing |
| 88 | + |
| 89 | +You can test the function locally using the AWS SAM CLI (Requires Docker to be installed): |
| 90 | + |
| 91 | +```bash |
| 92 | +sam local invoke ProtobufDeserializationFunction --event kafka-protobuf-event.json |
| 93 | +``` |
| 94 | + |
| 95 | +This command simulates an invocation of the Lambda function with the provided event data. |
| 96 | + |
| 97 | +## How It Works |
| 98 | + |
| 99 | +1. **Event Source**: Configure your Lambda functions with an MSK or self-managed Kafka cluster as an event source. |
| 100 | +2. **Deserializing Records**: Powertools handles deserializing the records based on the specified format. |
| 101 | +3. **Processing**: Each record is processed within the handler function. |
| 102 | + |
| 103 | +## Event Deserialization |
| 104 | + |
| 105 | +Pass the `PowertoolsKafkaProtobufSerializer` to the `[assembly: LambdaSerializer(typeof(PowertoolsKafkaProtobufSerializer))]`: |
| 106 | + |
| 107 | +```csharp |
| 108 | +[assembly: LambdaSerializer(typeof(PowertoolsKafkaProtobufSerializer))] |
| 109 | + ``` |
| 110 | + |
| 111 | +## Configuration |
| 112 | + |
| 113 | +The SAM template (`template.yaml`) defines three Lambda function: |
| 114 | + |
| 115 | +- **ProtobufDeserializationFunction**: Handles Protobuf-formatted Kafka messages |
| 116 | + |
| 117 | +## Customization |
| 118 | + |
| 119 | +To customize the examples: |
| 120 | + |
| 121 | +1. Modify the schema definitions to match your data structures |
| 122 | +2. Update the handler logic to process the records according to your requirements |
| 123 | +3. Ensure you have the proper `.proto` files and that they are included in your project for Protocol Buffers serialization/deserialization. |
| 124 | + |
| 125 | +## Resources |
| 126 | + |
| 127 | +- [AWS Lambda Powertools for .NET Documentation](https://docs.powertools.aws.dev/lambda/dotnet/) |
| 128 | +- [Amazon MSK Documentation](https://docs.aws.amazon.com/msk/) |
| 129 | +- [AWS Lambda Developer Guide](https://docs.aws.amazon.com/lambda/) |
| 130 | +- [Protocol Buffers Documentation](https://developers.google.com/protocol-buffers) |
0 commit comments