简体   繁体   中英

S3 Bucket replication using CDK in Python

I am trying to do Cross region replication using Python in CDK. I have enabled versioning on both bucket and added policy to replicate object on destination bucket. I Want to add "replication rule configuration" to source bucket,Have got process to do using yaml in cloudformation template.

But i want to implement same using Python. Can anyone please suggest something for this. Thanks in Advance!

I had a use case where I had to enable bucket replication for my bucket with multiple destination buckets.

I tried to replicate the policy defined here - https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/730#issuecomment-753692737

Here is a snippet of my code

    my_dest_buckets = ["bucket-1", "bucket-2"]
    s3.CfnBucket(
        self,
        "my-id",
        bucket_name="my-bucket",
        public_access_block_configuration=s3.BlockPublicAccess.BLOCK_ALL,
        bucket_encryption=s3.CfnBucket.BucketEncryptionProperty(
            server_side_encryption_configuration=[
                s3.CfnBucket.ServerSideEncryptionRuleProperty(
                    server_side_encryption_by_default=s3.CfnBucket.ServerSideEncryptionByDefaultProperty(
                        sse_algorithm="AES256"
                    )
                )
            ]
        ),
        ownership_controls=s3.CfnBucket.OwnershipControlsProperty(
            rules=[
                s3.CfnBucket.OwnershipControlsRuleProperty(
                    object_ownership="BucketOwnerPreferred"
                )
            ]
        ),
        versioning_configuration=s3.CfnBucket.VersioningConfigurationProperty(
            status="Enabled"
        ),
        replication_configuration=s3.CfnBucket.ReplicationConfigurationProperty(
            
            role=f"arn:aws:iam::{self.account}:role/my-role",
            rules=[
                # Creating rules with destination buckets other than bucket in main region
                s3.CfnBucket.ReplicationRuleProperty(
                    id=f"rule-{bucket}",
                    destination=s3.CfnBucket.ReplicationDestinationProperty(
                        bucket=f"arn:aws:s3:::{bucket}",
                    ),
                    delete_marker_replication=s3.CfnBucket.DeleteMarkerReplicationProperty(
                        status="Disabled"
                    ),
                    status="Enabled",
                    priority=count,
                    filter=s3.CfnBucket.ReplicationRuleFilterProperty(prefix="")
                    # This is an empty filter which we have added so that AWS
                    # uses latest schema (V2). By default it used old schema which allows only
                    # one destination bucket.
                )
                # For all the dest buckets
                for count, bucket in enumerate(
                    [bucket for bucket in my_dest_buckets]
                )
            ],
        ),
    )

I'm not sure if this is helpfull at all, but I was bound to the Bucket Class in Java (and not CfnBucket ) and therefore needed a little workaround.

final Bucket bucket = Bucket.Builder.create(this, bucketName)
                .bucketName(bucketName)
                .publicReadAccess(live)
                ...

CfnBucket.ReplicationConfigurationProperty replicationConfigurationProperty = CfnBucket.ReplicationConfigurationProperty.builder()
                .role(replicationRole.getRoleArn())
                .rules(...)
                ...

CfnBucket cfnBucket = (CfnBucket)bucket.getNode().getDefaultChild();
cfnBucket.setReplicationConfiguration(replicationConfigurationProperty);

Current cdk "S3Bucket" construct do not has direct replication method exposed. Its in AWS's feature list. But you can do with using CfnS3Bucket class. Here you need to create the two stack one in primary region and secondary region, which will create the two buckets, one in one region and second in another region. And using Cfn constructs you can easily achieve the replication.

Sample repo for your reference: https://github.com/techcoderunner/s3-bucket-cross-region-replication-cdk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM