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.
Prerequisites
Before you set up S3 Files with your EC2 instance, make sure you have the following:
-
You must have an S3 file system and at least one mount target in available state. For instructions on creating an S3 file system, see the Amazon S3 Files User Guide.
-
An EC2 instance running Linux OS with an instance profile attached to it. Learn more about required permissions to mount the file system.
-
Security groups that allow NFS traffic (port 2049) between your instance and the file system’s mount targets. Learn more about required security groups settings.
Mount S3 file system to an EC2 instance
You can either mount an S3 file system at launch or after launch on a running instance.
Mount a file system at instance launch using the EC2 console
Open the Amazon EC2 console at https://eusc-de-east-1.console.amazonaws-eusc.eu/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 drop down, 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 User data field in 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 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.
Mount a file system to an Amazon EC2 instance after launch
Connect to your EC2 instance through Secure Shell (SSH) or EC2 Instance Connect on 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 -
Refer to the efs-utils GitHub repository
for other Linux distributions.
-
-
Create a directory for 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.