Package: aws-cdk.aws-cloudtrail
AWS CloudTrail Construct Library---AWS CDK v1 has reached End-of-Support on 2023-06-01.
This package is no longer being updated, and users should migrate to AWS CDK v2.For more information on how to migrate, see theMigrating to AWS CDK v2guide.TrailAWS 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 theCloudTrail
documentation.TheTrailconstruct enables ongoing delivery of events as log files to an Amazon S3 bucket. Learn more aboutCreating
a Trail for Your AWS Account.
The following code creates a simple CloudTrail for your account -trail=cloudtrail.Trail(self,"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 theTrailPropsinterface.Log File ValidationIn 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 atValidating CloudTrail Log File
Integrity.This is enabled on theTrailconstruct by default, but can be turned off by settingenableFileValidationtofalse.trail=cloudtrail.Trail(self,"CloudTrail",enable_file_validation=False)NotificationsAmazon SNS notifications can be configured upon new log files containing Trail events are delivered to S3.
Learn more atConfiguring Amazon SNS Notifications for
CloudTrail.
The following code configures an SNS topic to be notified -topic=sns.Topic(self,"TrailTopic")trail=cloudtrail.Trail(self,"CloudTrail",sns_topic=topic)Service IntegrationsBesides sending trail events to S3, they can also be configured to notify other AWS services -Amazon CloudWatch LogsCloudTrail 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.importaws_cdk.aws_logsaslogstrail=cloudtrail.Trail(self,"CloudTrail",send_to_cloud_watch_logs=True,cloud_watch_logs_retention=logs.RetentionDays.FOUR_MONTHS)If you would like to use a specific log group instead, this can be configured viacloudwatchLogGroup.Amazon EventBridgeAmazon EventBridge rules can be configured to be triggered when CloudTrail events occur using theTrail.onEvent()API.
Using APIs available inaws-events, these events can be filtered to match to those that are of interest, either from
a specific service, account or time range. SeeEvents delivered via
CloudTrailto 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.my_function_handler=lambda_.Function(self,"MyFunction",code=lambda_.Code.from_asset("resource/myfunction"),runtime=lambda_.Runtime.NODEJS_14_X,handler="index.handler")event_rule=cloudtrail.Trail.on_event(self,"MyCloudWatchEvent",target=targets.LambdaFunction(my_function_handler))event_rule.add_event_pattern(account=["123456789012"],source=["aws.s3"])Multi-Region & Global Service EventsBy default, aTrailis 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 aboutHow
CloudTrail Behaves Regionallyand about theIsMultiRegionproperty.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 moreAbout Global Service Events.Events for global services are turned on by default forTrailconstructs in the CDK.The following code disables multi-region trail delivery and trail delivery for global services for a specificTrail-trail=cloudtrail.Trail(self,"CloudTrail",# ...is_multi_region_trail=False,include_global_service_events=False)Events TypesManagement eventsprovide information about management operations that are performed on resources in your AWS
account. These are also known as control plane operations. Learn more aboutManagement
Events.By default, aTraillogs 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 theTrailto only track management events that are of type 'Read'.trail=cloudtrail.Trail(self,"CloudTrail",# ...management_events=cloudtrail.ReadWriteType.READ_ONLY)Data eventsprovide information about the resource operations performed on or in a resource. These are also known
as data plane operations. Learn more aboutData
Events.
By default, no data events are logged for aTrail.AWS CloudTrail supports data event logging for Amazon S3 objects and AWS Lambda functions.ThelogAllS3DataEvents()API configures the trail to log all S3 data events while theaddS3EventSelector()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 forfooBucketand with object prefixbar/.importaws_cdk.aws_s3ass3# bucket: s3.Buckettrail=cloudtrail.Trail(self,"MyAmazingCloudTrail")# Adds an event selector to the bucket footrail.add_s3_event_selector([bucket=bucket,object_prefix="bar/"])Similarly, thelogAllLambdaDataEvents()configures the trail to log all Lambda data events while theaddLambdaEventSelector()API can be used to configure logging for specific Lambda functions. The following code
configures logging of Lambda data events for a specific Function.trail=cloudtrail.Trail(self,"MyAmazingCloudTrail")amazing_function=lambda_.Function(self,"AnAmazingFunction",runtime=lambda_.Runtime.NODEJS_14_X,handler="hello.handler",code=lambda_.Code.from_asset("lambda"))# Add an event selector to log data events for the provided Lambda functions.trail.add_lambda_event_selector([amazing_function])