Introduction

Amazon EC2 allows you to run virtual machines in the cloud, while Amazon S3 provides a secure and scalable storage solution. Manually uploading files from EC2 to S3 can be time-consuming, especially when dealing with frequent backups or log files.

Automating this process using cron jobs ensures that files are uploaded on schedule without manual intervention. This is useful for,

  • Backups: Automatically store critical files in S3 for safekeeping.
  • Log Management: Move logs from EC2 to S3 to free up disk space.
  • Data Processing: Upload files to S3 for further processing by AWS services.

In this article, we will walk you through setting up an EC2 instance, installing AWS CLI, creating a shell script, and using cron jobs to schedule automatic uploads to S3.

Step 1. Launch an EC2 Instance.

  • Log in to your AWS Management Console.
  • Navigate to the EC2 service and click Launch Instance.
  • Choose Ubuntu or Amazon Linux as the operating system.
  • Select an instance type (Free Tier: t2.micro).
  • Configure security groups and allow SSH access (port 22).
  • Launch the instance and connect via SSH.

Connect to the EC2 Instance

Bash
ssh -i your-key.pem ubuntu@your-ec2-ip

Step 2. Install AWS CLI on EC2.

Install AWS CLI using the following commands.

Bash
sudo apt update
Bash
sudo apt install awscli -y

If sudo apt install awscli -y does not work, try this instead.

Bash
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Bash
sudo apt install unzip -y
Bash
unzip awscliv2.zip
Bash
sudo ./aws/install

Verify installation

Bash
aws --version

Aws Version

Step 3. Verify AWS CLI Configuration.

Verify access to S3.

Bash
aws s3 ls s3://your-bucket-name/

Confirm Bucket

Step 4. Create a Sample File to Upload.

C#
echo "This is a test file" > testfile.txt

Step 5. Create a Shell Script for Uploading Files.

Create a new script file.

Bash
nano /home/ubuntu/upload_to_s3.sh

Add the following script inside the file.

Bash
#!/bin/bash
/usr/local/bin/aws s3 cp /home/ubuntu/testfile.txt s3://your-bucket-name/

GNU

Save and exit (CTRL + X, then Y, then Enter).

Make the script executable.

Bash
chmod +x /home/ubuntu/upload_to_s3.sh

Step 6. Schedule the Script with Cron Job.

Edit the cron jobs

Bash
crontab -e

Add this line at the bottom to run the script every 3 minutes.

Bash
*/3 * * * * /home/ubuntu/upload_to_s3.sh >> /home/ubuntu/cron.log 2>&1

Save and exit. Restart the cron service.

Bash
sudo systemctl restart cron

Verify active cron jobs.

Bash
crontab -l

Cron Entry

Step 7. Verify File Uploads to S3.

Check if the file has been uploaded to your S3 bucket.

Bash
aws s3 ls s3://your-bucket-name/

Check cron logs.

Bash
cat /home/ubuntu/cron.log

Jobs

Step 8. Stop or Remove the Cron Job.

To stop the cron job, you can,

  • Edit the cron job (crontab -e) and remove the line.
  • Or remove all cron jobs.
Bash
crontab -r

Delete Cron

Conclusion

In this article, we learned how to automate file uploads from an EC2 instance to an S3 bucket using cron jobs. We set up an EC2 instance, installed AWS CLI, created a shell script, and scheduled it with cron to run every 3 minutes.