We can use s3 bucket as file system on ec2 instance also know as S3fs file system.
it is a FUSE filesystem application backed by amazon web services, that allows you to mount an Amazon S3 bucket as a local file-system.
In this tutorial, I would make it cover for both Centos and Ubuntu.
Step : 1
Update the System
yum update all
Step : 3
Clone s3fs source code from git
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
Step : 2
Install the dependencies
For CentOS or Red Hat
sudo yum install automake fuse fuse-devel gcc-c++ git libcurl-devel libxml2-devel make openssl-devel
Step : 4
Compile and install s3fs
cd s3fs-fuse
./autogen.sh
./configure --prefix=/usr --with-openssl
make
sudo make install
For Ubuntu or Debian
sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
Step : 5
Getting key and secret for s3 bucket
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 \u00e2\u20ac\u0153S3 Full-Access\u00e2\u20ac\u009d policy or create a new policy with sufficient permissions.
Step : 6
Please skip "step : 6", if your key and secret has the "S3 Full-Access"
you can create policy with the following code in ur IAM policy.
{
"Version": "2012–10–17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:ListAllMyBuckets"
],
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": [
"s3:ListBucket"
],
"Resource": [
"arn:aws:s3:::your-s3-bucket"
]
},
{
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": [
"arn:aws:s3:::your-s3-bucket/*"
]
}
]
}
Step : 7
Create a file to store s3 key and secret
For this tutorial. I created "/etc/s3key"
save your key and secret as the following.
Your_accesskey:Your_secretkey
Step : 8
Set the permission into /etc/s3key file
sudo chmod 640 /etc/s3key
Step : 9
It is time to mount. we have finished collecting necessary things.
For this tutorial we will mount our s3 backet to /var/www/media directory.
Firstly, we have to create directory.
mkdir /var/www/media
After we have created the direction, we have to mount with the following command.
s3fs your_bucket -o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /var/www/media
Step : 10
Auto Remount after reboot the system
echo "$(which s3fs) your_bucket-o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /var/www/media">/etc/rc.local
or
~ which s3fs
-> /usr/local/bin/s3fs
~ vi /etc/rc.local
-> /usr/local/bin/s3fs your_bucket-o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /var/www/media
Congrats!!
You have successfully mounted your S3 bucket to your EC2 instance.
Thank you for taking the time to read my article. I hope you find it helpful. Please feel free to reach out if you have any questions or would like to discuss further.