This guide walks you through the process of mounting an AWS S3 bucket on an EC2 instance using S3FS. This setup allows you to interact with your S3 bucket as if it were a local filesystem.
Prerequisites
AWS Account with appropriate permissions
EC2 instance running Ubuntu
AWS CLI installed and configured on your local machine
Step 1: Launch EC2 Instance and Configure IAM Role
Your EC2 instance should be running and have the appropriate IAM role attached. In this case, we're using an Ubuntu 24.04 instance with an IAM role named "EC2-S3-Access-Role" that has S3 access permissions.
Key details to verify:
Instance State: Running
IAM Role: EC2-S3-Access-Role
Platform: Ubuntu 24.04
Step 2: Create S3 Bucket and Configure Permissions
Create an S3 bucket and configure the necessary policies:
Create bucket with these settings:
Bucket type: General purpose
Block all public access: Enabled (recommended for security)
Bucket Versioning: Optional
Default encryption: Enabled (recommended)
Add the following IAM policy to your role:
jsonCopy{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:GetBucketLocation",
"s3:ListBucketMultipartUploads",
"s3:ListBucketVersions"
],
"Resource": "arn:aws:s3:::your-bucket-name"
},
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListMultipartUploadParts",
"s3:AbortMultipartUpload"
],
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
Important configuration points:
Choose "General purpose" bucket
Block public access
Enable bucket versioning for data protection
Configure appropriate encryption settings
Step 3: Install S3FS on EC2
Connect to your EC2 instance and install S3FS:
sudo apt update
sudo apt install s3fs -y
Verify the installation:
s3fs --version
Step 4: Mount S3 Bucket
Create the mount point and mount the bucket:
# Create mount point
sudo mkdir -p /mnt/s3-bucket
# Mount the bucket
sudo s3fs project10bucket /mnt/s3-bucket -o iam_role=EC2-S3-Access-Role -o allow_other
# Verify mount
df -h | grep s3fs
Step 5: Test the Mount
Create a test file and verify:
# Create test file
echo "test file" > /mnt/s3-bucket/test.txt
# List contents
ls -la /mnt/s3-bucket
Step 6: Verify Using AWS CLI
Verify the file appears in your S3 bucket using AWS CLI:
aws s3 ls s3://project10bucket
You can also verify through the AWS Console that your test file has been successfully created in the S3 bucket.
Troubleshooting Tips
If mount fails, check:
IAM role permissions
S3 bucket name
Network connectivity
If files aren't visible:
Verify mount point exists
Check S3 bucket permissions
Ensure IAM role has proper access
Security Considerations
Keep bucket private and block public access
Use IAM roles instead of access keys
Enable encryption for sensitive data
Regularly audit access logs
Follow the principle of least privilege
Best Practices
Use versioning for important buckets
Implement proper monitoring
Regularly backup critical data
Keep S3FS updated
Document your configuration
This setup allows you to interact with your S3 bucket as if it were a local filesystem while maintaining the security and scalability benefits of S3 storage.