This AWS CDK v1 construct allows you to deploy local files or directories to an Amazon S3 bucket. It handles packaging (e.g., zipping) and uploading the assets, making them available for other CDK constructs or applications. As part of AWS CDK v1, it is currently in maintenance mode, with v2 being the active major version. The latest v1 release is 1.204.0.
pip install aws-cdk.aws-s3-assetsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create an `Asset` from a local directory. It creates a temporary folder, adds a file, defines a CDK stack that uses `Asset` to reference this folder, and outputs the S3 details. To run, save as `app.py`, then execute `cdk synth` in the same directory. This generates the CloudFormation template.
Refer to the AWS CDK v2 migration guide. All constructs are now under the single 'aws-cdk' package (e.g., `from aws_cdk.aws_s3_assets import Asset` in v1 becomes `from aws_cdk import aws_s3_assets` and then `aws_s3_assets.Asset` in v2, after installing `aws-cdk` instead of individual construct packages).
Ensure asset content changes to trigger redeployment. If you need to force a redeployment with identical content (e.g., for testing), you might have to temporarily modify the asset content or use a unique identifier in the asset path/ID.
To automatically delete assets upon stack destruction (e.g., for development/testing), set the `removal_policy` on the `Asset` construct to `core.RemovalPolicy.DESTROY`.
Ensure the path is correct and accessible. Avoid hardcoded absolute paths that might not be consistent across environments. Use `os.path.join(os.path.dirname(__file__), 'my_asset_dir')` for paths relative to the current Python file.
Verify the `path` argument. Ensure it refers to an existing local file or directory. Use `os.path.abspath()` or `os.path.join(os.path.dirname(__file__), 'my_folder')` for robust pathing.
For CDK v1, ensure `pip install aws-cdk.aws-s3-assets`. The correct import is `from aws_cdk.aws_s3_assets import Asset`.
To get the S3 bucket name and object key after deployment, use `asset.s3_bucket_name` and `asset.s3_object_key`. These are string tokens that resolve at deployment time.
Ensure each `Asset` construct has a unique ID within its scope. If you need to deploy different versions of an asset, either update the asset's content (which changes its hash) or define them as separate `Asset` constructs with unique IDs.