Sub topic 15
Replicating objects across buckets and Regions
This chapter assumes you can create a bucket, enable versioning, and read an IAM policy. Replication copies new objects from a source bucket to a destination bucket automatically. It builds on two earlier ideas: versioning, which replication requires, and Regions, which decide where the copies live.
What replication does
S3 replication is asynchronous. After you write an object to the source bucket, S3 copies it to the destination according to rules you define. There are two common forms:
- Cross-Region replication (CRR) copies to a bucket in a different Region. Use it for lower latency for distant readers, or to keep a copy away from a single-Region failure.
- Same-Region replication (SRR) copies to a bucket in the same Region. Use it to keep a separate copy under a different account or owner, or to aggregate logs.
Replication is not a backup of history and not a sync tool. It copies only objects written after the rule is active, and the copy is not instant.
Requirements
- Versioning must be enabled on both the source and destination buckets.
- S3 needs an IAM role it can assume to read from the source and write to the destination.
- The destination can be in the same account or another account. For another account, the destination bucket policy must also allow the replication role.
Creating a rule
Save this as replication.json. Replace the account ID and bucket names with your own.
{
"Role": "arn:aws:iam::111122223333:role/s3-replication-role",
"Rules": [
{
"ID": "replicate-reports",
"Status": "Enabled",
"Priority": 1,
"Filter": { "Prefix": "reports/" },
"DeleteMarkerReplication": { "Status": "Disabled" },
"Destination": {
"Bucket": "arn:aws:s3:::my-example-bucket-replica-123456789012",
"StorageClass": "STANDARD_IA"
}
}
]
}Apply it to the source bucket:
aws s3api put-bucket-replication \
--bucket my-example-bucket-123456789012 \
--replication-configuration file://replication.jsonThe filter limits the rule to keys under reports/. The destination storage class can differ from the source, which lets you keep the replica in a cheaper class.
How it interacts with other features
- Existing objects are not replicated by a new rule. Use S3 Batch Replication to copy objects that already exist.
- Deletes: with versioning, a delete in the source creates a delete marker. Delete marker replication is optional, as set above. Deleting a specific version is never replicated, which protects the replica from malicious or accidental version deletion.
- Lifecycle rules run independently in each bucket. An expiration in the source does not remove the replica's objects.
- Replicas are not chained. An object that is itself a replica is not replicated again to a third bucket by default.
- Encryption: objects encrypted with SSE-KMS need extra permissions on the KMS keys, and the rule must be configured to replicate them.
Common errors
| Symptom | Likely cause |
|---|---|
| Rule cannot be saved | Versioning is off on the source or destination |
| Objects stay in `PENDING` or become `FAILED` | The role lacks permissions, or the destination policy blocks it |
| Old objects are missing in the destination | Replication only applies to new writes; run Batch Replication |
| Replica still exists after a source delete | Delete markers are not replicated, or a specific version was deleted |
You can check an object's state with aws s3api head-object and read the ReplicationStatus field. It shows PENDING, COMPLETED, or FAILED on the source, and REPLICA on the destination.
Exercise
You enabled a replication rule on a bucket that already holds 10,000 objects. A week later, only objects uploaded after the rule was created appear in the destination. Why, and how do you fix it?
Show answer
Replication applies only to objects written after the rule is active. To copy the existing 10,000 objects, run S3 Batch Replication against the source bucket.
Exercise
A teammate permanently deletes a specific object version in the source bucket. Does the replica lose that version?
Show answer
No. Deletes of specific version IDs are not replicated, so the replica keeps its copy.