Example 1: Configuring replication when the source and destination buckets are owned by the same account
In this example, you set up replication for source and destination buckets that are owned by the same AWS account. Examples are provided for using the Amazon S3 console, the AWS Command Line Interface (AWS CLI), and the AWS SDK for Java and AWS SDK for .NET.
Topics
For step-by-step instructions, see How Do I Add a Replication Rule to an S3 Bucket? in the Amazon Simple Storage Service Console User Guide. This topic provides instructions for setting replication configuration when buckets are owned by same and different AWS accounts.
To use the AWS CLI to set up replication when the source and destination buckets are owned by the same AWS account, you create source and destination buckets, enable versioning on the buckets, create an IAM role that gives Amazon S3 permission to replicate objects, and add the replication configuration to the source bucket. To verify your setup, you test it.
To set up replication when source and destination buckets are owned by the same AWS account
-
Set a credentials profile for the AWS CLI. In this example, we use the profile name
acctA. For information about setting credential profiles, see Named Profiles in the AWS Command Line Interface User Guide.Important The profile you use for this exercise must have the necessary permissions. For example, in the replication configuration, you specify the IAM role that Amazon S3 can assume. You can do this only if the profile you use has the
iam:PassRolepermission. For more information, see Granting a User Permissions to Pass a Role to an AWS Service in the IAM User Guide. If you use administrator user credentials to create a named profile, you can perform all the tasks. -
Create a
sourcebucket and enable versioning on it. The following code creates asourcebucket in the US East (N. Virginia) (us-east-1) Region.aws s3api create-bucket \ --bucketsource\ --region us-east-1 \ --profile acctAaws s3api put-bucket-versioning \ --bucketsource\ --versioning-configuration Status=Enabled \ --profile acctA -
Create a
destinationbucket and enable versioning on it. The following code creates adestinationbucket in the US West (Oregon) (us-west-2) Region.Note To set up replication configuration when both source and destination buckets are in the same AWS account, you use the same profile. This example uses
acctA. To test replication configuration when the buckets are owned by different AWS accounts, you specify different profiles for each. This example uses theacctBprofile for the destination bucket.aws s3api create-bucket \ --bucketdestination\ --region us-west-2 \ --create-bucket-configuration LocationConstraint=us-west-2 \ --profile acctAaws s3api put-bucket-versioning \ --bucketdestination\ --versioning-configuration Status=Enabled \ --profile acctA -
Create an IAM role. You specify this role in the replication configuration that you add to the
sourcebucket later. Amazon S3 assumes this role to replicate objects on your behalf. You create an IAM role in two steps:-
Create a role.
-
Attach a permissions policy to the role.
-
Create the IAM role.
-
Copy the following trust policy and save it to a file named
S3-role-trust-policy.jsonin the current directory on your local computer. This policy grants Amazon S3 service principal permissions to assume the role.{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Principal":{ "Service":"s3.amazonaws.com" }, "Action":"sts:AssumeRole" } ] } -
Run the following command to create a role.
$aws iam create-role \ --role-name replicationRole \ --assume-role-policy-document file://s3-role-trust-policy.json \ --profile acctA
-
-
Attach a permissions policy to the role.
-
Copy the following permissions policy and save it to a file named
S3-role-permissions-policy.jsonin the current directory on your local computer. This policy grants permissions for various Amazon S3 bucket and object actions.{ "Version":"2012-10-17", "Statement":[ { "Effect":"Allow", "Action":[ "s3:GetObjectVersionForReplication", "s3:GetObjectVersionAcl" ], "Resource":[ "arn:aws:s3:::source-bucket/*" ] }, { "Effect":"Allow", "Action":[ "s3:ListBucket", "s3:GetReplicationConfiguration" ], "Resource":[ "arn:aws:s3:::source-bucket" ] }, { "Effect":"Allow", "Action":[ "s3:ReplicateObject", "s3:ReplicateDelete", "s3:ReplicateTags", "s3:GetObjectVersionTagging" ], "Resource":"arn:aws:s3:::destination-bucket/*" } ] } -
Run the following command to create a policy and attach it to the role.
$aws iam put-role-policy \ --role-name replicationRole \ --policy-document file://s3-role-permissions-policy.json \ --policy-name replicationRolePolicy \ --profile acctA
-
-
-
Add replication configuration to the
sourcebucket.-
Although the Amazon S3 API requires replication configuration as XML, the AWS CLI requires that you specify the replication configuration as JSON. Save the following JSON in a file called
replication.jsonto the local directory on your computer.{ "Role": "IAM-role-ARN", "Rules": [ { "Status": "Enabled", "Priority": 1, "DeleteMarkerReplication": { "Status": "Disabled" }, "Filter" : { "Prefix": "Tax"}, "Destination": { "Bucket": "arn:aws:s3:::destination-bucket" } } ] } -
Update the JSON by providing values for the
destination-bucketandIAM-role-ARN. Save the changes. -
Run the following command to add the replication configuration to your source bucket. Be sure to provide the
sourcebucket name.$aws s3api put-bucket-replication \ --replication-configuration file://replication.json \ --bucketsource\ --profile acctA
To retrieve the replication configuration, use the
get-bucket-replicationcommand.$aws s3api get-bucket-replication \ --bucketsource\ --profile acctA -
-
Test the setup in the Amazon S3 console:
-
Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/
. -
In the
sourcebucket, create a folder namedTax. -
Add sample objects to the
Taxfolder in thesourcebucket.Note The amount of time it takes for Amazon S3 to replicate an object depends on the size of the object. For information about how to see the status of replication, see Replication status information.
In the
destinationbucket, verify the following:-
That Amazon S3 replicated the objects.
-
In object properties, that the Replication Status is set to
Replica(identifying this as a replica object). -
In object properties, that the permission section shows no permissions. This means that the replica is still owned by the
sourcebucket owner, and thedestinationbucket owner has no permission on the object replica. You can add optional configuration to tell Amazon S3 to change the replica ownership. For an example, see Example 3: Changing the replica owner when the source and destination buckets are owned by different accounts.
-
-
Update an object's ACL in the
sourcebucket and verify that changes appear in thedestinationbucket.For instructions, see How Do I Set Permissions on an Object? in the Amazon Simple Storage Service Console User Guide.
-
Use the following code examples to add a replication configuration to a bucket with the AWS SDK for Java and AWS SDK for .NET, respectively.

