Use Amazon S3 with Amazon EC2 instances
Amazon Simple Storage Service (Amazon S3) is an object storage service that offers industry-leading scalability, data availability, security, and performance. You can use Amazon S3 to store and retrieve any amount of data for a range of use cases, such as data lakes, websites, backups, and big data analytics, from an Amazon EC2 instance or from anywhere over the internet. For more information, see What is Amazon S3?
There are two ways to access Amazon S3 data from your Amazon EC2 instances:
-
File access – Use Amazon S3 Files to mount an S3 bucket as a high performance file system on your instance.
-
Object access – Use the Amazon S3 API, AWS CLI, AWS SDKs, or tools like wget to copy objects to and from S3.
File access with Amazon S3 Files
Amazon S3 Files is a serverless file system that lets you mount your S3 general purpose bucket as a high performance file system on your compute instance. S3 Files provides access to your S3 objects as files using standard file system operations such as read and write on the local mount path.
You can mount an S3 file system to an EC2 instance either at launch, or after launch on a running instance.
Prerequisites
Before you set up S3 Files with your EC2 instance, make sure you have the following:
-
An S3 file system and at least one mount target in the available state. For information about creating an S3 file system, see Working with Amazon S3 Files in the Amazon S3 User Guide.
-
An EC2 instance running Linux OS with an instance profile attached to it. For information about the required permissions to mount the file system, see IAM roles and policies in the Amazon S3 User Guide.
-
Security groups that allow NFS traffic (port 2049) between your instance and the file system’s mount targets. For information about the required security groups settings, see Security groups in the Amazon S3 User Guide.
To mount a file system to an EC2 instance at launch using the EC2 console
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
. -
Choose Launch instance.
-
Select a subnet under Network settings.
-
Select the default security group to make sure that your EC2 instance can access your S3 file system. You can't access your EC2 instance by Secure Shell (SSH) using this security group. For access by SSH, later you can edit the default security and add a rule to allow SSH or a new security group that allows SSH. You can use the following settings:
-
Type: SSH
-
Protocol: TCP
-
Port Range: 22
-
Source: Anywhere 0.0.0.0/0
-
-
Under Storage, select File systems and choose S3 Files.
-
Under the file system dropdown, you will see your file systems in the Availability Zone based on the subnet you selected in your Network settings. Choose the S3 file system that you want to mount. If you don’t have any file systems, choose Create a new file system to create a new one.
-
Enter a local mount path on your EC2 instance where you want to mount the file system (for example,
/mnt/s3files). -
A command will be generated to mount the file system and add it to fstab. You can add this command to the User data field under Advanced details. Your EC2 instance will then be configured to mount the S3 file system at launch and whenever it's rebooted. You can also run these commands in your EC2 instance after it is launched.
-
-
Under Advanced details, attach an instance profile to your instance. Your IAM role must have permissions to mount the file system and access the S3 bucket. Learn more about required permissions.
-
Choose Launch instance.
-
After the instance launches, the required software utilities will be installed and file system mounted. You can view the file system by navigating to your local mount path.
To mount a file system to an EC2 instance after launch
-
Connect to your EC2 instance through Secure Shell (SSH) or by using EC2 Instance Connect in the EC2 console.
-
You mount your S3 file system using a mount helper utility
amazon-efs-utils. Install theamazon-efs-utilspackage using the following command:-
If you’re using Amazon Linux, run the following command to install efs-utils from Amazon's repositories:
sudo yum -y install amazon-efs-utils -
If you are using other supported Linux distributions
, you can do the following: curl https://amazon-efs-utils.aws.com/efs-utils-installer.sh | sudo sh -s -- --install -
For other Linux distributions, see the efs-utils
repository on GitHub.
-
-
Create a directory for the file system mount point using the following command:
sudo mkdir {path/to/mount} -
Mount the S3 file system:
FS="{YOUR_FILE_SYSTEM_ID}" sudo mount -t s3files $FS:/ {path/to/mount} -
Confirm the file system is mounted.
df -h {path/to/mount}
You can now read and write S3 objects as files on your local mount path using standard file system operations. If you have objects in your S3 bucket then you can view them as files using the following commands.
ls {path/to/mount}
Object-based access
You can copy files to and from Amazon S3 using the S3 API, AWS CLI, AWS SDKs, or standard HTTP tools. If you have permission, you can copy a file to or from Amazon S3 and your instance using one of the following methods.