A thumbnail Illustrating how to copy files from AWS S3 to AWS EC2

In this tutorial, Iโ€™ll walk you through how to copy files from and to an Amazon S3 bucket using an EC2 instance. Weโ€™ll create the EC2 instance via the AWS Console, and use the AWS CLI to perform S3 copy operations.


Prerequisites

  • AWS account
  • AWS CLI installed and configured

Step 1: Create an EC2 Instance Using the Console

  1. Go to the EC2 Dashboard in the AWS Console
  2. Click Launch Instance
  3. Fill in the details:
    • Name: S3-Copy-Instance
    • AMI: Ubuntu
    • Instance Type: t2.micro (free tier)
    • Key Pair: Select or create a key pair (download the .pem file)
    • Network Settings:
      • Use default security group
      • Make sure SSH (port 22) is open
  4. Click Launch Instance

Once it launches, copy the Public IPv4 address of the instance.


Step 2: SSH Into the EC2 Instance

Open your terminal:

ssh -i my-key-pair.pem ec2-user@<EC2_PUBLIC_IP>

โš ๏ธ Replace my-key-pair.pem with your actual PEM file name, and <EC2_PUBLIC_IP> with the instanceโ€™s public IP address.


Step 3: Create a Dummy Log File on the Instance

echo "This is a test log from EC2" > test.log

Step 4: Create an S3 Bucket Using the Console

  1. Go to the S3 Dashboard
  2. Click Create Bucket
  3. Name it something like: my-s3-bucket-logs-unique
  4. Region: us-west-2
  5. Leave the rest as default (Block public access enabled)
  6. Click Create Bucket

Step 5: Try to Copy the File to S3 (This Will Fail)

Back in your EC2 terminal:

aws s3 cp test.log s3://my-s3-bucket-logs-unique/

Youโ€™ll likely see a permission denied or Access Denied (403) error โ€” this is expected because the instance doesnโ€™t have S3 permissions yet.


Step 6: Create an IAM Role and Attach It to the EC2 Instance

  1. Go to the IAM Dashboard โ†’ Roles
  2. Click Create role
  3. Trusted entity type: AWS service
  4. Use case: EC2
  5. Attach policy: AmazonS3FullAccess
  6. Name the role: EC2-S3Access-Role
  7. Click Create Role

Now, go back to the EC2 Dashboard:

  1. Select your instance
  2. Choose Actions โ†’ Security โ†’ Modify IAM Role
  3. Attach the role: EC2-S3Access-Role
  4. Click Update IAM Role

Step 7: Retry the Copy Operation (Now It Works)

Back in your EC2 shell:

aws s3 cp test.log s3://my-s3-bucket-logs-john/

โœ… You should see output confirming the upload.


Step 8: Download from S3 Back to EC2 (Optional)

aws s3 cp s3://my-s3-bucket-logs-john/test.log downloaded.log
cat downloaded.log

๐Ÿง  Recap

  • โœ… Created an EC2 instance using AWS Console
  • โœ… SSHed into the instance and created a dummy log file
  • โœ… Created an S3 bucket
  • โŒ Tried copying without permissions (expected failure)
  • โœ… Created IAM role and attached it to EC2
  • โœ… Successfully copied files to and from S3

๐ŸŽฅ Watch the Full Demo

This post is the perfect companion to my YouTube walkthrough โ€” subscribe and follow along visually!

Leave a Reply

Your email address will not be published. Required fields are marked *