Mounting an S3 Bucket to Ubuntu EC2 Instance

Rachael Creager
3 min readJan 28, 2020
Bucket time!

When you create a fresh EC2 instance, one of the first things you may want to do is mount an S3 bucket to your instance. This is convenient because mounting the bucket like a disk allows you to navigate to the bucket like a directory. You can add new content to the bucket either directly or via scp through your home directory. Plus, you mounted bucket doesn’t count towards your instance’s memory, so you can make a large bucket available to a small node!

The directions for other Unix systems are similar, but you might need to use yum instead of apt-get.

The first time your start an instance:

  • Create a new EC2 Ubuntu instance (I used 18.04 but this should work with 16.04 as well). Once the instance is running, ssh into the instance
  • Confirm that the permissions are working for your instance. If you get an error here, there’s something wrong with the permissions for your instance and you’ll need to make a new one.
sudo apt-get update
  • Install S3FS, then confirm it’s installed:
sudo apt-get -y install s3fswhich s3fs
  • Go to the AWS Console and create an access key. This is located in Access Management → Users page, then click your IAM account listed, then the Security Credentials tab, and finally Create Access Key.
  • Once you have the access key information, add this information to your EC2 instance by echo-ing it to a password file (make sure to replace access-key-id and secret-access-key with the info from the previous step):
echo access-key-id:secret-access-key > /home/ubuntu/.passwd-s3fs
  • Change the permissions of your password file:
chmod 600 /home/ubuntu/.passwd-s3fs
  • To make the bucket available to non-root users, edit the file /etc/fuse.conf (you will need sudo permissions) and remove the # sign from the start of the final line to uncomment it:
sudo vim /etc/fuse.conf#user_allow_other -> user_allow_other
  • Now you’re ready to mount! Create a directory where you will mount the bucket. You can make it where you want, but don’t make it anywhere in /home or it will count towards your memory usage.
mkdir /s3mnt
  • Now it’s time to run the mounting command! We’ll need to briefly be the root user, but we can exit right after running this command. Make sure to replace my-bucket-name with the name of your (already created) bucket.
sudo sus3fs -o allow_other -o use_cache=/tmp/cache3 -o passwd_file=/home/ubuntu/.passwd-s3fs my-bucket-name /s3mntexit
  • You can now cd to your mounted directory to see your bucket contents! Write and read to your heart’s delight, noting that some copy commands (such as scp) might have permissions issues.

Unmounting the bucket:

If you want to unmount your bucket for any reason, just use the umount command:

umount /s3mnt

Whenever you restart an instance:

When you stop and restart the instance, you can remount your bucket with the final step of the “fresh start” directions:

sudo sus3fs -o allow_other -o use_cache=/tmp/cache3 -o passwd_file=/home/ubuntu/.passwd-s3fs my-bucket-name /s3mntexit

Good luck! If you have any problems with this tutorial, please comment!

--

--