Skip to main content

Preview of CDK Mixins - composable, reusable abstractions that can be applied to any construct (L1, L2 or custom).

Project description

CDK Mixins (Preview)

---

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.

Key Benefits

  • Universal Compatibility: Apply the same abstractions to L1 constructs, L2 constructs, or custom constructs
  • Composable Design: Mix and match features without being locked into specific implementations
  • Cross-Service Abstractions: Use common patterns like encryption across different AWS services
  • Escape Hatch Freedom: Customize resources in a safe, typed way while keeping the abstractions you want

Basic Usage

Mixins use Mixins.of() as the fundamental API for applying abstractions to constructs:

# Base form: apply mixins to any construct
bucket = s3.CfnBucket(scope, "MyBucket")
Mixins.of(bucket).apply(EncryptionAtRest()).apply(AutoDeleteObjects())

Fluent Syntax with .with()

For convenience, you can use the .with() method for a more fluent syntax:

from aws_cdk.mixins_preview.with import


bucket = s3.CfnBucket(scope, "MyBucket").with(EnableVersioning()).with(AutoDeleteObjects())

The .with() method is available after importing @aws-cdk/mixins-preview/with, which augments all constructs with this method. It provides the same functionality as Mixins.of().apply() but with a more chainable API.

Note: The .with() fluent syntax is only available in JavaScript and TypeScript. Other jsii languages (Python, Java, C#, and Go) should use the Mixins.of(...).mustApply() syntax instead. The import requirement is temporary during the preview phase. Once the API is stable, the .with() method will be available by default on all constructs and in all languages.

Creating Custom Mixins

Mixins are simple classes that implement the IMixin interface (usually by extending the abstract Mixin class:

# Simple mixin that enables versioning
@jsii.implements(IMixin)
class CustomVersioningMixin(Mixin):
    def supports(self, construct):
        return construct instanceof s3.CfnBucket

    def apply_to(self, bucket):
        bucket.versioning_configuration = {
            "status": "Enabled"
        }
        return bucket

# Usage
bucket = s3.CfnBucket(scope, "MyBucket")
Mixins.of(bucket).apply(CustomVersioningMixin())

Construct Selection

Mixins operate on construct trees and can be applied selectively:

# Apply to all constructs in a scope
Mixins.of(scope).apply(EncryptionAtRest())

# Apply to specific resource types
Mixins.of(scope, ConstructSelector.resources_of_type(s3.CfnBucket)).apply(EncryptionAtRest())

# Apply to constructs matching a pattern
Mixins.of(scope, ConstructSelector.by_id(/.*-prod-.*/)).apply(ProductionSecurityMixin())

Built-in Mixins

Cross-Service Mixins

EncryptionAtRest: Applies encryption to supported AWS resources

# Works across different resource types
bucket = s3.CfnBucket(scope, "Bucket")
Mixins.of(bucket).apply(EncryptionAtRest())

log_group = logs.CfnLogGroup(scope, "LogGroup")
Mixins.of(log_group).apply(EncryptionAtRest())

S3-Specific Mixins

AutoDeleteObjects: Configures automatic object deletion for S3 buckets

bucket = s3.CfnBucket(scope, "Bucket")
Mixins.of(bucket).apply(AutoDeleteObjects())

EnableVersioning: Enables versioning on S3 buckets

bucket = s3.CfnBucket(scope, "Bucket")
Mixins.of(bucket).apply(EnableVersioning())

L1 Property Mixins

For every CloudFormation resource, CDK Mixins automatically generates type-safe property mixins. These allow you to apply L1 properties with full TypeScript support:

from aws_cdk.mixins_preview.with import


bucket = s3.Bucket(scope, "Bucket").with(CfnBucketPropsMixin(
    versioning_configuration=CfnBucketPropsMixin.VersioningConfigurationProperty(status="Enabled"),
    public_access_block_configuration=CfnBucketPropsMixin.PublicAccessBlockConfigurationProperty(
        block_public_acls=True,
        block_public_policy=True
    )
))

Property mixins support two merge strategies:

# bucket: s3.CfnBucket


# MERGE (default): Deep merges properties with existing values
Mixins.of(bucket).apply(CfnBucketPropsMixin(CfnBucketMixinProps(versioning_configuration=CfnBucketPropsMixin.VersioningConfigurationProperty(status="Enabled")), strategy=PropertyMergeStrategy.MERGE))

# OVERWRITE: Replaces existing property values
Mixins.of(bucket).apply(CfnBucketPropsMixin(CfnBucketMixinProps(versioning_configuration=CfnBucketPropsMixin.VersioningConfigurationProperty(status="Enabled")), strategy=PropertyMergeStrategy.OVERRIDE))

Property mixins are available for all AWS services:

from aws_cdk.mixins_preview.aws_logs.mixins import CfnLogGroupPropsMixin
from aws_cdk.mixins_preview.aws_lambda.mixins import CfnFunctionPropsMixin
from aws_cdk.mixins_preview.aws_dynamodb.mixins import CfnTablePropsMixin

Error Handling

Mixins provide comprehensive error handling:

# Graceful handling of unsupported constructs
Mixins.of(scope).apply(EncryptionAtRest()) # Skips unsupported constructs

# Strict application that requires all constructs to match
Mixins.of(scope).must_apply(EncryptionAtRest())

Project details


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_mixins_preview-2.229.1a0.tar.gz (28.1 MB view details)

Uploaded Source

Built Distribution

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

aws_cdk_mixins_preview-2.229.1a0-py3-none-any.whl (28.5 MB view details)

Uploaded Python 3

File details

Details for the file aws_cdk_mixins_preview-2.229.1a0.tar.gz.

File metadata

File hashes

Hashes for aws_cdk_mixins_preview-2.229.1a0.tar.gz
Algorithm Hash digest
SHA256 79185a0f894c3855543bdf174cd795cf9d375e4c98a1edc1f07e082726c345f0
MD5 6a8b2cd5acacbd08f26ea9fce60dfa72
BLAKE2b-256 af7f54bb4409386617357f9413ba637ad4da2c8ba622a800af320abc126f2c76

See more details on using hashes here.

File details

Details for the file aws_cdk_mixins_preview-2.229.1a0-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_cdk_mixins_preview-2.229.1a0-py3-none-any.whl
Algorithm Hash digest
SHA256 40564686ddeebd8643c14f4838bb98a6bc344579556115773866629590598b78
MD5 9c4c32711f602fbc2edc871450b30e93
BLAKE2b-256 ec1750e86df61ce4e3c1ef5609a706eb069e62532e5a211ed52062fa0b351037

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