DEV Community

Robert Slootjes
Robert Slootjes

Posted on

Logging EventBridge events to S3 with Firehose

When using EventBridge I always log all events to an S3 bucket for auditing, analytics and debugging purposes. A super easy method to do this is to create a Kinesis Data Firehose stream and create a rule that captures all events that points to the Firehose stream. The Firehose stream can then flush the events on S3 in an interval/size of choice based on configuration.

The issue I ran into is that all events in JSON were all on a single line while I prefer to have them separated by a newline. This is known as the JSON Lines format. I knew this would be possible by leveraging the Data Transformation feature of Firehose however this just didn't feel right (in other words, I didn't want to pay for it :-). Back when I was looking into this, I found a fix for it and posted it on Twitter. Since others might be unfamiliar with this solution I decided to create a post explaining this in more detail to make it easier to find than the thread on Twitter.

The solution turned out to be relatively simple in the end. You need to transform the input from EventBridge by configuring an Input Transformer on your rule. To modify the event to include a newline. As the Input path you should configure:

Enter fullscreen mode Exit fullscreen mode

and as Template you should configure:


Enter fullscreen mode Exit fullscreen mode

The trick is to add a newline after the <>.

As I highly recommend to use CloudFormation rather than doing things in the console:

  Type: AWS::Events::Rule
    EventBusName: !GetAtt EventBus.Name
        - Ref: 'AWS::AccountId'
    State: ENABLED
      - Id: FirehoseLogger
        Arn: !GetAtt FirehoseStream.Arn
        RoleArn: !GetAtt EventBridgeFirehoseDeliveryRole.Arn
          InputPathsMap: { }
          InputTemplate: |
# empty because of the newline ^
Enter fullscreen mode Exit fullscreen mode

Thanks to James for providing a CDK solution:

return new targets.KinesisFirehoseStream(kinesisStream, {
    message: {
        bind: () => ({inputPathsMap: {}, inputTemplate: '<>\n'}),
Enter fullscreen mode Exit fullscreen mode

The files written by Firehose on S3 will have every event separated by a newline:


Enter fullscreen mode Exit fullscreen mode

If you configure your S3 bucket with Object Lock you will have an immutable log of everything that happened in your application which is easily parsable.

To easily query events, you can of course store the events in a database of choice (DynamoDB, Elasticsearch, Postgres). To populate the database with batches of events, rather than per event, you can create a Lambda that triggers when a new log file is written by Firehose. You can then read the file from S3, split by newline and push the events to the database.

Top comments (0)