aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.6k stars 3.9k forks source link

(aws_s3): add asset bundling functionality to aws_s3_deployment #27214

Open myerscf opened 1 year ago

myerscf commented 1 year ago

Describe the feature

Extend the functionality of S3 Assets - Asset Bundling 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 construct, in order to create robust deployment of application managed resources such as front end assets and media content.

Alternative Options:

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 Assets 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

CDK version used

2.96.2

Environment details (OS name and version, etc.)

OSX Ventura 13.5.2

khushail commented 1 year ago

Hi @myerscf , thanks for reaching out with this request. Yes, it would be a good addItion to have such prop in AssetProps or any other alternate provided.