Namespace Amazon.CDK.AWS.CloudTrail
AWS CloudTrail Construct Library
Trail
AWS CloudTrail enables governance, compliance, and operational and risk auditing of your AWS account. Actions taken by a user, role, or an AWS service are recorded as events in CloudTrail. Learn more at the CloudTrail documentation.
The Trail
construct enables ongoing delivery of events as log files to an Amazon S3 bucket. Learn more about Creating
a Trail for Your AWS Account.
The following code creates a simple CloudTrail for your account -
var trail = new Trail(this, "CloudTrail");
By default, this will create a new S3 Bucket that CloudTrail will write to, and choose a few other reasonable defaults
such as turning on multi-region and global service events.
The defaults for each property and how to override them are all documented on the TrailProps
interface.
Log File Validation
In order to validate that the CloudTrail log file was not modified after CloudTrail delivered it, CloudTrail provides a digital signature for each file. Learn more at Validating CloudTrail Log File Integrity.
This is enabled on the Trail
construct by default, but can be turned off by setting enableFileValidation
to false
.
var trail = new Trail(this, "CloudTrail", new TrailProps {
EnableFileValidation = false
});
Notifications
Amazon SNS notifications can be configured upon new log files containing Trail events are delivered to S3. Learn more at Configuring Amazon SNS Notifications for CloudTrail. The following code configures an SNS topic to be notified -
var topic = new Topic(this, "TrailTopic");
var trail = new Trail(this, "CloudTrail", new TrailProps {
SnsTopic = topic
});
Service Integrations
Besides sending trail events to S3, they can also be configured to notify other AWS services -
Amazon CloudWatch Logs
CloudTrail events can be delivered to a CloudWatch Logs LogGroup. By default, a new LogGroup is created with a default retention setting. The following code enables sending CloudWatch logs but specifies a particular retention period for the created Log Group.
using Amazon.CDK.AWS.Logs;
var trail = new Trail(this, "CloudTrail", new TrailProps {
SendToCloudWatchLogs = true,
CloudWatchLogsRetention = RetentionDays.FOUR_MONTHS
});
If you would like to use a specific log group instead, this can be configured via cloudwatchLogGroup
.
Amazon EventBridge
Amazon EventBridge rules can be configured to be triggered when CloudTrail events occur using the Trail.onEvent()
API.
Using APIs available in aws-events
, these events can be filtered to match to those that are of interest, either from
a specific service, account or time range. See Events delivered via
CloudTrail
to learn more about the event structure for events from CloudTrail.
The following code filters events for S3 from a specific AWS account and triggers a lambda function.
var myFunctionHandler = new Function(this, "MyFunction", new FunctionProps {
Code = Code.FromAsset("resource/myfunction"),
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler"
});
var eventRule = Trail.OnEvent(this, "MyCloudWatchEvent", new OnEventOptions {
Target = new LambdaFunction(myFunctionHandler)
});
eventRule.AddEventPattern(new EventPattern {
Account = new [] { "123456789012" },
Source = new [] { "aws.s3" }
});
Multi-Region & Global Service Events
By default, a Trail
is configured to deliver log files from multiple regions to a single S3 bucket for a given
account. This creates shadow trails (replication of the trails) in all of the other regions. Learn more about How
CloudTrail Behaves Regionally
and about the IsMultiRegion
property.
For most services, events are recorded in the region where the action occurred. For global services such as AWS IAM, AWS STS, Amazon CloudFront, Route 53, etc., events are delivered to any trail that includes global services. Learn more About Global Service Events.
Events for global services are turned on by default for Trail
constructs in the CDK.
The following code disables multi-region trail delivery and trail delivery for global services for a specific Trail
-
var trail = new Trail(this, "CloudTrail", new TrailProps {
// ...
IsMultiRegionTrail = false,
IncludeGlobalServiceEvents = false
});
Events Types
Management events provide information about management operations that are performed on resources in your AWS account. These are also known as control plane operations. Learn more about Management Events.
By default, a Trail
logs all management events. However, they can be configured to either be turned off, or to only
log 'Read' or 'Write' events.
The following code configures the Trail
to only track management events that are of type 'Read'.
var trail = new Trail(this, "CloudTrail", new TrailProps {
// ...
ManagementEvents = ReadWriteType.READ_ONLY
});
Data events provide information about the resource operations performed on or in a resource. These are also known
as data plane operations. Learn more about Data
Events.
By default, no data events are logged for a Trail
.
AWS CloudTrail supports data event logging for Amazon S3 objects and AWS Lambda functions.
The logAllS3DataEvents()
API configures the trail to log all S3 data events while the addS3EventSelector()
API can
be used to configure logging of S3 data events for specific buckets and specific object prefix. The following code
configures logging of S3 data events for fooBucket
and with object prefix bar/
.
using Amazon.CDK.AWS.S3;
Bucket bucket;
var trail = new Trail(this, "MyAmazingCloudTrail");
// Adds an event selector to the bucket foo
trail.AddS3EventSelector(new [] { new S3EventSelector {
Bucket = bucket,
ObjectPrefix = "bar/"
} });
Similarly, the logAllLambdaDataEvents()
configures the trail to log all Lambda data events while the
addLambdaEventSelector()
API can be used to configure logging for specific Lambda functions. The following code
configures logging of Lambda data events for a specific Function.
var trail = new Trail(this, "MyAmazingCloudTrail");
var amazingFunction = new Function(this, "AnAmazingFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "hello.handler",
Code = Code.FromAsset("lambda")
});
// Add an event selector to log data events for the provided Lambda functions.
trail.AddLambdaEventSelector(new [] { amazingFunction });
Organization Trail
It is possible to create a trail that will be applied to all accounts in an organization if the current account manages an organization.
To enable this, the property isOrganizationTrail
must be set. If this property is set and the current account does not manage an organization, the stack will fail to deploy.
new Trail(this, "OrganizationTrail", new TrailProps {
IsOrganizationTrail = true
});
CloudTrail Insights
Set InsightSelector
to enable Insight.
Insights selector values can be ApiCallRateInsight
, ApiErrorRateInsight
, or both.
new Trail(this, "Insights", new TrailProps {
InsightTypes = new [] { InsightType.API_CALL_RATE, InsightType.API_ERROR_RATE }
});
Classes
AddEventSelectorOptions | Options for adding an event selector. |
CfnChannel | Contains information about a returned CloudTrail channel. |
CfnChannel.DestinationProperty | Contains information about the destination receiving events. |
CfnChannelProps | Properties for defining a |
CfnEventDataStore | Creates a new event data store. |
CfnEventDataStore.AdvancedEventSelectorProperty | Advanced event selectors let you create fine-grained selectors for AWS CloudTrail management, data, and network activity events. |
CfnEventDataStore.AdvancedFieldSelectorProperty | A single selector statement in an advanced event selector. |
CfnEventDataStore.InsightSelectorProperty | A JSON string that contains a list of Insights types that are logged on an event data store. |
CfnEventDataStoreProps | Properties for defining a |
CfnResourcePolicy | Attaches a resource-based permission policy to a CloudTrail channel that is used for an integration with an event source outside of AWS . |
CfnResourcePolicyProps | Properties for defining a |
CfnTrail | Creates a trail that specifies the settings for delivery of log data to an Amazon S3 bucket. |
CfnTrail.AdvancedEventSelectorProperty | Advanced event selectors let you create fine-grained selectors for AWS CloudTrail management, data, and network activity events. |
CfnTrail.AdvancedFieldSelectorProperty | A single selector statement in an advanced event selector. |
CfnTrail.DataResourceProperty | You can configure the |
CfnTrail.EventSelectorProperty | Use event selectors to further specify the management and data event settings for your trail. |
CfnTrail.InsightSelectorProperty | A JSON string that contains a list of Insights types that are logged on a trail. |
CfnTrailProps | Properties for defining a |
DataResourceType | Resource type for a data event. |
InsightType | Util element for InsightSelector. |
ManagementEventSources | Types of management event sources that can be excluded. |
ReadWriteType | Types of events that CloudTrail can log. |
S3EventSelector | Selecting an S3 bucket and an optional prefix to be logged for data events. |
Trail | Cloud trail allows you to log events that happen in your AWS account For example:. |
TrailProps | Properties for an AWS CloudTrail trail. |
Interfaces
CfnChannel.IDestinationProperty | Contains information about the destination receiving events. |
CfnEventDataStore.IAdvancedEventSelectorProperty | Advanced event selectors let you create fine-grained selectors for AWS CloudTrail management, data, and network activity events. |
CfnEventDataStore.IAdvancedFieldSelectorProperty | A single selector statement in an advanced event selector. |
CfnEventDataStore.IInsightSelectorProperty | A JSON string that contains a list of Insights types that are logged on an event data store. |
CfnTrail.IAdvancedEventSelectorProperty | Advanced event selectors let you create fine-grained selectors for AWS CloudTrail management, data, and network activity events. |
CfnTrail.IAdvancedFieldSelectorProperty | A single selector statement in an advanced event selector. |
CfnTrail.IDataResourceProperty | You can configure the |
CfnTrail.IEventSelectorProperty | Use event selectors to further specify the management and data event settings for your trail. |
CfnTrail.IInsightSelectorProperty | A JSON string that contains a list of Insights types that are logged on a trail. |
IAddEventSelectorOptions | Options for adding an event selector. |
ICfnChannelProps | Properties for defining a |
ICfnEventDataStoreProps | Properties for defining a |
ICfnResourcePolicyProps | Properties for defining a |
ICfnTrailProps | Properties for defining a |
IS3EventSelector | Selecting an S3 bucket and an optional prefix to be logged for data events. |
ITrailProps | Properties for an AWS CloudTrail trail. |