How to enable encryption for all S3 buckets

Encryption for S3 bucket can be enabled from the AWS console itself. But if the number of buckets are more, its difficult to do this manually.

This article explains one Python script to enable encryption for all S3 buckets.

import boto3
from botocore.exceptions import ClientError

client = boto3.client(
    region_name=' ',
    aws_access_key_id=' ',
    aws_secret_access_key=' '

response = client.list_buckets()
SSECNF = 'ServerSideEncryptionConfigurationNotFoundError'
for bucket in response['Buckets']:
    bucket = client.get_bucket_encryption(Bucket=bucket['Name'])
except client.exceptions.ClientError as e:
    if e.response['Error']['Code'] == SSECNF:
        'Rules': [
            'ApplyServerSideEncryptionByDefault': {
                'SSEAlgorithm': 'AES256'
        print("Unexpected error: %s" % e)

Here the value of region_name, aws_access_key_id and aws_secret_access_key are need to be updated with corresponding values.

That's all…