question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(module name): short issue description

See original GitHub issue

According to this https://github.com/aws/aws-cdk/issues/10348#issuecomment-692685610 we can import existing bucket if the bucket is created in the same cdk app.

const sourcebucket = s3.Bucket.fromBucketAttributes(this, 'SourceBucket', {
      bucketArn : 'arn:aws:s3:::test-bucket'
    }
    );
const cfnBucket = sourcebucket.node.defaultChild as s3.CfnBucket;

cfnBucket.replicationConfiguration is still giving me Cannot set property ‘replicationConfiguration’ of undefined error.

This test-bucket is being created in the same cdk app.

Use Case:

I have 2 stack. One i am using to create bucket and another is to add replication configuration.

Please let me know if there is another way to do this.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
peterwoodworthcommented, Jul 29, 2021

Thanks for the reply @jumic, saved me some time here. This is a great solution to the question asked!

@krishansubudhi imported buckets will work the same way regardless if the bucket was originally created in the same CDK app or not. That is; they will return an IBucket which as jumic explained, doesn’t contain a CfnBucket.

The comment you linked was saying that:

  • You cannot set replication configuration on an imported bucket (unless you use a custom resource)
  • The only way to set replication configuration with the CDK is to use the Bucket construct and use an escape hatch, or simply use the CfnBucket construct

Is there a reason you want to set replication configuration in the second stack instead of the stack where you initially create the bucket? Creating two stacks to do this is unnecessary so unless you have a reason to do this in two separate stacks I’d advise against it 😄

1reaction
jumiccommented, Jul 29, 2021

s3.Bucket.fromBucketAttributes doesn’t create a new S3 bucket. It only imports an existing bucket. Hence it doesn’t contain a CfnBucket (which would create a new bucket).

However, you could pass the bucket object from the first to the second stack. In this case, you could split the definition of the S3 bucket and the replication configuration into to CDK stacks in a CDK app.

Example of both stacks:

export class BucketStack extends cdk.Stack {
  bucket: s3.Bucket;
  destinationBucket: s3.Bucket;

  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    this.bucket = new s3.Bucket(this, 'MyBucket');
    this.destinationBucket = new s3.Bucket(this, 'DestinationBucket');

  }
}

interface ImportStackProps extends cdk.StackProps {
  bucket: s3.Bucket;
  destinationBucket: s3.Bucket;
}

export class ImportStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: ImportStackProps) {
    super(scope, id, props);

    const cfnBucket = props.bucket.node.defaultChild as s3.CfnBucket;
    cfnBucket.replicationConfiguration = {
      role: 'myrole',
      rules:[
        {
          destination:{
            bucket: props.destinationBucket.bucketName
          },
          prefix: "target_prefix",
          status: "Enabled",
          filter: {
            prefix: "source_prefix"
          },
        },
      ],
    };
    
  }
}

In the app, pass the buckets to the second stack:

const app = new cdk.App();
const bucketStack = new BucketStack(app, 'BucketStack');
new ImportStack(app, 'ImportStack', {
  bucket: bucketStack.bucket,
  destinationBucket: bucketStack.destinationBucket,
});

Note:

  • This example doesn’t show how to setup everything to use the replication. It’s only an example which demonstrates how to setup property replicationConfiguration in a second stack.
  • Please check the generated CloudFormation templates in folder cdk.out. In the CloudFormation templates that are generated by CDK, the bucket and the replicationConfiguration will be moved to the first stack. This is correct because all properties have to be merged into a single CloudFormation resource (AWS::S3::Bucket).
Read more comments on GitHub >

github_iconTop Results From Across the Web

(module name): (short issue description) #20201 - GitHub
Describe the bug. Lang: python. Attempting to remove bootstrapped version rules etc from final synthesized yaml templates by customizing the ...
Read more >
Module Release, Project "short name" "messes up ... - Drupal
This is by design to disambiguate modules and other components that share a name across projects. Since https://www.drupal.org/project/dff was ...
Read more >
Module:Settlement short description - Wikipedia
This Lua module is used on approximately 689,000 pages, or roughly 1% of all pages. ... Used in Template:Infobox settlement to generate short...
Read more >
What is a module in software, hardware and programming?
In programming, a module is a section of code that is added in as a whole or is designed for easy reusability. What...
Read more >
Java SE 9 - JPMS module naming - Stephen Colebourne's blog
Modules are a group of packages. As such, the module name must be related to the package names. Module names are strongly recommended...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found