Attaching and mounting an S3 volume to an EC2 Linux instance involves creating an S3 bucket, installing necessary tools, and using s3fs
to mount the S3 bucket as a filesystem. Here’s a step-by-step guide:
Step 1: Create an S3 Bucket
- Log in to your AWS Management Console.
- Navigate to the S3 service.
- Click on “Create bucket.”
- Provide a unique bucket name and select the region.
- Configure bucket settings as needed and create the bucket.
Step 2: Launch an EC2 Instance
- Log in to your AWS Management Console.
- Navigate to the EC2 service.
- Launch an instance with your preferred configuration.
- Make sure to attach a role to your instance with S3 access permissions (e.g.,
AmazonS3FullAccess
policy).
Step 3: Connect to Your EC2 Instance
- Use SSH to connect to your EC2 instance.
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-instance-public-dns
Step 4: Install s3fs
- Update your package repository and install dependencies.
sudo apt-get update -y
sudo apt-get install -y s3fs
If you are using a RedHat-based system (like Amazon Linux or CentOS):
sudo yum update -y
sudo yum install -y s3fs-fuse
Step 5: Configure s3fs
- Create an S3 credentials file.
echo "ACCESS_KEY_ID:SECRET_ACCESS_KEY" > ~/.passwd-s3fs
chmod 600 ~/.passwd-s3fs
- Replace
ACCESS_KEY_ID
andSECRET_ACCESS_KEY
with your AWS access key and secret key.
Step 6: Mount the S3 Bucket
- Create a directory where you want to mount the S3 bucket.
mkdir ~/s3bucket
Mount the S3 bucket using s3fs
.
s3fs your-bucket-name ~/s3bucket -o passwd_file=~/.passwd-s3fs
Step 7: Verify the Mount
- List the contents of the mounted directory to verify
ls ~/s3bucket
Step 8: Automate Mounting (Optional)
To automate the mounting process so that it persists across reboots, add the following entry to your /etc/fstab
file:
- Open
/etc/fstab
in a text editor.
sudo nano /etc/fstab
Add the following line to the file:
s3fs#your-bucket-name /home/ec2-user/s3bucket fuse _netdev,passwd_file=/home/ec2-user/.passwd-s3fs 0 0
Adjust the paths as needed for your setup.
Save and close the file.
Test the configuration by unmounting and remounting the directory:
sudo umount ~/s3bucket
sudo mount ~/s3bucket