How to move files from amazon ec2 to s3 bucket using command line
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Moving files from an EC2 instance to S3 from the command line is usually just an AWS CLI operation, but the setup matters: the instance needs permissions, the bucket path must be correct, and the command should match whether you are moving one file, many files, or an entire directory. In practice, the safest pattern is to copy to S3 first and then remove local files only after you verify the upload succeeded.
Make Sure the EC2 Instance Can Access S3
Before running any transfer command, confirm that the EC2 instance has AWS credentials through an IAM role or configured CLI profile. The preferred production setup is an IAM role attached to the instance.
A minimal test is:
If this fails, S3 commands will fail too. Also verify that the IAM permissions allow writes to the target bucket or prefix.
Copy a Single File to S3
The simplest command uses aws s3 cp.
This uploads one file from the EC2 instance to the specified S3 key. The S3 path must include the bucket name and, if you want one, the key prefix.
Copy a Directory Recursively
If you need to upload a directory tree, use the recursive option.
This is useful for batch uploads, exports, and backup-style operations. You can also combine it with include and exclude filters.
That uploads only CSV files from the directory tree.
Use mv Only When You Really Mean Move
The AWS CLI also supports aws s3 mv, which copies the object to S3 and removes the source file afterward.
This is convenient, but it is safer to think of it as a copy-plus-delete operation. If the file is important, many teams prefer:
cpto S3- verify upload success
- delete local file explicitly
That gives you a clearer recovery path if something unexpected happens during the transfer process.
Verify the Upload
After the transfer, confirm that the object exists where you expect it.
For automation, it is also common to check the command exit status and stop the script if the upload fails.
That pattern prevents accidental local deletion after a failed upload.
Consider Encryption and Region Details
If your bucket policy requires server-side encryption, include it in the command.
You should also make sure the CLI is targeting the right AWS region, especially when working across multiple environments.
Use sync for Repeated Directory Mirroring
If the real goal is keeping a directory mirrored in S3, aws s3 sync is often better than repeated cp commands.
sync is useful when the directory changes incrementally and you want a repeated command that uploads only differences.
Common Pitfalls
- Running S3 commands without confirming the EC2 instance has the right IAM permissions.
- Using
mvbefore establishing a reliable verification or recovery process. - Uploading to the wrong bucket or key prefix because the S3 path was not checked carefully.
- Forgetting encryption flags when the bucket policy requires them.
- Using repeated
cpcommands wheresyncwould better match the actual goal.
Summary
- Use
aws s3 cpfor straightforward uploads from EC2 to S3. - Use
--recursivefor directory uploads and filters for selective copies. - Prefer copy-then-delete over
mvwhen the data is important and you want safer failure handling. - Verify uploads and check exit status before removing local files.
- Use
syncwhen the real requirement is repeated directory mirroring.

