Mounting Amazon S3 as Storage on AWS EC2 Using s3fs

English

Although I've never really written about it before, I've recently been using Amazon Web Services (AWS)—the cloud everyone talks about—to run a Minecraft server.
It's so my daughter and I can play Minecraft together. Actually, I needed a server so that either of us could play independently even when the other wasn't on. And guess what? AWS lets you use their services for free for a whole year!
Today's topic: Mounting S3 as storage on an AWS EC2 instance.
Everything is working surprisingly well, so I'm writing this down to keep a record.

■ Background


My Minecraft world backup files are taking up way too much space.
They're cluttering up the EC2 storage. Looking ahead to when the free tier ends in a year and it starts costing money, I want to keep the EC2 instance itself as compact and low-capacity as possible. I could easily just upgrade the EC2 storage, but apparently, S3 is much cheaper to use.
So, I'm trying this out partly as a learning experience.
S3 can be slow. Plus, if I put my main game data on S3, constant read/write access might spike the traffic and increase costs. Let's just put the backup files on S3.
With my usual Unix-like mindset, the idea is to mount S3 and create a symbolic link to the backup directory there.
So I did a quick Google search. Oh boy, plenty of guides pop up.

■ Directory Structure Concept


~/MinecraftBedRockServer
|-worlds
|-backup -> link:~/mnt/S3_Minecraft/worlds_backup
/mnt/S3 ... Newly created, mount S3 here.
An S3 bucket dedicated to Minecraft. Since this EC2 instance is exclusively a Minecraft server anyway, maybe just an S3 bucket is fine.
|-worlds_backup
・Candidate 1... Rejected
~/mnt/S3_Minecraft ... Newly created, mount S3 here.
An S3 bucket dedicated to Minecraft. Since this EC2 instance is just a Minecraft server, is S3 alone enough?
|-worlds_backup
・Candidate 2... Rejected
~/mnt/s3 ... Newly created, mount S3 here.
|-Minecraft ... What if I want to store non-Minecraft files later? Should I use a separate S3 bucket for those?
|-worlds_backup

■ References for Mounting S3


Candidate 1: Mounting tool: goofys ... Apparently, this one is faster.
https://www.acrovision.jp/service/aws/?p=797
1. Create an S3 bucket named: S3_Minecraft
2. Create an IAM role so the software can access it. Same as the role I created when accessing S3 from WordPress.
  Set it to AmazonS3FullAccess.
3. Install various dependencies for goofys?
Install what's needed.
■ Git
■ golang
■ goofys
Here are the commands to run:
$ sudo yum -y install golang fuse git
$ export GOPATH=$HOME/go
$ go get github.com/kahing/goofys
$ go install github.com/kahing/goofys
 …Rejected this because the name of the alternative option below sounded better.
■ Candidate 2: Mounting tool: s3fs
  …I went with this one because the name sounded more fitting for the job.
  Apparently, this one is slower though.
https://qiita.com/latin1/items/aade223be36a87cbe538
I'd hate it if this were exclusive to Amazon Linux.
Wait, I'm using Ubuntu. Let me search for that, and sure enough, guides exist.
https://qiita.com/taito273/items/17e469373478ff38a200
0. Prepare S3 Bucket
Named minecraft-nyanpu. No uppercase letters. No underscores. Globally unique.
1. Create an IAM user
  Set it to AmazonS3FullAccess.
An Access Key ID and Secret Access Key will be displayed, so make sure to save them.
  The reference URL creates it from a group, but creating a USER is fine, right?
