24

I am trying to code a CDK doing the job of writing some empty objects inside some folders that I need to be visible in my bucket.

I have found this answer https://serverfault.com/questions/957686/how-to-upload-a-file-into-s3-bucket-using-cloudformation-script showing the way in CloudFormation.

I wonder if somebody has done something similar with CDK.

Thank you

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Nicola Ben
  • 10,615
  • 8
  • 41
  • 65

1 Answers1

44

You can achieve this with @aws-cdk/aws-s3-deployment.

Using TypeScript:

import s3 = require('@aws-cdk/aws-s3');
import s3deploy = require('@aws-cdk/aws-s3-deployment');

const myBucket = new s3.Bucket(this, 'Bucket');

new s3deploy.BucketDeployment(this, 'DeployFiles', {
  sources: [s3deploy.Source.asset('./folder')], # 'folder' contains your empty files at the right locations
  destinationBucket: bucket,
});

Asset feature will require the execution of the command:

cdk bootstrap aws://<account>/<region>

that will run a cloudFormation and create a bucket with name cdktoolkit-stagingbucket-<random_chars>.

Nicola Ben
  • 10,615
  • 8
  • 41
  • 65
jogold
  • 6,667
  • 23
  • 41
  • That was very useful, I added an integration to your answer. Thank you, – Nicola Ben Dec 03 '19 at 09:32
  • I am trying to upload a jar file which is roughly 200MB and each time it failed I think it might be time out? Then I set the `memoryLimit` but it thrown out of memory exception. – RobotCharlie Aug 14 '20 at 02:32
  • The `memoryLimit` applies to the runtime memory of the lambda which does the copy under the hood but the lambda is construct to unzip the contents once copied but the `/tmp` directory has a hard limit of `512 MB`. So in case the data to extract from zip + zip contents, if that exceeds 500MB, this method won't be helpful as of now. Ref: https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html This issue is open as of now: https://github.com/aws/aws-cdk/issues/7950 – Arnab Das Jul 14 '21 at 13:37
  • how to get the version of the object in the destinationBucket? – mchlfchr Oct 03 '21 at 20:49
  • does this work for a jar file? – Geoff Langenderfer Aug 16 '22 at 02:01