DEV Community

Cover image for How to Mount S3 Bucket on EC2 Linux Using IAM Role
Amit Gupta
Amit Gupta

Posted on

How to Mount S3 Bucket on EC2 Linux Using IAM Role

Hello readers,
In this blog we will be learning how to mount a S3 bucket as a drive on an EC2 Linux server.

Before that we must know what exactly a S3 bucket is?

  • Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere.

Now let's understand how to mount a S3 bucket to an EC2 instance:

- Create an EC2 instance and attach an iam role of S3fullaccess to that server

Image description

Image description

- Create S3 Bucket

Here I have created a s3 bucket for this blog purpose and I have uploaded a file named as "Assignment.docx" in my S3 bucket as shown below.

Image description

Image description

We are assuming that you have a running Linux EC2(Red Hat/Amazon Linux AMI) instance on AWS.

- Connect to the instance using ssh client

- Goto root directory using below command

# sudo su
# cd
Enter fullscreen mode Exit fullscreen mode

Step-1: Update the system with the below command.

#sudo yum update
Enter fullscreen mode Exit fullscreen mode

Image description

Step-2: Install Required Packages.

# sudo yum install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel

Enter fullscreen mode Exit fullscreen mode

Image description

Step-3: Download s3fs source code from git.

# git clone https://github.com/s3fs-fuse/s3fs-fuse.git
Enter fullscreen mode Exit fullscreen mode

Image description

Step-4: Now Compile and install the code.

Following the set of command will compile fuse and add fuse module in the kernel.

# cd  s3fs-fuse
# ./autogen.sh 
# ./configure --prefix=/usr --with-openssl
# make 
# sudo make install
Enter fullscreen mode Exit fullscreen mode

Image description

Step-5: Use below command to check where s3fs command is placed in os.

# which s3fs
Enter fullscreen mode Exit fullscreen mode

Image description

Step-6: Getting the access key and secret key.

You will need an AWS Access key and Secret key with appropriate permissions to access your s3 bucket from your EC2 instance. You can easily manage your user permissions from IAM (Identity and Access Management) Service provided by AWS. Create an IAM user with S3 full access(or with a role with sufficient permissions) or use your Account’s root credentials. Here we will use the root credentials for simplicity.

Go to AWS Menu -> Your AWS Account Name -> My Security Credentials. Here your IAM console will appear. You have to go to Users > Your Account name and under permissions Tab, check whether you have sufficient access on S3 bucket. If not, you can manually assign an existing “S3 Full-Access” policy or create a new policy with sufficient permissions.

Now go to Security Credentials Tab and Create Access Key. A new Access Key and Secret Key pair will be generated. Here you can see access key and secret key (secret key is visible when you click on show tab) which you can also download. Copy these both keys separately.

Note that you can always use an existing access and secret key pair. Alternatively, you can also create a new IAM user and assign it sufficient permissions to generate the access and secret key.

Step-7: Create a new file in /etc with the name passwd-s3fs and Paste the access key and secret key in the below format.

# touch /etc/passwd-s3fs
Enter fullscreen mode Exit fullscreen mode
# vim /etc/passwd-s3fs
Enter fullscreen mode Exit fullscreen mode

Provide your Access Key and Secret Key in the below format.

# Your_accesskey:Your_secretkey
Enter fullscreen mode Exit fullscreen mode

Step-8: Change the permission of file.

# chmod 640 /etc/passwd-s3fs
Enter fullscreen mode Exit fullscreen mode

Image description

Step-9: Now create a directory or provide the path of an existing directory and mount S3bucket in it.

# mkdir /mys3bucket
# s3fs your_bucket-name /mys3bucket/ -o passwd_file=/etc/passwd-s3fs
Enter fullscreen mode Exit fullscreen mode

Step-10: Check mounted s3 bucket.

# df -h
Enter fullscreen mode Exit fullscreen mode

Image description

Step-11: Change directory to mys3bucket.

# cd /mys3bucket
# ls
Enter fullscreen mode Exit fullscreen mode

If it shows the mounted file system, you have successfully mounted the S3 bucket on your EC2 Instance. You can also test it further by creating a test file.
Here we can see our uploaded file (Assignment,docx) in our S3 bucket.

Image description

Congrats!! You have successfully mounted your S3 bucket to your EC2 instance. Any files written to /mys3bucket will be replicated to your Amazon S3 bucket.😴

Top comments (0)