AWS CDK Assets (Deprecated)

1.204.0 · deprecated · verified Thu Apr 16

The `aws-cdk-assets` module (v1.x) for the AWS Cloud Development Kit (CDK) is deprecated. Its functionality, primarily for S3 and ECR assets, has been consolidated into specific, dedicated asset modules such as `aws-cdk.aws_s3_assets` and `aws-cdk.aws_ecr_assets` within CDK v1, or `aws_cdk_lib.aws_s3_assets` and `aws_cdk_lib.aws_ecr_assets` for CDK v2. It currently publishes alongside `aws-cdk` v1 releases, but its direct use is strongly discouraged.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the recommended pattern for S3 assets within AWS CDK (v2+). Instead of `aws-cdk-assets`, you directly import and use `aws_s3_assets` from the `aws-cdk-lib` package. For CDK v1, replace `aws_cdk_lib` with `aws_cdk` and ensure you `pip install aws-cdk.aws-s3-assets`.

import os
from aws_cdk import (  # aws-cdk-lib for v2
    App, Stack,
    aws_s3_assets as s3_assets,
    aws_s3 as s3
)

class MyAssetStack(Stack):
    def __init__(self, scope: App, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create a dummy directory for the asset
        asset_dir = 'my-asset-content'
        os.makedirs(asset_dir, exist_ok=True)
        with open(os.path.join(asset_dir, 'hello.txt'), 'w') as f:
            f.write('Hello, CDK Assets!')

        # Define an S3 Asset (CDK v2 example)
        my_asset = s3_assets.Asset(self, 'MyFileAsset',
            path=asset_dir
        )

        # Create an S3 Bucket and grant read access to the asset
        bucket = s3.Bucket(self, 'MyBucket',
            versioned=True  # Assets can be versioned
        )

        # Output the asset S3 path
        self.asset_s3_url = my_asset.s3_object_url
        print(f"Asset S3 URL: {self.asset_s3_url}")

        # Clean up dummy directory (optional)
        # import shutil
        # shutil.rmtree(asset_dir)

if __name__ == '__main__':
    app = App()
    stack = MyAssetStack(app, 'MyAssetStackExample')
    # Uncomment to synthesize for deployment
    # app.synth()

view raw JSON →