Skip to main content

How to mount Amazon S3 Bucket into AWS EC2 instance





Step 1
Update all the packages if required
(RHEL/CentOS)
yum update

(Debian/Ubuntu)
apt-get update

Step 2
Install all the dependencies require to configure
(RHEL/CentOS)
sudo yum install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel

(Debian/Ubuntu)
sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config

Step 3
Clone the s3fs repository from the GitHub
(RHEL Family and Debian Family)
git clone https://github.com/s3fs-fuse/s3fs-fuse.git

Step 4
Now change directory to just clone from the GitHub and compile and setup the s3fs-fuse
cd s3fs-fuse

./autogen.sh

./configure --prefix=/usr --with-openssl

make

sudo make install


Step 5
Run the following command to check if every set up correctly
which s3fs
Output
/usr/bin/s3fs

Step 6
Now, Go to IAM and create a user name "s3user" and give him Programmatic Access and attached the Policy "AmazonS3FullAccess".

While creating a user, it will provide you "Access key ID" and "Secret access key". Note it down into a notepad or download the CSV file. We will need this for configuring the configuration file for s3fs.

Step 7
Now create a configuration file name "passwd-s3fs" under etc folder
vi /etc/passwd-s3fs

and enter the following into the file and save and exit
Access-key-ID:Secret-access-key
eg. AKIATJAADJMXQVLQIOSR:gUSMAUjXPJy4sAPa00+nntGpIjR0eWnL96M9AHOx

Step 8
Change the permission of the file
chmod 640 /etc/passwd-s3fs
Step 9
Now, create a directory into your ec2 instance where you want to mount the S3 Bucket
mkdir /gautam-bucket

Step 10
Now mount the S3 Bucket into created directory name "gautam-bucket"
sudo s3fs gautam-bucket /gautam-bucket -o passwd_file=/etc/passwd-s3fs

We have mounted the gautam-bucket into /gautam-bucket folder successfully. But But But this is not the secure way to access the bucket into ec2 instance because you have paste the Access key ID and Secret access key into the instance.

In-case your system compromise, hacker can easily steal your data. To overcome this situation we use roles.

Now we will umount the S3 Bucket and remove the passwd-s3fs file. We will now use the Roles to achieve this.

Step 11
Now we will create a role for ec2 to give permission "AmazonS3FullAccess"

Step 12
Go to ec2 instance dashboard select the instance, go to instance setting and attached the s3role with the instance.

Step 13
Now, mount the S3 Bucket again with IAM Role
s3fs -o iam_role="s3role" gautam-bucket /gautam-bucket
Congratulations. You have setup the S3 Bucket privately from your EC2 instance. 

Popular posts from this blog

WordPress Site is not loading properly behind Google Cloud/AWS Load Balancer

Hello Guys, Today we are going to understand how can we fix a WordPress loading issue (CSS and JS loading issue) behind the Google Cloud Load Balancer or AWS Load Balancer. Generally, When we host a WordPress site directly with Google Cloud Compute Engine VM instance or AWS EC2 instance. It's loading perfectly fine. But once we added this WordPress server behind any Load Balancer either from GCP Load Balancer or AWS Load Balancer, you site will completely broken, means the CSS and JS of your site not loaded properly. The reason for this is - When you put a load balancer in front of WordPress, you need to modify wp-config.php to process the HTTP header HTTP_X_FORWARDED_PROTO to detect the protocol that the user is using and not the protocol the load balancer is using to connect to your backend. To fix this issue, we have to make following changes in the  wp-config.php  file and add the below code snippet on the top of  wp-config.php file - Google Cloud Platform Load Bal...

Git and GitHub Commands Mastery

1. Setup Git and GitHub Global Configuration  git config --global user.email "gautamthakur1983@gmail.com" git config --global user.name "Gautam Thakur" git config --global list git config --list 2. Git Lifecycle | Initilize, Status, Add, Commit git status git init git add git commit -m "Commit Message" git log git log --oneline 3. Git Difference between last commit changes and current version changes git diff 4. Git Compare between 2 different Git Commits git diff eac4c5b 82485b1 5. Git Statsh - To save some changes for temporary purpose ## Pop take out stash and clear but apply take out stash but not clear git stash (To save current changes for temporary) git stash pop (To take out all stash contents) git stash list git stash clear (To clear all the stash changes) git stash save "NAME"  git stash save "NAME1" (Working with multiple stash) git stash sapply 0 or 1 (0 for name, 1 for about) after that run git stash clear git stash clear 6...

How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS

  Hello Friends, In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL). Requirements: Master Node: No. of VMs 1 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access  Worker Node: No. of VMs 2 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet),  sudo access with admin access  Disable Swap and comment fstab entry: First, Disable Swap and remove or comment the Swap entries from fstab file : sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab Next...