How to Mount S3 Bucket on EC2 Linux Using IAM Role
Published: 2022-09-01 10:16:48

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kosbih6sd3ita8xc86c2.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kex4z6onalwyp63b7s06.png)

**- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqz5ybz7q4zyfydkyb5c.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c61gdo03v9ru7ciwfobn.png)

**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
```
**Step-1:** Update the system with the below command.

```
#sudo yum update
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7zn3joe3eyqdbm6f3f8.png)

**Step-2:** Install Required Packages.

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

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ock0s6x9ldhkoiwym32.png)

**Step-3:** Download s3fs source code from git.

```
# git clone https://github.com/s3fs-fuse/s3fs-fuse.git
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0c9pzuea8va8keq98l8.png)

**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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dzyw7c9f54dzgv2rucvc.png)

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

```
# which s3fs
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhefgg1on5dguvlq30kn.png)

**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
```
```
# vim /etc/passwd-s3fs
```
Provide your Access Key and Secret Key in the below format.
```
# Your_accesskey:Your_secretkey
```
**Step-8:** Change the permission of file.

```
# chmod 640 /etc/passwd-s3fs
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ta1z5e5n2l3i7weh0o2v.png)

**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
```
**Step-10:** Check mounted s3 bucket.

```
# df -h
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0wsp1w42bsqkjegkxbi.png)

**Step-11:** Change directory to mys3bucket.

```
# cd /mys3bucket
# ls
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4xyrnzwuc6zuyyxpkl4.png)

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.😴