Create a basic S3 replication rule
📑 On this page
Part 73 of AWS from Zero. This lesson keeps the scope to one S3 behavior you can verify from the terminal.
What we are learning
A replication rule connects a source bucket, an IAM role trusted by S3, a filter, and a destination bucket ARN.
Before you run it
SOURCE_BUCKET="replace-with-versioned-source"
DEST_BUCKET="replace-with-versioned-destination"
REPLICATION_ROLE_ARN="arn:aws:iam::123456789012:role/S3ReplicationRole"Create and test the replication IAM role first. Both buckets must have versioning enabled.
Cost note: Replication writes a second copy and may add cross-Region transfer and KMS requests.
The command
cat > replication.json <<EOF
{
"Role": "$REPLICATION_ROLE_ARN",
"Rules": [{
"ID": "ReplicateReports",
"Status": "Enabled",
"Priority": 1,
"DeleteMarkerReplication": {"Status": "Disabled"},
"Filter": {"Prefix": "reports/"},
"Destination": {"Bucket": "arn:aws:s3:::$DEST_BUCKET"}
}]
}
EOF
aws s3api put-bucket-replication \
--bucket "$SOURCE_BUCKET" \
--replication-configuration file://replication.jsonA successful configuration command may return no output. Treat inspection as a separate required step.
Inspect the result
aws s3api get-bucket-replication \
--bucket "$SOURCE_BUCKET" \
--query "ReplicationConfiguration.Rules[].{ID:ID,Status:Status,Prefix:Filter.Prefix,Destination:Destination.Bucket}" \
--output tableRead the returned fields rather than assuming the write succeeded exactly as intended.
One tiny variation
echo "replication test" > replication.txt
aws s3 cp replication.txt "s3://$SOURCE_BUCKET/reports/replication.txt"Replication is asynchronous. Poll the destination rather than expecting immediate consistency.
Common mistake
A valid-looking configuration can still fail at runtime because of role trust, bucket policy, ownership, KMS, or destination permission errors.
Cleanup
aws s3api delete-bucket-replication --bucket "$SOURCE_BUCKET"
rm replication.json replication.txtRemove the rule. Delete test object versions from both buckets separately.
Next, we will learn Audit an S3 replication configuration.