AWS Builder Center

How to Copy S3 Bucket Objects Between Two AWS Accounts Using AWS CLI

Learn how to securely copy objects between two AWS accounts using Amazon S3, IAM policies, S3 bucket policies, and AWS CLI. This step-by-step guide covers cross-account permissions, configuration, synchronization, verification, and real-world best practices for secure and efficient data migration.

DevOps Engineer | AWS | Terraform | Docker | Kubernetes | CI/CD
A Practical Step-by-Step Guide to Cross-Account S3 Data Migration
In real-world cloud environments, organizations often maintain multiple AWS accounts for development, testing, staging, and production.
There are situations where you need to migrate or share data between these accounts. One common requirement is copying objects from an Amazon S3 bucket in one AWS account to another.
In this guide, we'll explore how to securely copy objects between two S3 buckets owned by different AWS accounts using AWS IAM permissions, S3 bucket policies, and the AWS CLI.

1. Understanding the Architecture

Before getting started, let's understand the setup.
We'll use two AWS accounts:
  • Source Account: Contains the original S3 bucket and objects.
  • Destination Account: Contains the S3 bucket where we want to copy the objects.
Our objective: Allow an authorized identity in the Destination Account to read objects from the Source Account and copy them into the destination bucket.

2. Prerequisites

Make sure you have:
  1. Access to both AWS accounts.
  1. An existing S3 bucket in the Source Account.
  1. An existing S3 bucket in the Destination Account.
  1. An IAM identity in the Destination Account with the required permissions.
  1. AWS CLI installed and configured.
Note: For production environments, prefer an IAM role with temporary credentials over long-term IAM user access keys.

3. Configure the Source AWS Account

First, we need to allow the Destination AWS Account to access objects in the source bucket.
Step 1: Log in to the Source AWS Account.
Step 2: Navigate to Amazon S3 and select your source bucket.
Step 3: Open the Permissions tab and locate Bucket policy.
Step 4: Add the following bucket policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowDestinationAccountToReadObjects",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::DESTINATION_ACCOUNT_ID:root"
},
"Action": [
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::SOURCE_BUCKET_NAME"
},
{
"Sid": "AllowDestinationAccountToGetObjects",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::DESTINATION_ACCOUNT_ID:root"
},
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::SOURCE_BUCKET_NAME/*"
}
]
}
Replace:
  •  DESTINATION_ACCOUNT_ID  with your destination AWS account ID.
  •  SOURCE_BUCKET_NAME  with your source S3 bucket name.
Why is this required?
This bucket policy delegates access to identities in the destination account, provided those identities also have the necessary IAM permissions.

4. Configure the Destination AWS Account

Now, let's configure permissions in the Destination Account.
Step 1: Log in to the Destination AWS Account.
Step 2: Create or select an IAM identity that will perform the copy operation.
Step 3: Attach an IAM policy with the following permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadSourceBucket",
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::SOURCE_BUCKET_NAME"
},
{
"Sid": "ReadSourceObjects",
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::SOURCE_BUCKET_NAME/*"
},
{
"Sid": "AccessDestinationBucket",
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::DESTINATION_BUCKET_NAME"
},
{
"Sid": "WriteDestinationObjects",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:AbortMultipartUpload"
],
"Resource": "arn:aws:s3:::DESTINATION_BUCKET_NAME/*"
}
]
}
Replace the bucket name placeholders with your actual bucket names.
Important: The IAM policy grants permissions to the destination identity, while the source bucket policy allows that identity's AWS account to access the source bucket. Both sides must be configured correctly.

5. Configure AWS CLI

Install and configure AWS CLI if you haven't already.
Installation guide:
Configure your AWS CLI credentials using the destination account identity:
aws configure
You can verify which AWS identity is being used with:
aws sts get-caller-identity
Make sure the returned account ID belongs to your Destination AWS Account.

6. Copy Objects Between the S3 Buckets

Once the permissions are configured, execute the following command:
aws s3 sync \
s3://SOURCE_BUCKET_NAME \
s3://DESTINATION_BUCKET_NAME
That's it!
The AWS CLI compares the source and destination objects and copies new or updated objects from the source bucket to the destination bucket.
For example:
aws s3 sync \
s3://company-source-data \
s3://company-destination-data
Useful options:
  •  --dryrun  – Preview the operations without actually copying objects.
  •  --delete  – Remove destination objects that no longer exist in the source. Use this carefully!
  •  --only-show-errors  – Display only errors during execution.
For example, preview the operation first:
aws s3 sync \
s3://SOURCE_BUCKET_NAME \
s3://DESTINATION_BUCKET_NAME \
--dryrun

7. Verify the Copied Objects

After the synchronization completes, verify the destination bucket:
aws s3 ls s3://DESTINATION_BUCKET_NAME --recursive
You can also compare the total object count and size between the two buckets.

8. Important Considerations for Real-World Environments

Before using this approach in production, keep these points in mind:
1. AWS KMS Encryption
If the source objects use SSE-KMS encryption, the copying identity needs permission to decrypt using the source KMS key. The source KMS key policy must also allow the required cross-account access.
If the destination bucket uses SSE-KMS, the identity needs permission to encrypt using the destination KMS key, and its key policy must allow the operation.
2. S3 Object Ownership
When the destination bucket uses the recommended  Bucket owner enforced  Object Ownership setting, ACLs are disabled and the destination bucket owner owns the copied objects.
You generally do not need  s3:PutObjectAcl  for this configuration.
3. IAM Permissions
Cross-account access requires permissions on both sides:
  • Source bucket policy must allow the destination account.
  • Destination IAM identity policy must grant the required S3 actions.
4. Versioning
The  aws s3 sync  command copies current object versions; it does not replicate the complete version history of the source bucket.
5. Large Data Transfers
For very large datasets, consider AWS S3 Batch Operations, AWS DataSync, or S3 Replication, depending on your migration and ongoing synchronization requirements.

Conclusion

Copying S3 objects between AWS accounts is a common cloud engineering task. With the right combination of IAM policies, S3 bucket policies, and AWS CLI commands, you can securely transfer data without making your buckets publicly accessible.
Understanding cross-account permissions is especially valuable when working with multi-account AWS environments, infrastructure automation, and production data migrations.
Let's discuss!
Have you ever migrated S3 data between two AWS accounts?
Would you prefer AWS CLI, S3 Replication, or AWS DataSync for a large-scale data migration?
Share your experience and suggestions in the comments.
#AWS #AmazonS3 #AWSIAM #CloudComputing #DevOps #AWSCLI #CloudEngineering #DataMigration
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article