How to Connect S3 Bucket from An EC2 Instance?

Learn how to seamlessly connect your Amazon S3 storage bucket with an EC2 instance on AWS. Follow this post to configure IAM roles, set up AWS CLI, and effectively manage data operations between EC2 and S3, ensuring secure and efficient cloud resource management.

download-icon
Free Download
for VM, OS, DB, File, NAS, etc.
iris-lee

Updated by Iris Lee on 2024/07/17

Table of contents
  • Why connect S3 bucket from EC2 instance?

  • How to connect S3 bucket from EC2 instance?

  • Flexible backup and recovery for AWS EC2 instances with Vinchin solution

  • Connect S3 bucket from EC2 instance FAQs

  • Conclusion

Are you looking for a robust AWS EC2 backup solution? Try Vinchin Backup & Recovery!↘ Download Free Trial

In the AWS environment, Amazon EC2 is a web service that allows users to run virtual servers, known as instances, in the cloud. It provides resizable compute capacity, making web-scale cloud computing easier for developers. Amazon S3 is an object storage service that offers industry-leading scalability, data availability, security, and performance. S3 is designed to store and retrieve any amount of data from anywhere on the web. Connecting an EC2 instance to an S3 bucket is a common requirement. This article will detail the steps to achieve this connection.

Why connect S3 bucket from EC2 instance?

Data storage and backup: S3 provides scalable storage, so it’s ideal for storing and backing up large amounts of data from your EC2 instances.

Data sharing: Multiple EC2 instances can share data easily through a common S3 bucket. This is useful for distributed applications where instances need access to shared files.

Static file hosting: S3 can be used to host static files such as images, videos, and other media, which can be served directly to users from S3, reducing the load on EC2 instances.

Cost-effective Storage: Storing data in S3 can be more cost-effective than using EBS volumes for data that doesn’t require high IOPS or immediate access.

Integration with AWS services: S3 integrates with many AWS services, such as AWS Lambda for serverless computing, Amazon Athena for querying data directly in S3, and Amazon EMR for big data processing.

How to connect S3 bucket from EC2 instance?

Prerequisites

Before you begin, ensure the following prerequisites:

  • AWS Account: Have a valid AWS account.

  • EC2 Instance: A created and running EC2 instance.

  • S3 Bucket: A created S3 bucket.

  • IAM Role and Policy: Appropriate IAM role and permission policy to allow the EC2 instance to access the S3 bucket.

Step 1: Configure an IAM Role

1. Create an IAM Role:

Log in to the AWS Management Console.

Navigate to the IAM service.

Select Roles and click Create role.

Choose AWS service as the trusted entity type.

Select EC2 as trusted entity.

Click Next.

2. Attach Permissions:

 Search for and select AmazonS3FullAccess or a custom S3 policy with the necessary permissions. Then click Next.

Attach Permissions3. Name the role and create it.

4. Attach the Role to the EC2 Instance:

Navigate to the EC2 service.

Select your EC2 instance.

Click Actions > Security > Modify IAM role.

Select the IAM role you created, then save.

Attach the Role to the EC2 Instance

Step 2: Install and Configure AWS CLI

1. Install AWS CLI and connect to your EC2 instance (via SSH).

2. Configure AWS CLI (if default configuration is needed):

Run the following command and follow the prompts to enter your AWS access key, secret key, region, and output format. Typically, this step is not needed on an EC2 instance as it will use the attached IAM role.

aws configure

Step 3: Access S3 Using AWS CLI

1. List all S3 buckets:

aws s3 ls

2. Upload a file to an S3 bucket:

Move a file: 

aws s3 mv /path/to/local/file s3://your-bucket-name/

Copy a file: 

aws s3 cp /path/to/local/file s3://your-bucket-name/

3. Download a file from an S3 bucket:

aws s3 cp s3://your-bucket-name/your-file /path/to/local/directory

4. Sync a local directory with an S3 bucket:

aws s3 sync /path/to/local/directory s3://your-bucket-name/

Flexible backup and recovery for AWS EC2 instances with Vinchin solution

Vinchin Backup & Recovery supports AWS EC2 instance backups, addressing the need for secure and recoverable cloud-based data. Users can add EC2 instances using their AWS access key ID and configure full, incremental, or differential backups across different regions, with customizable schedules and encrypted storage.

In case of data loss or migration, Vinchin offers flexible recovery options, allowing restoration of entire instances or individual volumes, modification of instance parameters, and direct recovery to other virtualization platforms. Granular recovery of specific files or directories is also supported.

The solution integrates with Amazon S3 for secure, cost-effective backup data archiving. It also enables seamless V2V migrations, allowing EC2 backups to be restored to platforms like VMware, Hyper-V, and Proxmox. The user-friendly interface facilitates efficient batch selection and backup task configuration, streamlining cloud backup management.

To backup EC2 instance with Vinchin Backup & Recovery, follow these steps:

1. Select the EC2 instance to be backed up.

Backup EC2 instance

2. Select the backup destination.

Backup EC2 instance

3. Select the backup strategies.

Backup EC2 instance

4. Review and submit the job.

Start your 60-day free trial of Vinchin Backup & Recovery to experience its secure, resource-efficient backup solutions. Or, contact us for a customized plan tailored to your IT needs.

Connect S3 bucket from EC2 instance FAQs

1. Q: Can I use S3 buckets as a storage solution for my applications running on EC2?

A: Yes, S3 is commonly used for storing application data, static assets (like images, videos), backups, and more. It provides scalable and durable object storage accessible from anywhere.

2. Q: How do I troubleshoot connectivity issues between EC2 and S3?

A: Check IAM roles and policies to ensure correct permissions. Verify network configurations (e.g., security groups, VPC settings) to allow outbound connections from EC2 to S3. Use AWS CloudTrail logs and S3 access logs for debugging.

Conclusion

Connecting an S3 bucket from an EC2 instance is a straightforward process that enhances flexibility and scalability within AWS infrastructure. This seamless integration fosters a cohesive environment where EC2 instances can seamlessly interact with S3 storage, facilitating reliable data storage and retrieval for diverse application needs.

Share on:

Categories: VM Tips
Free Trial Now close-icon