# Trail

Cloud trail allows you to log events that happen in your AWS account For example:.

import { CloudTrail } from 'aws-cdk-lib/aws-cloudtrail'

const cloudTrail = new CloudTrail(this, 'MyTrail');

Extends: Construct › Resource › Trail
Implements: IResource, IEnvironmentAware, IConstruct, IDependable

- CloudFormation: [`AWS::CloudTrail::Trail`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-cloudtrail-trail.html)
- L1 type: [`CfnTrail`](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudtrail.CfnTrail.html)
- Source: [`aws-cloudtrail/lib/cloudtrail.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-cloudtrail/lib/cloudtrail.ts#L211)
- Type last changed: v2.196.0 · 16 May 2025

## Example

```ts
import * as cloudtrail from 'aws-cdk-lib/aws-cloudtrail';

const myKeyAlias = kms.Alias.fromAliasName(this, 'myKey', 'alias/aws/s3');
const trail = new cloudtrail.Trail(this, 'myCloudTrail', {
  sendToCloudWatchLogs: true,
  encryptionKey: myKeyAlias,
});
```

## Initializer

```ts
new Trail(scope: Construct, id: string, props?: TrailProps)
```

## Construct Props

- `bucket`: [IBucket](/cdk/api/v2/docs/aws-cdk-lib.aws_s3.IBucket.html) — The Amazon S3 bucket.

  **Default:** if not supplied a bucket will be created with all the correct permissions

- `cloudWatchLogGroup`: [ILogGroupRef](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_logs.ILogGroupRef.html) — Log Group to which CloudTrail to push logs to.

  **Default:** a new log group is created and used.

  Ignored if sendToCloudWatchLogs is set to false.

- `cloudWatchLogsRetention`: [RetentionDays](/cdk/api/v2/docs/aws-cdk-lib.aws_logs.RetentionDays.html) = logs.RetentionDays.ONE_YEAR — How long to retain logs in CloudWatchLogs.

  Ignored if sendToCloudWatchLogs is false or if cloudWatchLogGroup is set.

- `enableFileValidation`: boolean = true — To determine whether a log file was modified, deleted, or unchanged after CloudTrail delivered it, you can use CloudTrail log file integrity validation.

  This feature is built using industry standard algorithms: SHA-256 for hashing and SHA-256 with RSA for digital signing.
  This makes it computationally infeasible to modify, delete or forge CloudTrail log files without detection.
  You can use the AWS CLI to validate the files in the location where CloudTrail delivered them.

- `encryptionKey`: [IKey](/cdk/api/v2/docs/aws-cdk-lib.aws_kms.IKey.html) — The AWS Key Management Service (AWS KMS) key ID that you want to use to encrypt CloudTrail logs.

  **Default:** No encryption.

- `includeGlobalServiceEvents`: boolean = true — For most services, events are recorded in the region where the action occurred.

  For global services such as AWS Identity and Access Management (IAM), AWS STS, Amazon CloudFront, and Route 53,
  events are delivered to any trail that includes global services, and are logged as occurring in US East (N. Virginia) Region.

- `insightTypes`: [InsightType](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudtrail.InsightType.html)[] — A JSON string that contains the insight types you want to log on a trail.

  **Default:** No Value.

- `isMultiRegionTrail`: boolean = true — Whether or not this trail delivers log files from multiple regions to a single S3 bucket for a single account.

- `isOrganizationTrail`: boolean = false — Specifies whether the trail is applied to all accounts in an organization in AWS Organizations, or only for the current AWS account.

  If this is set to true then the current account _must_ be the management account. If it is not, then CloudFormation will throw an error.

  If this is set to true and the current account is a management account for an organization in AWS Organizations, the trail will be created in all AWS accounts that belong to the organization.
  If this is set to false, the trail will remain in the current AWS account but be deleted from all member accounts in the organization.

- `managementEvents`: [ReadWriteType](/cdk/api/v2/docs/aws-cdk-lib.aws_cloudtrail.ReadWriteType.html) = ReadWriteType.ALL — When an event occurs in your account, CloudTrail evaluates whether the event matches the settings for your trails.

  Only events that match your trail settings are delivered to your Amazon S3 bucket and Amazon CloudWatch Logs log group.

  This method sets the management configuration for this trail.

  Management events provide insight into management operations that are performed on resources in your AWS account.
  These are also known as control plane operations.
  Management events can also include non-API events that occur in your account.
  For example, when a user logs in to your account, CloudTrail logs the ConsoleLogin event.

- `orgId`: string — The orgId.

  **Default:** No orgId

  Required when `isOrganizationTrail` is set to true to attach the necessary permissions.

- `s3KeyPrefix`: string — An Amazon S3 object key prefix that precedes the name of all log files.

  **Default:** No prefix.

- `sendToCloudWatchLogs`: boolean = false — If CloudTrail pushes logs to CloudWatch Logs in addition to S3.

  Disabled for cost out of the box.

- `snsTopic`: [ITopic](/cdk/api/v2/docs/aws-cdk-lib.aws_sns.ITopic.html) — SNS topic that is notified when new log files are published.

  **Default:** No notifications.

- `trailName`: string — The name of the trail.

  **Default:** AWS CloudFormation generated name.

  Required when `isOrganizationTrail` is set to true to attach the necessary permissions.

  Otherwise, we recommend customers do not set an explicit name.

## Properties

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `logGroup`: [ILogGroup](/cdk/api/v2/docs/aws-cdk-lib.aws_logs.ILogGroup.html) — The CloudWatch log group to which CloudTrail events are sent.

  `undefined` if `sendToCloudWatchLogs` property is false.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

- `trailArn`: string — ARN of the CloudTrail trail i.e. arn:aws:cloudtrail:us-east-2:123456789012:trail/myCloudTrail.

- `trailSnsTopicArn`: string — ARN of the Amazon SNS topic that's associated with the CloudTrail trail, i.e. arn:aws:sns:us-east-2:123456789012:mySNSTopic.

## Methods

- `addEventSelector(dataResourceType: DataResourceType, dataResourceValues: string[], options?: AddEventSelectorOptions): void` — When an event occurs in your account, CloudTrail evaluates whether the event matches the settings for your trails.

  Only events that match your trail settings are delivered to your Amazon S3 bucket and Amazon CloudWatch Logs log group.

  This method adds an Event Selector for filtering events that match either S3 or Lambda function operations.

  Data events: These events provide insight into the resource operations performed on or within a resource.
  These are also known as data plane operations.

  **Parameters**

  - `dataResourceValues` — the list of data resource ARNs to include in logging (maximum 250 entries).
  - `options` — the options to configure logging of management and data events.

- `addLambdaEventSelector(handlers: IFunction[], options?: AddEventSelectorOptions): void` — When an event occurs in your account, CloudTrail evaluates whether the event matches the settings for your trails.

  Only events that match your trail settings are delivered to your Amazon S3 bucket and Amazon CloudWatch Logs log group.

  This method adds a Lambda Data Event Selector for filtering events that match Lambda function operations.

  Data events: These events provide insight into the resource operations performed on or within a resource.
  These are also known as data plane operations.

  **Parameters**

  - `handlers` — the list of lambda function handlers whose data events should be logged (maximum 250 entries).
  - `options` — the options to configure logging of management and data events.

- `addS3EventSelector(s3Selector: S3EventSelector[], options?: AddEventSelectorOptions): void` — When an event occurs in your account, CloudTrail evaluates whether the event matches the settings for your trails.

  Only events that match your trail settings are delivered to your Amazon S3 bucket and Amazon CloudWatch Logs log group.

  This method adds an S3 Data Event Selector for filtering events that match S3 operations.

  Data events: These events provide insight into the resource operations performed on or within a resource.
  These are also known as data plane operations.

  **Parameters**

  - `s3Selector` — the list of S3 bucket with optional prefix to include in logging (maximum 250 entries).
  - `options` — the options to configure logging of management and data events.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `logAllLambdaDataEvents(options?: AddEventSelectorOptions): void` — Log all Lambda data events for all lambda functions the account.

- `logAllS3DataEvents(options?: AddEventSelectorOptions): void` — Log all S3 data events for all objects for all buckets in the account.

- `onEvent(scope: Construct, id: string, options?: OnEventOptions): Rule` (static) — Create an event rule for when an event is recorded by any Trail in the account.

  Note that the event doesn't necessarily have to come from this Trail, it can
  be captured from any one.

  Be sure to filter the event further down using an event pattern.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
