Configuring CloudTrail with EventBridge

Manually setting AWS API calls to integrate with EventBridge

Ed Reinoso
4 min readAug 14, 2022

In this quick tutorial, I am going to walk you to how to configure CloudTrail with EventBridge from the AWS console directly.

This integration helps you create very useful event-driven systems where EventBridge could dynamically react to other AWS events through CloudTrail APIs.

EventBridge can easily integrate with many target services within AWS, with a fast daily growing expansion. The picture below represents some of the services EventBridge could work with.

AWS EventBridge service integration

The purpose of this post to manually configure EventBridge and CloudTrail when EC2 APIs are called. Furthermore, we are going to configure a Lambda function as target whenever the rule gets triggered.

This setup can be done in four steps:

  1. Rule details
  2. Build pattern
  3. Select targets
  4. Review and create

1. Rule Details

First thing would be to create a rule for the event. The name and description are required, as well as the Rule Type.

Rule name, description and type configuration

Rule Type

There are two different kind of Rule Types, one based on some event patterns, and another one based on schedule.

  1. Event Patterns: this type will match a specific event pattern. In this case, EC2 APIs that are captured through CloudTrail.
  2. Schedule: this type represents a cron expression for how often the rule should be triggered.
Rule type settings: event pattern and schedule

More about the cron expressions in AWS in the Reference section.

2. Build Pattern

Event Source

Second step is to define the source in which the event will come in. There are three different types: AWS events, others and all types.

  1. AWS events: these correspond to internal events that are happening inside of your AWS environment through APIs.
  2. Other: this could integrate with your internal system or another third party provider that generates events.
Event pattern options

Event Pattern

This step is to setup the pattern to match the event that is going to trigger the rule. On Event Source, there are AWS services and also EventBridge Partners.

For the purpose of this post, we are going to select API calls via CloudTrail, but there are also other types such as EBS Volume Notification and AMI State Change for EC2.

Event pattern integration with AWS CloudTrail

This is a JSON example of what event through API call via CloudTrail may look like.

Code 1: CloudTrail configuration to work with EventBridge on AttachVolume API calls

Notice that the source is “ec2,” meaning that only EC2 APIs will match this pattern and therefore use this rule. Furthermore, the “eventName” key can be any sort of EC2 API operation, in this case AttachVolume, but it could also RunInstances, TerminateInstance, DetachVolume to mention few.

Resources for creating event patterns can be found in the Reference section.

3. Select Targets

Targets are essentially the services EventBridge will invoke based on the event. As mentioned, there is a variety of AWS services that integrate with EventBridge. But you could also connect this with other external APIs that are not necessarily in AWS, like for example a payment system such as Stripe.

Lambda Function

When trying to configure EventBridge to work with Lambda, there are multiple settings that could be enabled in order to fully exploit the capabilities of both services.

You could configure a custom target input that is passed to the function. This custom target input could be extremely useful when moving key data points around the system in an event-driven fashion.

Target configuration with Lambda functions and custom input

Additionally, there are also other settings with regards to the retry policies, such as the maximum age of event, the number of attempts and the dead-letter queues.

Configurations

4. Review and Create

Once you have made sure that the rule has been configured correctly, with the appropriate event pattern and the right targets, then go ahead and create this and wait until the next event that matches your rule!

Let me know if I can help in anything. Subscribe if you want to see more of this content.

Go Build 🔥

--

--

Ed Reinoso
Ed Reinoso

Written by Ed Reinoso

DevOps engineer trying to make technology accessible

Responses (2)