{"id":474,"date":"2025-09-26T15:19:33","date_gmt":"2025-09-26T15:19:33","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=474"},"modified":"2025-09-26T15:19:33","modified_gmt":"2025-09-26T15:19:33","slug":"mount-aws-s3-bucket-to-ec2-instance","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=474","title":{"rendered":"Mount AWS s3 Bucket to Ec2 Instance(Tutorial)"},"content":{"rendered":"<p>In this tutorial, you will learn how to mount an AWS S3 bucket to an EC2 instance with practical examples.<\/p>\n<p>Mounting an S3 bucket to an EC2 instance is a practical way to share data, configurations, and large datasets without constantly downloading them. <\/p>\n<p>For example, ML teams often store datasets in S3 and access them directly from EC2 for machine learning or analytics. Similarly, it is frequently used in ETL pipelines.<\/p>\n<p>In this guide, we will use <strong>Mountpoint for Amazon S3<\/strong> for this use case.<\/p>\n<h2 id=\"mountpoint-for-the-amazon-s3\">Mountpoint for the Amazon S3 <\/h2>\n<p><a href=\"https:\/\/github.com\/awslabs\/mountpoint-s3?utm_source=newsletter.devopscube.com&#038;utm_medium=referral&#038;utm_campaign=mounting-s3-using-fstab\" rel=\"noopener noreferrer nofollow\"><strong>Mountpoint for Amazon S3<\/strong><\/a>&nbsp;is a&nbsp;high-throughput, open-source file client that allows your applications to connect to Amazon S3 buckets and access S3 objects as if they were files in a local file system.<\/p>\n<p>In short, here is what it does.<\/p>\n<p>It acts as an intermediary, translating standard file system operations (like open, read, write, list directory) from your applications into S3 API calls (like GET, PUT, LIST \u2192 REST<strong>&nbsp;<\/strong>based API over HTTPS).<\/p>\n<p>Here is a sample ec2 s3 mount workflow.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-1.png\" class=\"kg-image\" alt=\"S3 Mount With ec2 fstab&nbsp;\" loading=\"lazy\" width=\"996\" height=\"1216\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/06\/image-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-1.png 996w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">For Mountpoint for S3 to work properly, the EC2 instance must have an IAM role that grants basic read and write permissions to the S3 bucket.<\/div>\n<\/div>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>Below are the prerequisites for this setup.<\/p>\n<ul>\n<li>An S3 bucket<\/li>\n<li><a href=\"https:\/\/devopscube.com\/use-aws-cli-create-ec2-instance\/\">EC2 instance<\/a><\/li>\n<li><a href=\"https:\/\/devopscube.com\/install-configure-aws-cli-linux\/\">AWS CLI<\/a> installed with permission to create IAM Roles.<\/li>\n<\/ul>\n<p>Now, lets get started with the setup.<\/p>\n<h2 id=\"install-mountpoint-for-amazon-s3\">Install Mountpoint for Amazon S3<\/h2>\n<p>Follow the steps below to install Mountpoint for Amazon S3.<\/p>\n<p>The following steps are for Ubuntu. For other Linux distributions, refer to the <a href=\"https:\/\/github.com\/awslabs\/mountpoint-s3\/blob\/main\/doc\/INSTALL.md?ref=devopscube.com\" rel=\"noreferrer\">official documentation<\/a>.<\/p>\n<h3 id=\"step-1-download-the-mountpoint-package\">Step 1: Download the Mountpoint Package<\/h3>\n<p>Run the following command to download the Mountpoint package.<\/p>\n<pre><code>wget https:\/\/s3.amazonaws.com\/mountpoint-s3-release\/latest\/x86_64\/mount-s3.deb\n<\/code><\/pre>\n<h3 id=\"step-2-install-mountpoint\">Step 2: Install Mountpoint<\/h3>\n<p>Run the following command to install Mountpoint.<\/p>\n<pre><code>sudo apt-get install -y .\/mount-s3.deb\n<\/code><\/pre>\n<h3 id=\"step-3-verify-the-installation\">Step 3: Verify the Installation<\/h3>\n<p>To verify if the Mountpoint for Amazon S3 is installed, run the following command.<\/p>\n<pre><code>mount-s3 --version<\/code><\/pre>\n<p>You will get an output as follows:<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-4.png\" class=\"kg-image\" alt=\"checking the version of mount S3\" loading=\"lazy\" width=\"960\" height=\"390\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/06\/image-4.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-4.png 960w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<h2 id=\"create-iam-role-and-attach-to-ec2\">Create IAM Role and Attach to EC2<\/h2>\n<p>To allow EC2 to access the S3 bucket, we need to create an <a href=\"https:\/\/devopscube.com\/aws-iam-role-instance-profile\/\" rel=\"noreferrer\">IAM role<\/a> with the necessary permissions. This role will then be attached to the EC2 instance.<\/p>\n<p>We will be creating a role for the bucket mounts through the AWS CLI. Follow the steps given below.<\/p>\n<h3 id=\"step-1-create-policy\">Step 1: Create Policy<\/h3>\n<p>Run the following command to create a <code>mountpoint-s3-policy.json<\/code> file with the permission to read, write, and delete.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\u26a0\ufe0f<\/div>\n<div class=\"kg-callout-text\">Update your S3 buckets name in below command before running it.<\/div>\n<\/div>\n<pre><code>cat &lt;&lt;EOF &gt; mountpoint-s3-policy.json\n{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"s3:ListBucket\"\n            ],\n            \"Resource\": \"arn:aws:s3:::&lt;bucket-name&gt;\"\n        },\n        {\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"s3:GetObject\",\n                \"s3:PutObject\",\n                \"s3:DeleteObject\"\n            ],\n            \"Resource\": \"arn:aws:s3:::&lt;bucket-name&gt;\/*\"\n        }\n    ]\n}\nEOF<\/code><\/pre>\n<p>Then, run the following command to create a policy with the JSON file.<\/p>\n<pre><code>aws iam create-policy \\\n    --policy-name s3-mount-policy \\\n    --policy-document file:\/\/mountpoint-s3-policy.json<\/code><\/pre>\n<p>After running this command, you will get the ARN of the newly created policy in the output. Note the <a href=\"https:\/\/devopscube.com\/aws-arn-guide\/\" rel=\"noreferrer\">ARN<\/a>, you will need it in another step.<\/p>\n<h3 id=\"step-2create-role\">Step 2:Create Role<\/h3>\n<p>Run the following command to create a <code>mountpoint-s3-trustpolicy.json<\/code> <\/p>\n<p>Since we will be attaching it to an <a href=\"https:\/\/devopscube.com\/aws-ssm\/\" rel=\"noreferrer\">EC2 instance<\/a>, let&#8217;s create trustpolicy based on it.<\/p>\n<pre><code>cat &lt;&lt;EOF &gt; mountpoint-s3-trustpolicy.json\n{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Allow\",\n      \"Principal\": {\n        \"Service\": \"ec2.amazonaws.com\"\n      },\n      \"Action\": \"sts:AssumeRole\"\n    }\n  ]\n}\nEOF<\/code><\/pre>\n<p>Then, run the following command to create a role with the JSON file.<\/p>\n<pre><code>aws iam create-role \\\n    --role-name s3-mount-role \\\n    --assume-role-policy-document file:\/\/mountpoint-s3-trustpolicy.json<\/code><\/pre>\n<p>This will create and give the ARN of the newly created role in the output.<\/p>\n<h3 id=\"step-3-attach-the-policy-to-the-role\">Step 3: Attach the Policy to the Role<\/h3>\n<p>The policy and the role have been created, let&#8217;s attach the policy to the role.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\u26a0\ufe0f<\/div>\n<div class=\"kg-callout-text\">Update your policies ARN in below command before running it.<\/div>\n<\/div>\n<pre><code>aws iam attach-role-policy \\\n    --role-name s3-mount-role \\\n    --policy-arn &lt;YOUR_POLICY_ARN&gt;<\/code><\/pre>\n<h3 id=\"step-4-create-an-instance-profile\">Step 4: Create an Instance Profile<\/h3>\n<p>And the final step is to create an instance profile, so that your can attach the role to the EC2 instance.<\/p>\n<p>Run the following command to create an instance profile.<\/p>\n<pre><code>aws iam create-instance-profile \\\n    --instance-profile-name s3-mount-instance-profile<\/code><\/pre>\n<p>Now, attach the role to the instance profile using the following command.<\/p>\n<pre><code>aws iam add-role-to-instance-profile \\\n    --instance-profile-name s3-mount-instance-profile \\\n    --role-name s3-mount-role<\/code><\/pre>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\ud83d\udca1<\/div>\n<div class=\"kg-callout-text\">Once the instance profile is created, attach it to your EC2 instance, which you are going to mount S3 bucket.<\/div>\n<\/div>\n<h2 id=\"mount-s3-bucket-on-ec2-instance\">Mount S3 Bucket on EC2 Instance<\/h2>\n<p>In the following steps, I will show you how to mount an S3 bucket on the EC2 instance.<\/p>\n<p>Execute all the steps from within the ec2 instance.<\/p>\n<h3 id=\"step-1-create-a-mount-directory\">Step 1: Create a Mount Directory<\/h3>\n<p>To create a mount directory, run the following command.<\/p>\n<pre><code>sudo mkdir \/mnt\/dcmount<\/code><\/pre>\n<p>You can choose any directory name you want.<\/p>\n<h3 id=\"step-2-mount-the-s3-bucket\">Step 2: Mount the S3 Bucket<\/h3>\n<p>Once the directory is created, mount the S3 bucket to the directory we created in the previous step.<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-blue\">\n<div class=\"kg-callout-emoji\">\u26a0\ufe0f<\/div>\n<div class=\"kg-callout-text\">Update your bucket name in below command before running it<\/div>\n<\/div>\n<pre><code>sudo mount-s3 &lt;bucket-name&gt; \/mnt\/dcmount<\/code><\/pre>\n<p>Once it&#8217;s mounted and the bucket is not empty, you can view the files and folders in the bucket within the mount directory.<\/p>\n<h3 id=\"step-3-test-the-s3-mount\">Step 3: Test the s3 Mount<\/h3>\n<p>Let&#8217;s test the mount by creating a folder inside the mounted directory and creating a file inside that folder.<\/p>\n<p>To create a new folder, run the following command.<\/p>\n<pre><code>sudo mkdir \/mnt\/dcmount\/mount<\/code><\/pre>\n<p>Now, create a file inside the newly created folder.<\/p>\n<pre><code>sudo touch \/mnt\/dcmount\/mount\/hello.txt<\/code><\/pre>\n<p>Check the S3 bucket, you can find a new folder and a file inside it as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-6.png\" class=\"kg-image\" alt=\"objects stored in AWS S3 bucket\" loading=\"lazy\" width=\"1208\" height=\"880\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/06\/image-6.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/06\/image-6.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/06\/image-6.png 1208w\" sizes=\"auto, (min-width: 720px) 720px\"><\/figure>\n<p>If you no longer want the mount, run the following command.<\/p>\n<pre><code>sudo umount \/mnt\/dcmount<\/code><\/pre>\n<h2 id=\"mount-s3-with-ec2-fstab-persistent-mounts\">Mount S3 with EC2 fstab (Persistent Mounts)<\/h2>\n<p>But first, what is <code>fstab<\/code>?<\/p>\n<p>The fstab (short for file systems table) is a file in Linux and Unix systems that tells the system which storage devices or filesystems to mount automatically when the server starts up.  <\/p>\n<p>It is the common method used to mount volumes in EC2 to persist <a href=\"https:\/\/devopscube.com\/mount-ebs-volume-ec2-instance\/\" rel=\"noreferrer\">EBS<\/a> or EFS volume mounts during reboots. It\u2019s located at&nbsp;<strong><code>\/etc\/fstab<\/code><\/strong><\/p>\n<p>Mountpoint for Amazon S3 now supports automatic mounting of S3 buckets using the standard Linux fstab file.<\/p>\n<p>This makes it easier to set up persistent mounts for your S3 buckets on EC2 instances.<\/p>\n<p>By adding a simple entry to fstab, your&nbsp;<strong>S3 buckets will mount automatically<\/strong>&nbsp;whenever the instance boots, just like local drives or network file systems like EBS or NFS.<\/p>\n<p>The best part is that this approach follows familiar Linux system admin practices, making mount management more consistent and centralized.<\/p>\n<p>You can follow the steps below to test it yourself.<\/p>\n<p>Open the fstab file:<\/p>\n<pre><code class=\"language-bash\">sudo vi \/etc\/fstab<\/code><\/pre>\n<p>Create a mount directory for the bucket using the following command:<\/p>\n<pre><code class=\"language-bash\">sudo mkdir -p \/mnt\/s3-bucket<\/code><\/pre>\n<p>Add the following line to the fstab file and save it. In this example,&nbsp;<code>dcmount<\/code>&nbsp;is the S3 bucket name, and&nbsp;<code>\/mnt\/s3-bucket<\/code>&nbsp;is the local mount path. Make sure to replace these with your actual bucket name and desired mount location:<\/p>\n<pre><code class=\"language-bash\">s3:\/\/dcmount\/ \/mnt\/s3-bucket mount-s3 _netdev,nosuid,nodev,nofail,rw 0 0<\/code><\/pre>\n<p>Now test the mount using the following command:<\/p>\n<pre><code class=\"language-bash\">sudo mount -a<\/code><\/pre>\n<p>On a successful mount, you will get the following message.<\/p>\n<pre><code class=\"language-bash\">bucket dcmount is mounted at \/mnt\/s3-bucket<\/code><\/pre>\n<h2 id=\"automating-with-ec2-user-data-script\">Automating with EC2 User Data Script<\/h2>\n<p>In real-world scenarios, you&#8217;ll often want to automate the fstab entry using <a href=\"https:\/\/devopscube.com\/ec2-user-data\/\" rel=\"noreferrer\">EC2 user data scripts<\/a>. This is especially important since most deployments&nbsp;<strong>use pre-configured AMIs<\/strong>, and instances are launched as part of an automated pipeline.<\/p>\n<p>Here&#8217;s a simple, tested shell script you can use in your EC2 user data to add the <strong><code>fstab<\/code><\/strong> entry at launch time.<\/p>\n<pre><code class=\"language-bash\">#!\/bin\/bash\n\nS3_BUCKET_NAME=\"dcmount\"\nMOUNT_POINT=\"\/mnt\/s3-bucket\"\nFSTAB_ENTRY=\"s3:\/\/${S3_BUCKET_NAME}\/  ${MOUNT_POINT}  mount-s3   _netdev,nosuid,nodev,nofail,rw 0 0\"\n\nsudo mkdir -p \"${MOUNT_POINT}\"\n\nif ! grep -q \"s3:\/\/${S3_BUCKET_NAME}.*${MOUNT_POINT}\" \/etc\/fstab; then\n  echo \"Adding S3 mount to \/etc\/fstab\"\n  echo \"${FSTAB_ENTRY}\" | sudo tee -a \/etc\/fstab\nelse\n  echo \"S3 mount entry already exists in \/etc\/fstab\"\nfi<\/code><\/pre>\n<h2 id=\"faq\">FAQ<\/h2>\n<h3 id=\"can-i-mount-multiple-s3-buckets-to-the-same-ec2-instance\">Can I mount multiple S3 buckets to the same EC2 instance?<\/h3>\n<p>Yes, you can mount multiple s3 buckets by repeating the mount commands with different directories inside ec2.<\/p>\n<h3 id=\"is-mountpoint-for-s3-free-to-use\">Is Mountpoint for S3 free to use?<\/h3>\n<p>Yes, it is an open-source client developed by AWS. However, standard S3 request and data transfer costs apply.<\/p>\n<h3 id=\"how-is-this-different-from-s3fs-fuse\">How is this different from s3fs-fuse?<\/h3>\n<p>Mountpoint for S3 provides higher throughput and is optimized for AWS workloads compared to s3fs-fuse.<\/p>\n<h3 id=\"does-the-s3-mount-persist-after-ec2-reboot\">Does the S3 mount persist after EC2 reboot?<\/h3>\n<p>No, not by default. You must configure it with <code>fstab<\/code> or EC2 user data for persistence as explained in the blog.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this hands-on guide, we looked at two key methods to mount an S3 bucket on an EC2 instance. Manual mounting and automatic mounting using <code>fstab<\/code>.<\/p>\n<p>This approach is widely used in real-world deployments, especially in data pipelines and analytics. <\/p>\n<p>Try it out and let us know your experience in the comments!<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/mount-aws-s3-bucket-to-ec2-instance\/\" target=\"_blank\" rel=\"noopener noreferrer\">Mount AWS s3 Bucket to Ec2 Instance(Tutorial) \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/mount-aws-s3-bucket-to-ec2-instance\/<\/p>\n","protected":false},"author":1,"featured_media":475,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=474"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/474\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/475"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}