How to Mount AWS S3 Bucket on EC2 Using S3FS: A Step-by-Step Guide

How to Mount AWS S3 Bucket on EC2 Using S3FS: A Step-by-Step Guide

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:

  1. Create bucket with these settings:

    • Bucket type: General purpose

    • Block all public access: Enabled (recommended for security)

    • Bucket Versioning: Optional

    • Default encryption: Enabled (recommended)

  2. 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/*"
        }
    ]
}

  1. 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

  1. If mount fails, check:

    • IAM role permissions

    • S3 bucket name

    • Network connectivity

  2. If files aren't visible:

    • Verify mount point exists

    • Check S3 bucket permissions

    • Ensure IAM role has proper access

Security Considerations

  1. Keep bucket private and block public access

  2. Use IAM roles instead of access keys

  3. Enable encryption for sensitive data

  4. Regularly audit access logs

  5. Follow the principle of least privilege

Best Practices

  1. Use versioning for important buckets

  2. Implement proper monitoring

  3. Regularly backup critical data

  4. Keep S3FS updated

  5. 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.