Share Files Between Amazon S3 and Amazon EC2

In this article, you will learn how to share files from Amazon S3 Bucket to Amazon EC2 Instance in detailed.

Login to the AWS Management Console and navigate to Amazon S3. Check the files that need to be transferred.

S3 Bucket
S3 Bucket

Creating IAM Role

Navigate to IAM and click on Create role.

IAM Dashboard
IAM Dashboard

Select the type and choose the use case as EC2 as the files need to be transferred from S3 to EC2.

Create Role
Create Role

Attach the AmazonS3FullAccess policy to the role and click on next.

Add Policy
Add Policy

Give the role a name and review it. Finally, click on Create.

Review the Role
Review the Role

The role got created successfully.

IAM Role
IAM Role

Creating Key Pair and Security Group

Navigate to the EC2 dashboard and click on Create key pair.

Create Key Pair
Create Key Pair

Give a name for the key pair, select the file format and click on create.

Create Key Pair
Create Key Pair

The key pair gets created and will be downloaded in the system.

Key Pair
Key Pair

Now, go to Security groups and click on Create security group.

Create Security Group
Create Security Group

Specify the essential details and the Inbound rules for the security group. Click on Create.

Specify Details
Specify Details
Specify Inbound rules
Specify Inbound rules

The security group gets created.

Security Group
Security Group

Creating EC2 Instance

Go to Instances and click on Launch Instance.

Choose an AMI; I would recommend Amazon Linux 2 AMI as it is a free tier.

Choose AMI
Choose AMI

Choose the Instance type; I would recommend t2.micro as it is a free tier.

Choose Instance Type
Choose Instance Type

Configure the Instance details. Select the IAM role created before and click on Next.

Configure Instance Details
Configure Instance Details

Select the Security group created before and click on Review and Launch.

Configure Security Group
Configure Security Group

Review the Instance and click on Launch.

Review Instance
Review Instance

Select the key pair which was created before and click on Launch.

Select Key Pair
Select Key Pair

The EC2 Instance gets launched successfully. Select the Instance and click on Connect.

EC2 Instance
EC2 Instance

Connecting EC2 Instance

Connect the EC2 Instance using an SSH client.

Connect the EC2 Instance
Connect the EC2 Instance

Open the terminal and enter the following commands:

To make the Key Pair private

Command: “chmod 400 keypair.pem”

To connect the EC2 Instance

Command: ssh -i “kp.pem” ec2-user@ec2-13-213-69-221.ap-southeast-1.compute.amazonaws.com

Finally, the EC2 Instance gets connected through SSH.

Conected EC2 Instance

Enter as a root user by using the following command,

Command: sudo su

Update the system using,

Command: yum update -y

Update the System

Enter the following command to transfer files from S3 to EC2 Instance.

Command: aws s3 cp s3://bucketname/foldername/filename copied-filename

Now, click ls, and the copied file name gets displayed.

Transfering files from S3 to EC2

Tip: You can also use wget command of Linux to directly download the required file from your S3 bucket into your EC2 Instance.

Command: sudo wget file-link-in-s3-bucket

I also run a small youtube channel on some exciting topics, do look into it. 🙂

https://www.youtube.com/channel/UCBO2LHhj4-SYejcaE3YfuNg

Leave a Reply

%d bloggers like this: