Package software.amazon.awscdk.mixins.preview


@Stability(Experimental) package software.amazon.awscdk.mixins.preview

CDK Mixins

Note: The core Mixins mechanism is now GA and available in constructs and aws-cdk-lib (Mixins, Mixin, IMixin, MixinApplicator, ConstructSelector). All service Mixins are now available in aws-cdk-lib. Please update your imports.

This package continues to provide Logs Delivery Mixins and EventBridge Event Facades, which are still experimental.

---

cdk-constructs: Experimental

The APIs of higher level constructs in this module are experimental and under active development. They are subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model and breaking changes will be announced in the release notes. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.


CDK Mixins provide a new, advanced way to add functionality through composable abstractions. Unlike traditional L2 constructs that bundle all features together, Mixins allow you to pick and choose exactly the capabilities you need for constructs. Mixins can be applied during or after construct construction. Mixins are an addition, not a replacement for construct properties. By itself, they cannot change optionality of properties or change defaults.

Usage and documentation

See the documentation for CDK Mixins in aws-cdk-lib.

Built-in Mixins

Logs Delivery

Configures vended logs delivery for supported resources to various destinations:

 import software.amazon.awscdk.mixins.preview.services.cloudfront.mixins.*;
 
 // Create CloudFront distribution
 IBucket origin;
 
 Distribution distribution = Distribution.Builder.create(scope, "Distribution")
         .defaultBehavior(BehaviorOptions.builder()
                 .origin(S3BucketOrigin.withOriginAccessControl(origin))
                 .build())
         .build();
 
 // Create log destination
 LogGroup logGroup = new LogGroup(scope, "DeliveryLogGroup");
 
 // Configure log delivery using the mixin
 distribution.with(CfnDistributionLogsMixin.CONNECTION_LOGS.toLogGroup(logGroup, CfnDistributionConnectionLogsLogGroupProps.builder()
         .outputFormat(CfnDistributionConnectionLogsOutputFormat.getLogGroup().JSON)
         .recordFields(List.of(CfnDistributionConnectionLogsRecordFields.CONNECTIONSTATUS, CfnDistributionConnectionLogsRecordFields.CLIENTIP, CfnDistributionConnectionLogsRecordFields.SERVERIP, CfnDistributionConnectionLogsRecordFields.TLSPROTOCOL))
         .build()));
 

Configures vended logs delivery for supported resources when a pre-created destination is provided:

 import software.amazon.awscdk.mixins.preview.services.cloudfront.mixins.*;
 
 // Create CloudFront distribution
 IBucket origin;
 
 Distribution distribution = Distribution.Builder.create(scope, "Distribution")
         .defaultBehavior(BehaviorOptions.builder()
                 .origin(S3BucketOrigin.withOriginAccessControl(origin))
                 .build())
         .build();
 
 // Create destination bucket
 Bucket destBucket = new Bucket(scope, "DeliveryBucket");
 // Add permissions to bucket to facilitate log delivery
 BucketPolicy bucketPolicy = BucketPolicy.Builder.create(scope, "DeliveryBucketPolicy")
         .bucket(destBucket)
         .document(new PolicyDocument())
         .build();
 // Create S3 delivery destination for logs
 CfnDeliveryDestination destination = CfnDeliveryDestination.Builder.create(scope, "Destination")
         .destinationResourceArn(destBucket.getBucketArn())
         .name("unique-destination-name")
         .deliveryDestinationType("S3")
         .build();
 
 distribution.with(CfnDistributionLogsMixin.CONNECTION_LOGS.toDestination(destination));
 

