aws / aws/aws-cdk

(aws_s3): add asset bundling functionality to aws_s3_deployment

Open
#27,214 1 comment 4 reactions 0 assignees View on GitHub
@aws-cdk/aws-s3 effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Extend the functionality of [S3 Assets - Asset Bundling](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3_assets-readme.html) functionality, to be able to build and bundle resources but pass them to a custom target, as an opposed to a CDK managed S3 bucket.

### Use Case

Use this functionality in collaboration with other resources such as the [S3 Deployment](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_s3_deployment-readme.html) construct, in order to create robust deployment of **_application_** managed resources such as front end assets and media content.

Alternative Options:

- Give the `aws_s3_deployment` construct a `bundling` parameter which could act the same as the `aws_s3_assets`.
- Create a type of "Asset Bundling" construct, which could then be passed as a `source` to `aws_s3_assets` - the retroactively update

### Proposed Solution

`aws_s3_assets` needs a `bucket` property, that will override its bundled destination target.

Update `sources` on `aws_s3_deployment` to be able to take an `aws_s3_assets` as a target. If it is passed a `aws_s3_asset` then it should set the bucket property for it, based in the `bucket` value of the deployment.

This implementation would not cause any BCs, as it would only take effect if the user overrides the `bucket` property. If omitted, `aws_s3_assets` would continue as normal to push to a CDK managed bucket.

### Other Information

**Scenario 1**: Creating an asset and giving it to a `BucketDeployment`.

In this example giving the `builtAsset` to the `BucketDeployment` overrides the `Asset`s new `bucket` property (done via setter when the deployment is constructed

```
const websiteBucket = new s3.Bucket(this, 'WebsiteBucket');

const builtAsset = new Asset(this, 'BundledAsset', {
path: path.join(__dirname, 'website'),
bundling: {
command: [
'npm build prod',
],
},
});

const deployment = new s3deploy.BucketDeployment(this, 'DeployWebsite', {
sources: [ builtAsset ],
destinationBucket: websiteBucket,
destinationKeyPrefix: 'web/static', // optional prefix in destination bucket
});
```

**Scenario 2**: Giving `Asset` a specified bucket.
In this example giving `Asset` a bucket, would put all built assets into the `websiteBucket` as opposed to the CDK managed one.

```
const websiteBucket = new s3.Bucket(this, 'WebsiteBucket');

const builtAsset = new Asset(this, 'BundledAsset', {
path: path.join(__dirname, 'website'),
bucket: websiteBucket
bundling: {
command: [
'npm build prod',
],
},
});
```

### Acknowledgements

- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.96.2

### Environment details (OS name and version, etc.)

OSX Ventura 13.5.2

Contributor guide

Open the contributing guide

Research direction

Start by reading the S3 Assets and S3 Deployment documentation linked in the issue, then trace the aws_s3_assets and aws_s3_deployment construct entry points. Compare the proposed bucket override and source behavior with both scenarios; done means bundled assets can target the deployment bucket without changing existing managed-bucket behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.