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. With S3 Files, you can access your S3 objects as files by 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 Linux instance 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 group 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.
-
Under Network settings, do the following:
-
Choose Edit.
-
For Subnet, select a subnet.
-
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, you can later edit the default security group and add a rule to allow SSH, or add 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 Configure storage, do the following:
-
Under File systems, choose S3 Files.
-
Choose Add shared file system.
-
For S3 file system, your file systems appear in the Availability Zone based on the subnet that 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. For more information about the required permissions, see IAM roles and policies in the Amazon S3 User Guide.
-
Choose Launch instance.
After the instance launches, the required software utilities are installed and the file system is 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.
-
To mount your S3 file system, use the mount helper utility
amazon-efs-utils. Depending on your Linux distribution, use one of the following commands to install theamazon-efs-utilspackage:-
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
, run the following command: 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}
To view objects in your S3 bucket as files
Having completed the preceding procedures, 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, you can view them as files by using the following command:
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 the required permissions, you can copy a file to or from Amazon S3 and your instance using one of the following methods.