I set it up the same way as my WordPress exercise.
Named it s3user.
Apparently, you can also use an IAM Role. Installing s3fs...
2. Installing s3fs
Pull from GitHub and build.
For Ubuntu, use the usual apt-get.
sudo apt-get install automake autotools-dev g++ git libcurl4-gnutls-dev libfuse-dev libssl-dev libxml2-dev make pkg-config
Grab the s3fs code, build it, and install.
$ git clone https://github.com/s3fs-fuse/s3fs-fuse.git
$ cd s3fs-fuse
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install
Usage of /: 92.6% of 7.69GB Users logged in: 0
Disk space is getting tight...
After installing, I removed the GitHub repository files.
Ran rm -r on s3fs as well.
Then cleared the cache with sudo apt-get clean.
Usage of /: 90.4% of 7.69GB Users logged in: 0
■ Create Mount Point
$ sudo mkdir /mnt/s3
■ Save IAM Access Key
$ echo <AccessKeyID>:<SecretAccessKey> | sudo tee -a /etc/passwd-s3fs
$ sudo chmod 640 /etc/passwd-s3fs
■ Mount
sudo s3fs <bucket-name> <mount-point> -o rw,allow_other,uid=<UID>,gid=<GID>,default_acl=public-read
In practice, it looks like this:
sudo s3fs minecraft-nyanpu /mnt/s3 -o rw,allow_other,uid=1000,gid=1000,default_acl=public-read

■ Wait, there's no auto-mount on startup!

Was it editing fstab?
s3fs#bucketname /share fuse auto,rw,allow_other,use_cache=/tmp,uid=222,gid=500 0 0
# Wait, isn't that a comment?
# echo "s3fs#testubuntusvr01data /mnt/objstorage/data fuse nonempty,allow_other,url=http://inter-st.gmocloud.com,passwd_file=/etc/s3fs/s3fspasswd 0 0" >> fstab
s3fs#[bucket-name] [directory-to-mount] fuse allow_other,use_cache=/tmp,uid=[UID],gid=[GID],passwd_file=/etc/passwd-s3fs 0 0
s3fs#[bucket-name]... doesn't that look weird? Guess it's fine.

■ Result

The mount was successful, and I could view directories, navigate them, and read files, but writing or creating new files wasn't working.
I tried enabling S3 ACLs and various other tweaks, but nothing worked.

■ Tried rebooting.

The next day, having run a mob trap continuously for three days in Minecraft, my CPU credits had dropped to zero.
Previously, the load wasn't high enough to consume CPU credits, but after removing a stray torch, spawn rates increased and it seems the load went into the negative.
Since my CPU credits were practically at zero anyway, I decided to reboot the server.
I rebooted after credits recovered to 17, but they stayed at 17. When you're above 30 it drops down to 30, but it doesn't look like it accumulates back up past 30.
The S3 mount defined in fstab mounted successfully. And for some reason, file writing, which hadn't worked before, started working.

■ Probable Cause


1. Command line failed, but fstab succeeded.
2. Thanks to enabling S3 ACLs, maybe there was a propagation delay or a reboot was required?
 My WordPress exercise was similar—it just magically started working out of nowhere.
I'll try disabling ACLs later and see what happens.
// Logs below
Welcome to Ubuntu 20.04.3 LTS (GNU/Linux 5.11.0-1028-aws x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Fri Feb 4 13:17:59 JST 2022
System load: 1.04 Processes: 124
Usage of /: 77.7% of 7.69GB Users logged in: 0
Memory usage: 19% IPv4 address for eth0: 172.31.45.138
Swap usage: 0%
* Ubuntu Pro delivers the most comprehensive open source security and
compliance features.
https://ubuntu.com/aws/pro
26 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
Last login: Fri Feb 4 10:05:08 2022 from 121.103.0.23
ubuntu@ip-172-31-45-138:~$ mount
/dev/xvda1 on / type ext4 (rw,relatime,discard)
devtmpfs on /dev type devtmpfs (rw,relatime,size=489256k,nr_inodes=122314,mode=755,inode64)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
:
Omitted
:
/var/lib/snapd/snaps/snapd_14549.snap on /snap/snapd/14549 type squashfs (ro,nodev,relatime,x-gdu.hide)
s3fs on /mnt/s3 type fuse.s3fs (rw,relatime,user_id=0,group_id=0,allow_other) .....Successfully mounted
tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,size=99172k,mode=700,uid=1000,gid=1000,inode64)
tmpfs on /run/snapd/ns type tmpfs (rw,nosuid,nodev,size=99176k,mode=755,inode64)
ubuntu@ip-172-31-45-138:~$ ls /mnt/s3
levelname.txt world_icon.jpeg worlds_backup
ubuntu@ip-172-31-45-138:~$ touch /mnt/s3/xxx ... Try creating a file named "xxx"
ubuntu@ip-172-31-45-138:~$ ls
HowToUseRemoteXonSsh.txt MinecraftServerBedrock
ubuntu@ip-172-31-45-138:~$ ls /mnt/s3
levelname.txt world_icon.jpeg worlds_backup xxx ... File "xxx" was successfully written

