4

I have imported an S3 bucket using below

const importbucket = s3.Bucket.fromBucketAttributes(this, 'ImportedBucket', {
  bucketArn: 'arn:aws:s3:::BUCKETNAME'
});

now I am trying to add lifecycle rule,

if the bucket is created in the stack I know we have 2 options like below

option 1 :

const nitinbucket = new s3.Bucket(this, 'bucket', {
  bucketName: 'sdasbktjsdhfksajdkdjlkas',
  removalPolicy: RemovalPolicy.DESTROY,
  versioned: false, 
});

nitinbucket.addLifecycleRule({
  abortIncompleteMultipartUploadAfter: Duration.days(7),
  enabled: true,
  expiration: Duration.days(75),
  id: 'rule',
});

Option 2:

const myBucket = new s3.Bucket(this, 'BuckyMcBucketface', {
  lifecycleRules: [
      {
          transitions: [
              {
                  storageClass: s3.StorageClass.INFREQUENT_ACCESS,
                  transitionAfter: cdk.Duration.days(30),
              },
          ],
      },
  ],
});

what I want is import an existing bucket and add transition rules to the bucket (similar to option 2)

Thanks !

shashwat
  • 7,851
  • 9
  • 57
  • 90
dheeraj
  • 49
  • 1
  • 8

1 Answers1

2

life cycle configuration is part of same cloudformation resource which creates S3 Bucket. Making changes to a resource that was created manually outside cloudformation/CDK is not supported unless we use a custom resource.

Here are some steps we can do without using a custom resource.

  • Create an empty cdk project with just 1 resource create s3 bucket (not import existing bucket) with same configuration as your current S3 bucket.
  • cdk synth and generate Cloud Formation Template.
  • Use cloudformation import process documented here and example for Here for DynamoDB.
Balu Vyamajala
  • 9,287
  • 1
  • 20
  • 42
  • I tried importing a bucket that is created manually and been created using CF. In both the cases i dont see the option to add lifecycle policy of imported s3. In my requirement all my S3 buckets are created using CF templates only. I will try your solution and will update you soon. – dheeraj Mar 17 '21 at 15:59
  • you followed cloudformation [import process](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resource-import-existing-stack.html) ? – Balu Vyamajala Mar 17 '21 at 16:01
  • Im following, for now we decided to go with simple awscli cmd to update lifecycle policies of an existing s3 bucket the input will be text file with list of s3 bucket followed by prefix delimeter is space. Give me like 3-4 days. I will update if I am able to do this. – dheeraj Mar 18 '21 at 21:49
  • Im following this https://medium.com/@visya/how-to-import-existing-aws-resources-into-cdk-stack-f1cea491e9 , but im getting an error while importing There was an error creating this change set You have modified resources [CDKMetadata] in your template that are not being imported. Update, create or delete operations cannot be executed during import operations. – dheeraj Mar 23 '21 at 17:41
  • are you generating a stack with `cdk --no-version-reporting deploy` , i would do this because it generates cloudformation without metadata. – Balu Vyamajala Mar 23 '21 at 17:53
  • i will do it with --no-version-reporting,i think metadata is conflicting, this is what im trying, create a sns topic , then add s3 bucket const with existing name and synth. then follow import resources steps. Then it complains sns topic needs to have DeletionPolicy, so I added it manually in cdk.out and passing that template but still it complains. – dheeraj Mar 23 '21 at 18:20
  • It started working, I will add the steps and accept your answer. Thanks for a quick response all the time. My above question still needs an answer. I followed your steps and im able to import s3 if my code only contains s3. But if i have created a topic already using cdk and i want to import an existing s3 then im getting above error. – dheeraj Mar 23 '21 at 18:34
  • @BaluVyamajala Can you please elaborate on how to use custom resource to add a new lifecycle policy to an existing bucket? I face the very same situation like OP but I can't create a new cdk project. –  May 10 '21 at 23:36