Amazon S3 is an object storage solution with high scalability. AWS EC2, on the other hand, offers resizable computing capacity in the cloud. Combining these two allows you to treat your S3 bucket as a local file system, simplifying data transfer, storage, and retrieval.
You can mount an S3 bucket on your EC2 Linux instance with S3fs, a FUSE (Filesystem in Userspace) file system behaving like a virtual drive. The process is pretty straightforward but has some prerequisites, which we shall discuss in this article. So, why waste time? Let’s dive in.
How Do I Mount S3 Bucket on an EC2 Linux Instance
You have to make sure to meet the following prerequisites.
- A functioning Linux EC2 instance on AWS with root access.
- An S3 bucket already created that you want to mount on your Linux instance.
- Access and Secret key pair with the necessary S3 permissions.
Now follow the steps shown here, and you should have your S3 bucket mounted on EC2 in no time.
1. Use the command prompt below to update the system on your Ubuntu instance
2. Now, you need to install the necessary dependencies. For that, run the following command.
sudo apt-get install automake autotools-dev fuse g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
3. After installing the dependencies, it is time to clone the S3fs source code. Here’s the command you’ll need.
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
4. The next step is the compilation and installation of the source code. But first, you have to change the directory.
./configure –prefix=/usr –with-openssl
sudo make install
You can use the ‘which s3fs’ command to check if the installation went right.
5. Open your IAM console and go to Users. There, you can check whether you have sufficient permissions for your EC2 instance to access the S3 bucket. Unless you have the required permissions, assign an S3 Full-Access policy to it.
6. Next, generate a new Access Key from the Security Credentials tab and copy both the access and security key to your clipboard.
7. Run the following command to make a new file and assign the name ‘passwd-s3fs’ to it.
Then, paste the keys you copied like:
8. Alter the permissions associated with the file using:
sudo chmod 640 /etc/passwd-s3fs
9. The following commands will let you mount your S3 bucket to your desired directory.
s3fs your_bucketname -o use_cache=/tmp -o allow_other -o uid=1001 -o mp_umask=002 -o multireq_max=5 /mys3bucket
Replace your_bucketname with the actual name of your S3 bucket and set the uid value as the ID of the mount point owner.
10. Here’s what to expect as the output when you test the S3 bucket mount.
df -Th /mys3bucket
Filesystem Type Size Used Avail Use% Mounted on s3fs fuse.s3fs 256T 0 256T 0% /mys3bucket
If you see the mounted file system in the output, it means the mounting process is successful.
Is data transfer from S3 to EC2 free?
The data transfer cost from an Amazon S3 bucket to an AWS EC2 instance is $0.09 per GB if both are in the same AWS region.
How do I copy multiple files from S3 to EC2?
Create an IAM role with S3 write access and map it to the EC2 instance. Next, use the AWS s3 cp command to copy the files.
Can I mount multiple S3 buckets on the same EC2 instance?
Yes, you can mount multiple S3 buckets on the same EC2 instance. It is possible to mount each bucket to a different directory on the same instance.
That’s it! You’ve successfully mounted an S3 bucket on your EC2 instance running Linux using ‘s3fs-fuse.’ If you have any further queries, feel free to ask them in the comment section below. Help should be on your way. Thanks for reading!