Trail L2 construct
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');
Example
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,
});
import aws_cdk.aws_cloudtrail as cloudtrail
my_key_alias = kms.Alias.from_alias_name(self, "myKey", "alias/aws/s3")
trail = cloudtrail.Trail(self, "myCloudTrail",
send_to_cloud_watch_logs=True,
encryption_key=my_key_alias
)
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.CloudTrail;
var myKeyAlias = Alias.FromAliasName(this, "myKey", "alias/aws/s3");
var trail = new Trail(this, "myCloudTrail", new TrailProps {
SendToCloudWatchLogs = true,
EncryptionKey = myKeyAlias
});
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import cloudtrail "github.com/aws/aws-cdk-go/awscdk"
myKeyAlias := kms.Alias_FromAliasName(this, jsii.String("myKey"), jsii.String("alias/aws/s3"))
trail := cloudtrail.NewTrail(this, jsii.String("myCloudTrail"), &TrailProps{
SendToCloudWatchLogs: jsii.Boolean(true),
EncryptionKey: myKeyAlias,
})
Initializer
new Trail(scope: Construct, id: string, props?: TrailProps)
Trail(scope: Construct, id: str, props: typing.Optional[TrailProps] = None)
new Trail(Construct scope, String id, TrailProps props)
new Trail(Construct scope, string id, TrailProps? props = null)
NewTrail(Scope Construct, Id *string, Props TrailProps)
Construct Props
bucketoptionalIBucketbucketoptionalIBucketbucketoptionalIBucketBucketoptionalIBucketBucketoptionalIBucket#Default: if not supplied a bucket will be created with all the correct permissions
cloudWatchLogGroupoptionalILogGroupRefcloud_watch_log_groupoptionalILogGroupRefcloudWatchLogGroupoptionalILogGroupRefCloudWatchLogGroupoptionalILogGroupRefCloudWatchLogGroupoptionalILogGroupRef#Default: a new log group is created and used.
Ignored if sendToCloudWatchLogs is set to false.
cloudWatchLogsRetentionoptionalRetentionDayscloud_watch_logs_retentionoptionalRetentionDayscloudWatchLogsRetentionoptionalRetentionDaysCloudWatchLogsRetentionoptionalRetentionDaysCloudWatchLogsRetentionoptionalRetentionDays= logs.RetentionDays.ONE_YEAR#Ignored if sendToCloudWatchLogs is false or if cloudWatchLogGroup is set.
enableFileValidationoptionalbooleanenable_file_validationoptionalboolenableFileValidationoptionalBooleanEnableFileValidationoptionalboolEnableFileValidationoptional*bool= true#notes
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.
encryptionKeyoptionalIKeyencryption_keyoptionalIKeyencryptionKeyoptionalIKeyEncryptionKeyoptionalIKeyEncryptionKeyoptionalIKey#Default: No encryption.
includeGlobalServiceEventsoptionalbooleaninclude_global_service_eventsoptionalboolincludeGlobalServiceEventsoptionalBooleanIncludeGlobalServiceEventsoptionalboolIncludeGlobalServiceEventsoptional*bool= true#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.
insightTypesoptionalInsightType[]insight_typesoptionaltyping.List[InsightType]insightTypesoptionalList<InsightType>InsightTypesoptionalInsightType[]InsightTypesoptional*[]InsightType#Default: No Value.
isMultiRegionTrailoptionalbooleanis_multi_region_trailoptionalboolisMultiRegionTrailoptionalBooleanIsMultiRegionTrailoptionalboolIsMultiRegionTrailoptional*bool= true#isOrganizationTrailoptionalbooleanis_organization_trailoptionalboolisOrganizationTrailoptionalBooleanIsOrganizationTrailoptionalboolIsOrganizationTrailoptional*bool= false#notes
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.
managementEventsoptionalReadWriteTypemanagement_eventsoptionalReadWriteTypemanagementEventsoptionalReadWriteTypeManagementEventsoptionalReadWriteTypeManagementEventsoptionalReadWriteType= ReadWriteType.ALL#notes
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.
Default: No orgId
Required when isOrganizationTrail is set to true to attach the necessary permissions.
s3KeyPrefixoptionalstrings3_key_prefixoptionalstrs3KeyPrefixoptionalStringS3KeyPrefixoptionalstringS3KeyPrefixoptional*string#Default: No prefix.
sendToCloudWatchLogsoptionalbooleansend_to_cloud_watch_logsoptionalboolsendToCloudWatchLogsoptionalBooleanSendToCloudWatchLogsoptionalboolSendToCloudWatchLogsoptional*bool= false#Disabled for cost out of the box.
snsTopicoptionalITopicsns_topicoptionalITopicsnsTopicoptionalITopicSnsTopicoptionalITopicSnsTopicoptionalITopic#Default: No notifications.
trailNameoptionalstringtrail_nameoptionalstrtrailNameoptionalStringTrailNameoptionalstringTrailNameoptional*string#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
envResourceEnvironmentenvResourceEnvironmentenvResourceEnvironmentEnvResourceEnvironmentEnvResourceEnvironment#notes
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.
logGroupmay be undefinedILogGrouplog_groupmay be undefinedILogGrouplogGroupmay be undefinedILogGroupLogGroupmay be undefinedILogGroupLogGroupmay be undefinedILogGroup#undefined if sendToCloudWatchLogs property is false.
PROPERTY_INJECTION_IDstringproperty_injection_idstrPROPERTY_INJECTION_IDStringPROPERTY_INJECTION_IDstringPROPERTY_INJECTION_ID*stringstatic#trailSnsTopicArnstringtrail_sns_topic_arnstrtrailSnsTopicArnStringTrailSnsTopicArnstringTrailSnsTopicArn*string#Methods
addEventSelector(dataResourceType: DataResourceType, dataResourceValues: string[], options?: AddEventSelectorOptions): voiddef add_event_selector(self, data_resource_type: DataResourceType, data_resource_values: typing.List[str], options: typing.Optional[AddEventSelectorOptions] = None) -> Nonepublic void addEventSelector(DataResourceType dataResourceType, List<String> dataResourceValues, AddEventSelectorOptions options)public void AddEventSelector(DataResourceType dataResourceType, string[] dataResourceValues, AddEventSelectorOptions? options = null)func AddEventSelector(DataResourceType DataResourceType, DataResourceValues *[]*string, Options AddEventSelectorOptions)#notes · 2 parameters
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
dataResourceValuesdata_resource_valuesdataResourceValuesdataResourceValuesDataResourceValues— the list of data resource ARNs to include in logging (maximum 250 entries).optionsoptionsoptionsoptionsOptions— the options to configure logging of management and data events.
addLambdaEventSelector(handlers: IFunction[], options?: AddEventSelectorOptions): voiddef add_lambda_event_selector(self, handlers: typing.List[IFunction], options: typing.Optional[AddEventSelectorOptions] = None) -> Nonepublic void addLambdaEventSelector(List<IFunction> handlers, AddEventSelectorOptions options)public void AddLambdaEventSelector(IFunction[] handlers, AddEventSelectorOptions? options = null)func AddLambdaEventSelector(Handlers *[]IFunction, Options AddEventSelectorOptions)#notes · 2 parameters
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
handlershandlershandlershandlersHandlers— the list of lambda function handlers whose data events should be logged (maximum 250 entries).optionsoptionsoptionsoptionsOptions— the options to configure logging of management and data events.
addS3EventSelector(s3Selector: S3EventSelector[], options?: AddEventSelectorOptions): voiddef add_s3_event_selector(self, s3_selector: typing.List[S3EventSelector], options: typing.Optional[AddEventSelectorOptions] = None) -> Nonepublic void addS3EventSelector(List<S3EventSelector> s3Selector, AddEventSelectorOptions options)public void AddS3EventSelector(S3EventSelector[] s3Selector, AddEventSelectorOptions? options = null)func AddS3EventSelector(S3Selector *[]S3EventSelector, Options AddEventSelectorOptions)#notes · 2 parameters
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
s3Selectors3_selectors3Selectors3SelectorS3Selector— the list of S3 bucket with optional prefix to include in logging (maximum 250 entries).optionsoptionsoptionsoptionsOptions— the options to configure logging of management and data events.
applyCrossStackReferenceStrength(strength: ReferenceStrength): voiddef apply_cross_stack_reference_strength(self, strength: ReferenceStrength) -> Nonepublic void applyCrossStackReferenceStrength(ReferenceStrength strength)public void ApplyCrossStackReferenceStrength(ReferenceStrength strength)func ApplyCrossStackReferenceStrength(Strength ReferenceStrength)#notes · 1 parameter
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
strengthstrengthstrengthstrengthStrength— - The reference strength to use for this resource.
applyRemovalPolicy(policy: RemovalPolicy): voiddef apply_removal_policy(self, policy: RemovalPolicy) -> Nonepublic void applyRemovalPolicy(RemovalPolicy policy)public void ApplyRemovalPolicy(RemovalPolicy policy)func ApplyRemovalPolicy(Policy RemovalPolicy)#notes
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@staticmethod def is_construct(x: Any) -> boolpublic static Boolean isConstruct(Object x)public static bool IsConstruct(object x)func IsConstruct(X interface{}) *boolstatic#notes · 1 parameter · returns
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
xxxxX— Any object.
Returns: true if x is an object created from a class which extends Construct.
isOwnedResource(construct: IConstruct): boolean@staticmethod def is_owned_resource(construct: IConstruct) -> boolpublic static Boolean isOwnedResource(IConstruct construct)public static bool IsOwnedResource(IConstruct construct)func IsOwnedResource(Construct IConstruct) *boolstatic#isResource(construct: IConstruct): boolean@staticmethod def is_resource(construct: IConstruct) -> boolpublic static Boolean isResource(IConstruct construct)public static bool IsResource(IConstruct construct)func IsResource(Construct IConstruct) *boolstatic#logAllLambdaDataEvents(options?: AddEventSelectorOptions): voiddef log_all_lambda_data_events(self, options: typing.Optional[AddEventSelectorOptions] = None) -> Nonepublic void logAllLambdaDataEvents(AddEventSelectorOptions options)public void LogAllLambdaDataEvents(AddEventSelectorOptions? options = null)func LogAllLambdaDataEvents(Options AddEventSelectorOptions)#logAllS3DataEvents(options?: AddEventSelectorOptions): voiddef log_all_s3_data_events(self, options: typing.Optional[AddEventSelectorOptions] = None) -> Nonepublic void logAllS3DataEvents(AddEventSelectorOptions options)public void LogAllS3DataEvents(AddEventSelectorOptions? options = null)func LogAllS3DataEvents(Options AddEventSelectorOptions)#onEvent(scope: Construct, id: string, options?: OnEventOptions): Rule@staticmethod def on_event(scope: Construct, id: str, options: typing.Optional[OnEventOptions] = None) -> Rulepublic static Rule onEvent(Construct scope, String id, OnEventOptions options)public static Rule OnEvent(Construct scope, string id, OnEventOptions? options = null)func OnEvent(Scope Construct, Id *string, Options OnEventOptions) Rulestatic#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(): stringdef to_string(self, ) -> strpublic String toString()public string ToString()func ToString() *string#with(...mixins: IMixin[]): IConstructdef with(self, mixins: IMixin) -> IConstructpublic IConstruct with(IMixin mixins)public IConstruct With(IMixin mixins)func With(Mixins IMixin) IConstruct#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.