Search code examples
pythonamazon-web-servicesamazon-s3aws-cdkamazon-kms

Adding encryption key to already created S3 Bucket object


I have a bucket created in a different region without encryption. I want to add a KMS encryption to the bucket object

@staticmethod
def change_bucket_encryption(
        stack: Stack,
        bucket_arn: str,
        bucket_id: str,
        kms_key: aws_kms.Key) -> s3.Bucket:
    """Change bucket encryption."""
    bucket = s3.Bucket.from_bucket_arn(
        scope=stack,
        id=bucket_id,
        bucket_arn=bucket_arn
    )
    bucket.encryption_key = kms_key
    return bucket

This is throwing the error:

AttributeError: can't set attribute 'encryption_key'

Solution

  • As per the AWS CDK documentation correct key is encryptionKey and not encryption_key

    Reference: https://docs.aws.amazon.com/cdk/api/v1/docs/aws-s3-readme.html

    Or if you're not using this way then share more details on the framework, language, sdk, etc.