aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

BucketDeployment: single files

Open austinmw opened this issue 3 years ago • 5 comments

Describe the feature

Hi, I'd love to be able to deploy single files in addition to directories and zip files using BucketDeployment.

Use Case

Due to the directory structure of my codebase I'm not able to move all single files into a folder or zip prior to cdk deployment. Currently I'm working around this by using with TemporaryDirectory() as temp_dir: in my CDK stack and using this temp_dir as the s3_deployment.Source.asset, however, this feels like the wrong way to do things.

Proposed Solution

I assume there is a justification for not including this functionality originally, but it'd be great if it were added in some way.

Other Information

Essentially my project structure contains a CDK app and a python library. I'd like to be able to use poetry and the same pyproject.toml (with tool.poetry.group's) to install the dependencies for either.

Here's a shortened version of the directory structure:

├── Makefile
├── pyproject.toml
├── poetry.toml
├── poetry.lock
├── environment.yml
├── app.py
├── cdk.json
├── docs/
├── examples/
├── lifecycles/
├── src/mypythonlibrary/
├── sample_datasets/
├── stacks/
└── tests/

Ideally I would like the directories src, examples, and sample_datasets, as well as the single files Makefile, pyproject.toml, poetry.toml, poetry.lock, and environment.yml placed in my S3 bucket.

Acknowledgements

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

CDK version used

2.70.0 (build c13a0f1)

Environment details (OS name and version, etc.)

macOS Monterey 12.6.3

austinmw avatar Mar 25 '23 13:03 austinmw

Thank you. I am making it p2 feature request now and any pull request or upvotes would be appreciated.

pahud avatar Mar 27 '23 16:03 pahud

You can use Source.data for single files:

import { App, Stack } from 'aws-cdk-lib/core'
import { Bucket } from "aws-cdk-lib/aws-s3"
import { BucketDeployment, Source } from "aws-cdk-lib/aws-s3-deployment"
import * as fs from "fs";
import * as path from "path";

const app = new App();
const stack = new Stack(app, "Stack");
const integBucket = new Bucket(stack, "Bucket");
new BucketDeployment(stack, "Deploy", {
    sources: [
        Source.data("Readme.md", fs.readFileSync(path.resolve(__dirname, "..", "README.md")).toString())
    ],
    destinationBucket: integBucket
})

Styerp avatar Apr 16 '23 22:04 Styerp

How about files exceeding 100Mb?

coding-velociraptor avatar Oct 24 '24 14:10 coding-velociraptor

Another consquece of missing this API seems to be that you cannot easily associate object-level metadata to individual files, for example attaching content-type, a hash, cache-control, or whatever else might be required. This is ultimately what led me to find this issue.

The suggestion to use Source.data isn’t really helpful or possible for many cases. For example, we can’t use that for fonts, images, or audio files, which are relatively common to have bundled with an app deployment.

The workaround seems to just be to move each file to a temp directory or to zip each file. This seems to work fine, but it is really cumbersome as an end user of CDK.

If this work gets taken on it would also be great to be able to rename files as part of the API.

jnields avatar Dec 19 '24 12:12 jnields

I am noticing that the Source.data method basically already does all the work to create a temporary directory and ultimately deploy a single file moved into it.

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-s3-deployment/lib/source.ts#L160-L176

  public static data(objectKey: string, data: string): ISource {
    return {
      bind: (scope: Construct, context?: DeploymentSourceContext) => {
        const workdir = FileSystem.mkdtemp('s3-deployment');
        const outputPath = join(workdir, objectKey);
        const rendered = renderData(scope, data);
        fs.mkdirSync(dirname(outputPath), { recursive: true });
        fs.writeFileSync(outputPath, rendered.text);
        const asset = this.asset(workdir).bind(scope, context);
        return {
          bucket: asset.bucket,
          zipObjectKey: asset.zipObjectKey,
          markers: rendered.markers,
        };
      },
    };

Would you see any issue with updating this slightly to define a Source.file, and to define the source asset via a symlink, for example something like this?

  public static file(path: string, opts?: { objectKey: string }): ISource {
    return {
      bind: (scope: Construct, context?: DeploymentSourceContext) => {
        const workdir = FileSystem.mkdtemp('s3-deployment');
        const outputPath = join(workdir, opts.objectKey ?? nodePath.basename(path));
        fs.mkdirSync(dirname(outputPath), { recursive: true });
        fs.symlinkSync(path, outputPath);
        const asset = Source.asset(workdir).bind(scope, context);
        return {
          bucket: asset.bucket,
          zipObjectKey: asset.zipObjectKey,
        };
      },
    };

Usage might be something like this:

new BucketDeployment(this, "Deploy Image", {
  destinationBucket, role, 
  sources: [
    Source.file("/path/to/my-image.png", { objectKey: imageHash })
  ],
  contentType: "image/png",
  cacheControl: [
    CacheControl.setPublic(),
    CacheControl.maxAge(Duration.days(365)),
    CacheControl.immutable(),
  ],
});

If you don’t see problems with this and it looks good to you, I’d be happy to clean up some repeated logic there and raise a PR.

jnields avatar Dec 19 '24 14:12 jnields