Exploring AWS CloudTrail: Auditing and Monitoring AWS API Activity

Christopher Adamson
6 min readNov 5, 2023

--

AWS CloudTrail is a service that enables governance, compliance, operational auditing, and risk auditing of your AWS account. It provides event history of your AWS account activity, including actions taken through the AWS Management Console, AWS SDKs, command line tools, and other AWS services.

What is CloudTrail?

CloudTrail continuously monitors and logs account activity across all AWS services, including actions taken by a user, role, or AWS service. The recorded information includes the identity of the API caller, the time of the API call, the source IP address of the API caller, the request parameters, and the response elements returned by the AWS service.

Why Use CloudTrail?

Here are some key reasons to use CloudTrail:

  • Audit Compliance: CloudTrail logs provide detailed records of all API calls, which can be used to audit compliance with regulatory standards like HIPAA and PCI.
  • Security Analysis: The API call logs can be analyzed to detect anomalous activity and unauthorized access to determine security issues.
  • Operational Issues: The activity history can help troubleshoot operational issues by pinpointing when an issue began and what actions were taken.
  • Resource Changes: You can identify what changes were made to AWS resources by viewing CloudTrail events.

CloudTrail Log Files

CloudTrail log files contain the history of API calls made on your account. These log files are stored in Amazon S3 buckets that you specify. You can define S3 buckets per region or use the same bucket for all regions.

The log files capture API activity from all Regions and are delivered every 5 minutes. You can easily search and analyze the logs using Amazon Athena, Amazon Elasticsearch, and other tools.

CloudTrail Events

CloudTrail categorizes events into two types:

  • Management events: Provides information about management operations that are performed on resources in your AWS account. These include operations like creating, modifying, and deleting resources.
  • Data events: Provides information about resource operations performed on or in a resource. These include operations like Amazon S3 object-level API activity.

You can choose to log both management and data events or just management events. Data events allow more granular visibility into resource access.

Enabling CloudTrail

Enabling CloudTrail is simple and can be done in a few steps:

  1. Sign into the AWS Management Console and open the CloudTrail console.
  2. Get started by creating a new trail and specify a name.
  3. Choose whether to log management and/or data events.
  4. Specify an existing S3 bucket or create a new one where logs will be stored.
  5. Click Create to finish enabling CloudTrail.

Once enabled, CloudTrail will begin recording events and delivering log files to the designated S3 bucket. You can customize trails further by adding tags, configuring log file validation, logging to CloudWatch Logs, and more.

Use Cases

Here are some common use cases for CloudTrail:

  • User Activity Monitoring: Review which users and accounts are performing actions across services.
  • Service Usage Optimization: Analyze usage patterns to identify opportunities to reduce costs.
  • Security Forensics: Investigate unusual activity when a security incident occurs by reviewing relevant events.
  • Regulatory Compliance: Meet compliance requirements that mandate detailed activity logging and audit trails.

CloudTrail provides a simple way to get visibility into account activity by recording API calls made across AWS. The event history and logs can be used for auditing, security analysis, troubleshooting, and more. Businesses of all sizes can benefit from enabling CloudTrail to gain insight into how their AWS resources are being accessed and modified.

Tutorial

AWS CloudTrail is a service that records AWS API calls for your account and delivers log files to you. The recorded information includes the identity of the API caller, the time of the API call, the source IP address of the API caller, the request parameters, and the response elements returned by the AWS service.

In this tutorial, we will walk through how to enable CloudTrail, view and analyze the log files, and leverage CloudTrail logs for auditing and security.

Prerequisites

Before starting, you should have:

  • An AWS account
  • Basic understanding of AWS services
  • An S3 bucket to store the CloudTrail logs

Enabling CloudTrail

Let’s start by enabling CloudTrail across all Regions:

  1. Go to the CloudTrail console in the AWS Management Console.
  2. Click “Trails” in the left sidebar and then “Create trail”.
  3. Enter a name for the trail such as “CloudTrail-AllRegions”.
  4. Under Storage location, create or select an existing S3 bucket.
  5. For log file encryption, select AWS KMS to encrypt the logs.
  6. Click “Create” to enable the trail.

CloudTrail will now begin recording events and sending log files to the designated S3 bucket.

Viewing CloudTrail Log Files

The log files can be viewed in the S3 bucket or analyzed using Athena, Elasticsearch, or other tools. Let’s take a look at the logs:

  1. Go to the S3 console and open the bucket storing the CloudTrail logs.
  2. Open one of the log files and inspect the JSON content.
  3. You will see API call details like source IP, user agent, resource affected, and parameters.

The logs provide a comprehensive audit trail of all API activity across services.

Using CloudTrail Insights

CloudTrail Insights detects unusual activity by continuously analyzing event patterns. Let’s enable it:

  1. From the CloudTrail console, go to “Trails” and select the trail.
  2. Under “Insights Events”, enable insights.
  3. In “Insights summary”, you can see detected anomalies.
  4. Click on events to see the anomalous activity details.

Insights makes it easy to identify potential security issues.

In this tutorial, we enabled CloudTrail across all Regions, viewed the generated log files, and enabled CloudTrail Insights. The event history and anomaly detection allow for auditing, operational analysis, security monitoring, and more. Be sure to leverage CloudTrail logs to gain visibility into your AWS account activity.

Common AWS CLI Commands for CloudTrail

Here are some common AWS CLI commands for working with AWS CloudTrail:

Create CloudTrail trail

Describe CloudTrail trail

Start CloudTrail logging

Stop CloudTrail logging

List CloudTrail events

Get CloudTrail log files

Delete CloudTrail trail

Additional CloudTrail CLI commands

There are many additional CloudTrail CLI commands available:

  • update-trail — Updates settings for a trail
  • list-tags — Lists tags for a trail
  • add-tags — Adds tags to a trail
  • remove-tags — Removes tags from a trail
  • list-public-keys — Lists public keys for log file validation
  • get-trail-status — Returns status of CloudTrail logging
  • list-trails — Lists trails in the account

Refer to the AWS CLI CloudTrail docs for more details.

Final Words

AWS CloudTrail provides a simple yet powerful way to gain visibility into activity across your AWS account. By recording API calls made to various AWS services, CloudTrail delivers detailed audit logs that can be analyzed for security, compliance, and operational purposes. This tutorial guided you through enabling CloudTrail across all Regions, inspecting the generated log files, and leveraging CloudTrail Insights to detect unusual activity. With CloudTrail activated, you now have comprehensive visibility into changes, user activity, and resource access within your AWS environment. Be sure to consult the CloudTrail logs regularly for auditing, monitoring AWS usage, troubleshooting issues, and investigating security incidents. We encourage you to explore the other capabilities of CloudTrail such as log file encryption, log validation, data event logging, and integrating logs with other AWS services. CloudTrail is a key component of the AWS shared responsibility model, enabling you to monitor the activity within your account and respond appropriately.

--

--