■ Testing and Verification

Since we're at it, let's test things out.
1. Is it a difference between the command line and fstab?
------
Unmount and try mounting via the command line again.
ubuntu@ip-172-31-45-138:~$ sudo umount /mnt/s3
ubuntu@ip-172-31-45-138:~$ ls /mnt/s3
ubuntu@ip-172-31-45-138:~$ sudo s3fs minecraft-nyanpu /mnt/s3 -o rw,allow_other,uid=1000,gid=1000,default_acl=public-read
ubuntu@ip-172-31-45-138:~$ ls /mnt/s3
levelname.txt world_icon.jpeg worlds_backup xxx
ubuntu@ip-172-31-45-138:~$ touch /mnt/s3/xxx2
touch: failed to close '/mnt/s3/xxx2': Operation not permitted
ubuntu@ip-172-31-45-138:~$
-----
It fails.
Seems like the command line method is the issue.
2. Revert ACL to disabled, reboot, and test fstab
levelname.txt world_icon.jpeg worlds_backup xxx
ubuntu@ip-172-31-45-138:~$ touch /mnt/s3/xxx2
ubuntu@ip-172-31-45-138:~$
ubuntu@ip-172-31-45-138:~$
ubuntu@ip-172-31-45-138:~$ ls /mnt/s3
levelname.txt world_icon.jpeg worlds_backup xxx xxx2
ubuntu@ip-172-31-45-138:~$
It worked.
Conclusion: Only the fstab configuration works properly.
Is the command-line method bugged? Or were the arguments wrong?

■ Using It for the Real Backup

ln -s /mnt/s3/worlds_backup worlds/backup
Modify the backup shell script
so that backup files are saved under worlds/backup/.
Start the Minecraft script and wait until backup time.
Checked the S3 console: OK
Confirmed that files are being created via the command line: OK
ubuntu@ip-172-31-45-138:~/MinecraftServerBedrock$ ls -l worlds/backup/
total 2065875
-rw-rw-r-- 1 ubuntu ubuntu 92340073 Feb 4 07:00 mc_backup_hourly_20220204-070002.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92343777 Feb 4 07:30 mc_backup_hourly_20220204-073001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92443911 Feb 4 08:00 mc_backup_hourly_20220204-080001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92561005 Feb 4 08:30 mc_backup_hourly_20220204-083001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92660318 Feb 4 09:00 mc_backup_hourly_20220204-090001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92779749 Feb 4 09:30 mc_backup_hourly_20220204-093001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92670514 Feb 4 10:00 mc_backup_hourly_20220204-100001.tar.gz
-rw-rw-r-- 1 ubuntu ubuntu 92435645 Feb 4 14:00 mc_backup_hourly_20220204-140001.tar.gz

■ Now that capacity has increased (since storage limits are no longer an issue?), I could extend the backup retention period, but
30-minute backups are just for rolling back in case of accidental death, so they probably aren't necessary long-term.
Instead, maybe I should bring back daily backups and keep them for about a week?

Leave a Reply