AWS CloudTrail is a crucial governance and auditing tool for any company operating in the cloud. At its core, this service records all activity in your AWS environments in the form of CloudTrail Events, which are specific API requests that can originate from:
- Individual users, moving around the AWS Management Console and reviewing or changing things.
- Other AWS services, as varied as EC2 instance creation due to an automatic EC2 Auto Scaling event, scheduled Fargate tasks running in an ECS cluster, or a new log stream being created in CloudWatch due to a recent Lambda function execution.
- Programmatic access using CLI and SDKs from any location in the world.
Tracking these events has countless benefits:
- Easily audit your account activity since it provides a centralized collection of logs you can view, search, analyze, and use for incident response.
- Satisfy regulatory compliance requirements by allowing you to store account activity for many years.
- Optimize your security and access management controls by applying permission restrictions based on usage evidence.
- Improve your operational excellence by allowing you to track account changes and their impact on your business workloads.
For all these reasons, it is fundamental to understand and know how to leverage this service. And that is why we will cover its main characteristics and explore some relevant use cases in this article.
A plethora of integrations
What kind of activity does it record? That is a fair question to ask. AWS CloudTrail is capable of recording API calls to several AWS services. So many of them that it is easier to list unsupported services:
- AWS Import/Export.
- Amazon VPC endpoint policy-specific events.
This means that CloudTrail will include requests made to API endpoints from Amazon EC2, API Gateway, CloudWatch, S3, and Secrets Manager, among many others. You can get a detailed list of all the services supported in the AWS CloudTrail user guide.
Besides being able to log events from all those services, plenty of AWS services use CloudTrail records under the hood to provide you with several useful features. Here are some of these benefits, only to name a few:
- Trigger Amazon EventBridge events whenever specific API calls occur.
- Organize multi-account activity records using AWS Organizations.
- Understand resource modifications by linking CloudTrail events from the AWS Config dashboard.
- Investigate security incidents through the Amazon Detective integration.
But how does CloudTrail work? And how can I use it for my governance and auditing purposes? We will now learn about the workflow and concepts that make up this essential tool.
Of events, trails, and lakes
Let’s start by taking a look at the service dashboard in the AWS Console. If you go there, you will immediately encounter a list of events already recorded by the service.
Events: Types and Anatomy
We already covered the definition of an Event as the record of a specific AWS API call received by your account, stored in JSON format. There are three types of events: management, data, and insights.
For example, a CreateLogStream management event would look like this:
Besides showing you which API call was actually executed (the Event Name), it gives you all sorts of details regarding who ****is executing the API call (the userIdentity field), the parameters of the call (requestParameters field), the response (responseElements, in this case empty), and many other specific values about the overall context and conditions of the request. Notice also that the eventCategory field specifies the type of event.
Here’s another example, this time for an Insights event:
In this case, the insightDetails field contains the nature of the unusual activity (insightType, in this case ApiCallRateInsight), and the difference between the baseline and the measured value in the insightContext. Additional contextual information is also provided in other fields.
A collection of events that are captured together and delivered to a common destination (S3 bucket, CloudWatch Logs or Amazon Eventbridge) is called a Trail. By default, AWS provides you with an initial trail storing up to 90 days of management events. And yes, the events from the Dashboard home belong to this initial trail.
But you can also create your own trails from the Trails section in the CloudTrail dashboard, or using the AWS CLI. Upon creation, you can specify the following trail attributes:
- S3 bucket that will hold the event files
- Optional encryption configuration using a KMS key
- Whether to enable log digest files to validate the integrity of the files
- SNS notification for every log file creation
- CloudWatch Logs integration
- Tags for the trail resource
- And most importantly, the type of events to collect in the trail.
Trails can also be multi-Regional, single-Regional or organizational. AWS recommends always using multi-Regional trails for a comprehensive audit of your account, as well as organizational trails for multi-account workloads by integrating with the AWS Organizations offering.
Since trail events can be delivered to S3 and CloudWatch Logs, you can leverage offerings like Amazon Athena or CloudWatch Logs metric filters to inspect and inspect your account’s activity. But you can also use CloudTrail Lake, a service that allows you to aggregate CloudTrail events into event data stores, and analyze them using SQL-based queries. This keeps you from implementing external data processing pipelines, as you can now process all audit information within one service. You can even add events from sources outside AWS, like Github, Okta, Snyk, or even your own on-premises applications.
Some Real World Applications
Monitor console login activity with CloudWatch Alarms
A classical example for CloudTrail usage is to monitor for login activity inside your AWS account. Imagine a user logs into the AWS Console. This will generate a Cloudtrail management event named ConsoleLogin. This event will contain useful contextual information about the incident, such as the user name, user agent, IP address, and even if MFA authentication was in place for the login.
In order to monitor this type of activity, you can create a trail that captures management events, sends them to an S3 bucket as well as CloudWatch Logs, and use CloudWatch Logs metric filters to look for events containing the ConsoleLogin name and create a custom metric. Using this metric, you can trigger a CloudWatch alarm with an SNS target to notify you whenever someone accesses the console.
Monitor suspicious account activity with Amazon Eventbridge
You can also use Amazon Eventbridge to automate responses to specific events captured by CloudTrail. Say for example that you have created a trail recording CloudTrail Insights events, and you wish to receive a Slack message whenever unusual activity is detected.
All you need to do is create an Eventbridge Rule having an AWS Cloudtrail event as source, and search for AWS Insight events. This would be the rule’s event pattern:
Once a target action is configured and this rule is enabled, you will be able to automate your response.
Now you know the benefits of using AWS CloudTrail in your accounts, but how much will it cost you? Like with most AWS services, you pay only for what you use.
You get your automatic first trail for free, recording 90 days of management events across your account. Should you create additional trails, there might be fees per event delivered to Amazon S3. Your first copy of management events are delivered for free, additional deliveries are charged USD$2.00 per 100,000 events. In the case of data events, there’s no free tier available, and you always pay USD$0.10 per 100,000 events.
If you enable CloudTrail Insights, you will be charged USD$0.35 per 100,000 events analyzed.
Essentially, your CloudTrail costs will depend on the amount of activity your account has, and on whether you’d like to measure it or not. You could get away monitoring management activity for free, but would have to increase your payments if you desire to create additional copies of the events, or start tracking data events.
Finally, for CloudTrail Lake, there’s a free trial period, valid until 30 days have passed or a 5 GB limit of ingested or scanned data is reached. After that, you have ingest and storage fee tiers ranging between USD$2.5 and USD$0.5 per GB depending on the amount of TB stored, and an additional scanning fee of $0.005 per GB of data analyzed.
You can find the latest information regarding CloudTrail pricing, as well as some useful examples, in the AWS CloudTrail Pricing page.
CloudTrail Best practices
Here are some useful recommendations when to help you secure your CloudTrail assets.
- Deliver all CloudTrail logs to a central S3 bucket with strong access control. Ideally in another AWS account, using a restrictive S3 resource policy.
- Prevent accidental loss of CloudTrail data by enabling versioning and MFA-delete on the S3 bucket.
- Encrypt your CloudTrail files enabling SSE-KMS encryption in your bucket.
- Always enable log file validation for your files, in order to prevent tampering.
AWS CloudTrail is a governance and auditing tool that records all activity in your AWS accounts, in the form of CloudTrail Events. By leveraging its integrations with AWS services like Eventbridge, CloudWatch, among many others, this service allows you to easily audit account activity, satisfy regulatory compliance requirements, automate security and access management controls, and improve your operational excellence.You can decide to record management events to keep track of your account activity as a whole, specific data events for monitoring changes to your data plane, or insights events for responding to unusual activity in your account.The costs of using this service will depend on the number of events recorded and analyzed. Finally, it is a security best practice to restrict access to your CloudTrail logs bucket, as well as protecting the availability and integrity of your event records.