Skip to main content

CDK Constructs for AWS CloudTrail

Project description

AWS CloudTrail Construct Library

---

cfn-resources: Stable

cdk-constructs: Stable


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 -

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
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 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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
trail = cloudtrail.Trail(self, "CloudTrail",
    enable_file_validation=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 -

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
topic = sns.Topic(self, "TrailTopic")
trail = cloudtrail.Trail(self, "CloudTrail",
    sns_topic=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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
trail = 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 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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
my_function_handler = lambda_.Function(self, "MyFunction",
    code=lambda_.Code.from_asset("resource/myfunction"),
    runtime=lambda_.Runtime.NODEJS_12_X,
    handler="index.handler"
)

event_rule = Trail.on_event(self, "MyCloudWatchEvent",
    target=event_targets.LambdaFunction(my_function_handler)
)

event_rule.add_event_pattern(
    account="123456789012",
    source="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 -

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
trail = cloudtrail.Trail(self, "CloudTrail",
    # ...
    is_multi_region_trail=False,
    include_global_service_events=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'.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
trail = cloudtrail.Trail(self, "CloudTrail",
    # ...
    management_events=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/.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_cloudtrail as cloudtrail

trail = cloudtrail.Trail(self, "MyAmazingCloudTrail")

# Adds an event selector to the bucket foo
trail.add_s3_event_selector([
    bucket=foo_bucket, # 'fooBucket' is of type s3.IBucket
    object_prefix="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.

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
trail = cloudtrail.Trail(self, "MyAmazingCloudTrail")
amazing_function = lambda_.Function(stack, "AnAmazingFunction",
    runtime=lambda_.Runtime.NODEJS_10_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([lambda_function])

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aws-cdk.aws-cloudtrail-1.63.0.tar.gz (80.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aws_cdk.aws_cloudtrail-1.63.0-py3-none-any.whl (78.3 kB view details)

Uploaded Python 3

File details

Details for the file aws-cdk.aws-cloudtrail-1.63.0.tar.gz.

File metadata

  • Download URL: aws-cdk.aws-cloudtrail-1.63.0.tar.gz
  • Upload date:
  • Size: 80.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.5

File hashes

Hashes for aws-cdk.aws-cloudtrail-1.63.0.tar.gz
Algorithm Hash digest
SHA256 b8ef9608d2e8ef2ea6129b4ba63f0b3089ce011216eefd3e6500c7baeaeeb214
MD5 473b0b73934fd2bc468dfa2e13b5eb9f
BLAKE2b-256 ed8414b9adf95a9345ce46f33ef7f436cfc8aad935289a7c8f3668a021b3ffdf

See more details on using hashes here.

File details

Details for the file aws_cdk.aws_cloudtrail-1.63.0-py3-none-any.whl.

File metadata

  • Download URL: aws_cdk.aws_cloudtrail-1.63.0-py3-none-any.whl
  • Upload date:
  • Size: 78.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.5

File hashes

Hashes for aws_cdk.aws_cloudtrail-1.63.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41c353d01f83ce8708c422b7a6827c223b50888971e3fd0efb15a87d98e73de0
MD5 1801b73e27e504f6c9f529dff68920cc
BLAKE2b-256 963144b803bb5b365735d5b89a232eba1649ad914db529ef3599b5bbfa0fd80c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page