Vended Logs Configuration for Cross Account delivery (only supported for S3 and Firehose destinations)

 import software.amazon.awscdk.mixins.preview.services.logs.*;
 import software.amazon.awscdk.mixins.preview.services.cloudfront.mixins.*;
 
 // Create CloudFront distribution
 IBucket origin;
 
 
 String destinationAccount = "123456789012";
 String sourceAccount = "234567890123";
 String region = "us-east-1";
 
 App app = new App();
 
 Stack destStack = Stack.Builder.create(app, "destination-stack")
         .env(Environment.builder()
                 .account(destinationAccount)
                 .region(region)
                 .build())
         .build();
 
 // Create destination bucket
 Bucket destBucket = new Bucket(destStack, "DeliveryBucket");
 S3DeliveryDestination.Builder.create(destStack, "Destination")
         .bucket(destBucket)
         .sourceAccountId(sourceAccount)
         .build();
 
 Stack sourceStack = Stack.Builder.create(app, "source-stack")
         .env(Environment.builder()
                 .account(sourceAccount)
                 .region(region)
                 .build())
         .build();
 Distribution distribution = Distribution.Builder.create(sourceStack, "Distribution")
         .defaultBehavior(BehaviorOptions.builder()
                 .origin(S3BucketOrigin.withOriginAccessControl(origin))
                 .build())
         .build();
 
 IDeliveryDestinationRef destination = CfnDeliveryDestination.fromDeliveryDestinationArn(sourceStack, "Destination", "arn of Delivery Destination in destinationAccount");
 
 distribution.with(CfnDistributionLogsMixin.CONNECTION_LOGS.toDestination(destination));
 


EventBridge Event Patterns

CDK Mixins automatically generates typed EventBridge event patterns for AWS resources. These patterns come in two flavors: resource-specific and standalone.

Resource-Specific Event Patterns

Resource-specific patterns are created by attaching a resource reference (e.g. an S3 bucket). The resource identifier is automatically injected into the event pattern, so calling a pattern method with no arguments still filters events to that specific resource. For example, an S3 objectCreatedPattern() will automatically include the bucket name in the pattern, meaning it only matches events from that particular bucket.

Event Patterns Basic Usage

 import software.amazon.awscdk.mixins.preview.services.s3.events.BucketEvents;
 import software.amazon.awscdk.services.events.*;
 import software.amazon.awscdk.services.events.targets.*;
 Function fn;
 
 
 // Works with L2 constructs
 Bucket myBucket = new Bucket(scope, "Bucket");
 BucketEvents bucketEvents = BucketEvents.fromBucket(myBucket);
 
 Rule.Builder.create(scope, "Rule")
         .eventPattern(bucketEvents.objectCreatedPattern(ObjectCreatedProps.builder()
                 .object(ObjectType.builder().key(Match.wildcard("uploads/*")).build())
                 .build()))
         .targets(List.of(new LambdaFunction(fn)))
         .build();
 
 // Also works with L1 constructs
 CfnBucket cfnBucket = new CfnBucket(scope, "CfnBucket");
 BucketEvents cfnBucketEvents = BucketEvents.fromBucket(cfnBucket);
 
 CfnRule.Builder.create(scope, "CfnRule")
         .state("ENABLED")
         .eventPattern(cfnBucketEvents.objectCreatedPattern(ObjectCreatedProps.builder()
                 .object(ObjectType.builder().key(Match.wildcard("uploads/*")).build())
                 .build()))
         .targets(List.of(TargetProperty.builder().arn(fn.getFunctionArn()).id("L1").build()))
         .build();
 

Event Pattern Features

 import software.amazon.awscdk.mixins.preview.services.s3.events.BucketEvents;
 
 
 BucketEvents bucketEvents = BucketEvents.fromBucket(bucket);
 
 // Bucket name is automatically injected from the bucket reference
 EventPattern pattern = bucketEvents.objectCreatedPattern();
 

Event Metadata Support: Control EventBridge pattern metadata

 import software.amazon.awscdk.mixins.preview.services.s3.events.BucketEvents;
 import software.amazon.awscdk.services.events.*;
 
 
 BucketEvents bucketEvents = BucketEvents.fromBucket(bucket);
 
 EventPattern pattern = bucketEvents.objectCreatedPattern(ObjectCreatedProps.builder()
         .eventMetadata(AWSEventMetadataProps.builder()
                 .region(Match.prefix("us-"))
                 .version(List.of("0"))
                 .build())
         .build());
 

