Introduction
Mounting an Amazon S3 bucket on an EC2 instance is a powerful way to access and interact with your object storage as if it were a local filesystem. This can be particularly useful for DevOps engineers, cloud administrators, and developers who want seamless integration between compute and storage on AWS.
In this tutorial, you will learn how to mount an S3 bucket on an Ubuntu EC2 instance using s3fs, a FUSE-based filesystem backed by Amazon S3.
Prerequisites
Before we begin, ensure you have the following:
- An active AWS account.
- An EC2 instance running Ubuntu 20.04 or later.
- An IAM user or role with access to S3.
- An S3 bucket created.
- SSH access to your EC2 instance.
Step 1: Update Your Ubuntu Instance
First, update and upgrade your Ubuntu packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install Required Dependencies
To install s3fs, you’ll need to install some dependencies first:
sudo apt install -y automake autotools-dev fuse g++ git libcurl4-openssl-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
Step 3: Install s3fs
Clone the s3fs GitHub repository and build it:
git clone https://github.com/s3fs-fuse/s3fs-fuse.git
cd s3fs-fuse
./autogen.sh
./configure
make
sudo make install
Check if the installation was successful:
s3fs --version
Step 4: Set Up IAM Access
You need to provide credentials for S3 access:
Option 1: IAM Role (Recommended for EC2)
Attach an IAM role with S3 access to your EC2 instance.
Option 2: AWS Credentials File
If you’re not using IAM roles, create a file with your AWS credentials:
echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > ~/.passwd-s3fs
chmod 600 ~/.passwd-s3fs
Step 5: Create a Mount Directory
Create a directory where your S3 bucket will be mounted:
sudo mkdir /mnt/my-s3-bucket
Replace my-s3-bucket
with your actual bucket name.
Step 6: Mount the S3 Bucket
Mount your S3 bucket using s3fs:
s3fs my-s3-bucket /mnt/my-s3-bucket -o allow_other -o use_path_request_style -o url=https://s3.amazonaws.com
If you used IAM roles, add -o iam_role=auto
.
Step 7: Make Mount Persistent (Optional)
To make the S3 mount persist after a reboot, add the following line to your /etc/fstab
file:
s3fs#my-s3-bucket /mnt/my-s3-bucket fuse _netdev,allow_other,use_path_request_style,url=https://s3.amazonaws.com 0 0
Step 8: Test the Mount
Verify that the mount is working:
ls /mnt/my-s3-bucket
Try uploading a file:
echo "Hello from EC2" > /mnt/my-s3-bucket/test.txt
Check the file in the AWS S3 Console.
Step 9: Troubleshooting
Issue 1: Permission Denied
Make sure you used chmod 600 ~/.passwd-s3fs
and your IAM permissions are correct.
Issue 2: Mount Fails on Reboot
Ensure network-dependent filesystems like S3 are using _netdev
in /etc/fstab
.
Issue 3: Mountpoint Not Empty
Run sudo umount /mnt/my-s3-bucket
before remounting.
Best Practices
- Use IAM roles instead of hardcoded credentials for better security.
- Monitor performance; s3fs is not a substitute for block storage.
- Consider tools like goofys or rclone for different use cases.
- Avoid mounting S3 in high IOPS scenarios.
Use Cases
- Centralized logging storage.
- Static file hosting.
- Backup solutions.
- Configuration and asset sharing across instances.
Unmounting the S3 Bucket
To unmount:
sudo umount /mnt/my-s3-bucket
Security Tips
- Never expose your access keys in scripts.
- Restrict bucket access using S3 bucket policies.
- Enable encryption (SSE) on your S3 bucket.
Final Thoughts
Mounting an S3 bucket on an EC2 Ubuntu instance provides flexible access to cloud storage. With s3fs
, the setup is straightforward, secure, and highly usable for a range of DevOps and cloud-native scenarios.
Keep in mind that S3 is object storage—not a replacement for local or block storage—so use it accordingly.
Follow me on Linkedin!