Copy data from an Amazon S3 bucket to another account and Region by using the AWS CLI
Appasaheb Bagali and Purushotham G K, Amazon Web Services
Summary
This pattern describes how to migrate data from a source Amazon Simple Storage Service (Amazon S3) bucket in an AWS account to a destination Amazon S3 bucket in another AWS account, either in the same AWS Region or in a different Region.
The source Amazon S3 bucket allows AWS Identity and Access Management (IAM) access by using an attached resource policy. A user in the destination account has to assume a role that has PutObject and GetObject permissions for the source bucket. Finally, you run copy and sync commands to transfer data from the source Amazon S3 bucket to the destination Amazon S3 bucket.
Accounts own the objects that they upload to Amazon S3 buckets. If you copy objects across accounts and Regions, you grant the destination account ownership of the copied objects. You can change the ownership of an object by changing its access control list (ACL) to bucket-owner-full-control. However, we recommend that you grant programmatic cross-account permissions to the destination account because ACLs can be difficult to manage for multiple objects.
Warning
This scenario requires IAM users with programmatic access and long-term credentials, which present a security risk. To help mitigate this risk, we recommend that you provide these users with only the permissions they require to perform the task and that you remove these users when they are no longer needed. Access keys can be updated if necessary. For more information, see Updating access keys in the IAM documentation.
Prerequisites and limitations
Prerequisites
Two active AWS accounts in the same or different AWS Regions.
An existing Amazon S3 bucket in the source account.
If your source or destination Amazon S3 bucket has default encryption enabled, you must modify the AWS Key Management Service (AWS KMS) key permissions. For more information, see the AWS re:Post article
on this topic. Familiarity with cross-account permissions.
Limitations
This pattern covers one-time migration. For scenarios that require continuous and automatic migration of new objects from a source bucket to a destination bucket, you can use Amazon S3 Batch Replication.
This patterns uses session credentials (
AccessKeyId,SecretAccessKey, andSessionToken) that are temporary and non-persistent. The expiration timestamp in the output indicates when these credentials expire. The role is configured with the maximum session duration. The copy job will be canceled if the session expires.
Architecture

Tools
AWS Command Line Interface (AWS CLI) is an open-source tool that helps you interact with AWS services through commands in your command line shell.
AWS Identity and Access Management (IAM) helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them.
Amazon Simple Storage Service (Amazon S3) is a cloud-based object storage service that helps you store, protect, and retrieve any amount of data.
Best practices
Security best practices in IAM (IAM documentation)
Applying least-privilege permissions (IAM documentation)
Epics
| Task | Description | Skills required |
|---|---|---|
Create an IAM user and get the access key. |
| AWS DevOps |
Create an IAM identity-based policy. | Create an IAM identity-based policy named
| AWS DevOps |
Create an IAM role. | Create an IAM role named
| AWS DevOps |
| Task | Description | Skills required |
|---|---|---|
Create and attach an Amazon S3 bucket policy. |
| Cloud administrator |
| Task | Description | Skills required |
|---|---|---|
Create a destination Amazon S3 bucket. |
| Cloud administrator |
| Task | Description | Skills required |
|---|---|---|
Configure the AWS CLI with the newly created user credentials. |
| AWS DevOps |
Assume the Amazon S3 migration role. |
For more information, see How do I use the AWS CLI to assume an IAM role? | AWS administrator |
Copy and synchronize data from the source bucket to the destination bucket. | When you have assumed the role Copy:
Synchronize:
| Cloud administrator |
Troubleshooting
| Issue | Solution |
|---|---|
An error occurred ( |
|
Related resources
Creating an Amazon S3 bucket (Amazon S3 documentation)
Amazon S3 bucket policies and user policies (Amazon S3 documentation)
IAM identities (users, groups, and roles) (IAM documentation)
cp command
(AWS CLI documentation) sync command
(AWS CLI documentation)