Standalone Event Patterns

Standalone patterns are not tied to any specific resource. They match events across all resources of that type. For example, a standalone awsAPICallViaCloudTrailPattern() will match CloudTrail API calls for all S3 buckets in the account, not just a specific one.

Event Patterns Basic Usage

 import software.amazon.awscdk.mixins.preview.services.s3.events.AWSAPICallViaCloudTrail;
 import software.amazon.awscdk.mixins.preview.services.s3.events.ObjectCreated;
 import software.amazon.awscdk.mixins.preview.services.s3.events.ObjectDeleted;
 import software.amazon.awscdk.services.events.*;
 import software.amazon.awscdk.services.events.targets.*;
 
 Function fn;
 
 
 // Works with L2 Rule
 // Works with L2 Rule
 Rule.Builder.create(scope, "Rule")
         .eventPattern(AWSAPICallViaCloudTrail.awsAPICallViaCloudTrailPattern(AWSAPICallViaCloudTrailProps.builder()
                 .tlsDetails(TlsDetails.builder().tlsVersion(List.of("TLSv1.3")).build())
                 .eventMetadata(AWSEventMetadataProps.builder().region(List.of("us-east-1")).build())
                 .build()))
         .targets(List.of(new LambdaFunction(fn)))
         .build();
 
 // Also works with L1 CfnRule
 // Also works with L1 CfnRule
 CfnRule.Builder.create(scope, "CfnRule")
         .state("ENABLED")
         .eventPattern(AWSAPICallViaCloudTrail.awsAPICallViaCloudTrailPattern(AWSAPICallViaCloudTrailProps.builder()
                 .tlsDetails(TlsDetails.builder().tlsVersion(List.of("TLSv1.3")).build())
                 .eventMetadata(AWSEventMetadataProps.builder().region(List.of("us-east-1")).build())
                 .build()))
         .targets(List.of(TargetProperty.builder().arn(fn.getFunctionArn()).id("L1").build()))
         .build();
 

Event Pattern Features

 import software.amazon.awscdk.mixins.preview.services.s3.events.AWSAPICallViaCloudTrail;
 
 
 // Matches CloudTrail API calls across ALL S3 buckets
 EventPattern pattern = AWSAPICallViaCloudTrail.awsAPICallViaCloudTrailPattern();
 

Event Metadata Support: Control EventBridge pattern metadata

 import software.amazon.awscdk.mixins.preview.services.s3.events.AWSAPICallViaCloudTrail;
 import software.amazon.awscdk.services.events.*;
 
 
 EventPattern pattern = AWSAPICallViaCloudTrail.awsAPICallViaCloudTrailPattern(AWSAPICallViaCloudTrailProps.builder()
         .eventMetadata(AWSEventMetadataProps.builder()
                 .region(Match.prefix("us-"))
                 .version(List.of("0"))
                 .build())
         .build());
 

Available Events

Event patterns are generated for EventBridge events available in the AWS Event Schema Registry. Common examples:

S3 Events:

  • objectCreatedPattern() - Object creation events
  • objectDeletedPattern() - Object deletion events
  • objectTagsAddedPattern() - Object tagging events
  • awsAPICallViaCloudTrailPattern() - CloudTrail API calls

Import events from service-specific modules:

 // Resource-specific (filters to a specific bucket)
 import software.amazon.awscdk.mixins.preview.services.s3.events.BucketEvents;
 
 // Standalone (matches across all buckets)
 import software.amazon.awscdk.mixins.preview.services.s3.events.AWSAPICallViaCloudTrail;
 import software.amazon.awscdk.mixins.preview.services.s3.events.ObjectCreated;
 import software.amazon.awscdk.mixins.preview.services.s3.events.ObjectDeleted;
 
  • Classes
    Class
    Description