How to transfer files between AWS s3 and AWS ec2
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The standard way to move files between Amazon S3 and an EC2 instance is to use the AWS CLI on the instance with an IAM role that grants S3 access. That avoids hardcoding long-lived credentials and works for both one-off copies and repeatable synchronization jobs. The actual transfer commands are simple; the permissions and path choices are where most mistakes happen.
Prefer an IAM Role Over Stored Keys
Before copying anything, make sure the EC2 instance can authenticate to AWS. The safest pattern is an instance profile role attached to the EC2 instance with only the S3 permissions it needs.
Typical permissions might allow:
- '
s3:GetObjectto download files' - '
s3:PutObjectto upload files' - '
s3:ListBucketto enumerate bucket contents'
Once the role is attached, the AWS CLI can use temporary credentials automatically. Verify access on the instance:
If these commands fail, fix IAM first. File transfer commands will not work until credentials and bucket permissions are correct.
Download From S3 to EC2
To copy a single file from S3 to the local EC2 filesystem:
To copy an entire prefix recursively:
This is the simplest way to stage deployment artifacts, configuration bundles, or datasets onto an instance.
Upload From EC2 to S3
To send a file from the instance to S3:
To upload a directory recursively:
S3 paths are object keys, not local folders, so the trailing slash controls the prefix you are targeting rather than a real directory object.
Use sync for Repeated Transfers
For recurring jobs, aws s3 sync is often better than repeated cp commands because it compares source and destination and transfers only the differences.
Download a bucket prefix to the instance:
Upload local files back to S3:
You can also exclude or include files:
That makes sync a good fit for deployment bundles, backups, and generated reports.
Typical EC2 Workflow
A common operational pattern looks like this:
- attach an S3-capable IAM role to the instance
- install the AWS CLI if it is not already present
- test access with
aws sts get-caller-identity - run
aws s3 cporaws s3 sync - automate the transfer with cron, systemd, or a deployment step if needed
For example, a nightly backup upload might be:
The CLI is usually enough. You do not need scp because S3 is not another SSH-accessible machine.
Cost and Network Considerations
Transfers between EC2 and S3 in the same region are common, but they still consume time and may affect throughput. Large syncs can also generate significant API traffic. For bulk jobs, use the correct region and avoid re-uploading unchanged files.
Compression before upload is often worthwhile for logs, text artifacts, and backups. For already compressed files such as videos or zip archives, the benefit is usually limited.
Common Pitfalls
The most common mistake is storing AWS access keys directly on the EC2 instance instead of using an IAM role. That creates unnecessary security risk.
Another mistake is forgetting the --recursive flag when copying directories. Without it, only a single object path is processed.
Developers also sometimes confuse S3 paths with local filesystem directories. S3 uses bucket names and object keys, so a trailing slash affects the key prefix, not a true folder.
Finally, transfer failures are often permission problems, not network problems. If AccessDenied appears, review the IAM role and the bucket policy before changing commands.
Summary
- Use the AWS CLI on the EC2 instance to transfer files to and from S3.
- Prefer an instance IAM role over hardcoded AWS credentials.
- Use
aws s3 cpfor straightforward copies andaws s3 syncfor repeated directory syncs. - Remember that S3 uses object keys and prefixes, not real folders.
- Check IAM and bucket permissions first when transfers fail.
Related reading
- How to trigger azure function when there is a message in Kafka topic
- How to turn off the pager for AWS CLI return value?
- How to uninstall aws-cli
- How to unload a table on RedShift to a single CSV file?
- How to update a Map or a List on AWS DynamoDB document API?
- How to update an item in Dynamodb of type String Set SS?
- How to update an item in Dynamodb of type String Set SS?
- How to update Kubernetes Dashboard in hosted Kubernetes on GKE?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.