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

1 Answer 1

39

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

5
  • That was very useful, I added an integration to your answer. Thank you,
    – Nicola Ben
    Dec 3, 2019 at 9: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. Aug 14, 2020 at 2: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: docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html This issue is open as of now: github.com/aws/aws-cdk/issues/7950
    – Arnab Das
    Jul 14, 2021 at 13:37
  • how to get the version of the object in the destinationBucket?
    – mchlfchr
    Oct 3, 2021 at 20:49
  • does this work for a jar file? Aug 16, 2022 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.