Mount AWS S3 Bucket on An EC2 Ubuntu Instance

The current Ubuntu version on AWS EC2 instances is 18.04 LTS. After launch an Ubuntu Instance and connect to it by SSH, one can follow those steps to set up a permanent mount to S3 bucket.

  1. Install the packages.
    $ sudo apt update
    $ sudo apt-get install s3fs
  2. Create a global access key password file. The access key can be created in IAM console, following those guidelines.
    $ echo "0ASDF87XJKN34LK32EXAMPLE:AklasjlUUHNJ98SaadASKEXAMPLE1237890"  | sudo tee  /etc/passwd-s3fs
    $ sudo chmod 600 /etc/passwd-s3fs
  3. As a test, we could first mount it manually in command line.
    $ sudo s3fs myBucket /path/to/mount/point
    $ sudo ls /path/to/mount/point
    # List of files and folders 

    Note that myBucket is the name of the bucket, exclude s3:// part. If everything works well, we can un-mount the bucket and prepare for the auto-mount steps.

    $ sudo fusermount -u /path/to/mount/point
  4. To set up the auto-mount at booting. We first add a line into /etc/fstab,
    s3fs#myBucket       /path/to/mount/point       fuse _netdev,allow_other,umask=0007,uid=1000     0 0

    where uid=1000 is the user's id. It could be found by

    $ id -u

    Now we can reboot the instance, the S3 bucket will be mount automatically. Alternatively, we can test the auto-mount without reboot.

    $ sudo mount -a
This entry was posted in AWS. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *