How do I link my EC2 instance to access files from my S3 bucket?
Loading
How do I link my EC2 instance to access files from my S3 bucket?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sarthak VarshneyPosted May 27, 2025, 5:05 AM
Step 1: Create an IAM Role with S3 Access
Go to the IAM Console:
https://console.aws.amazon.com/iam/
Create a new role:
Choose "AWS service" as the trusted entity.
Use case: EC2.
Click Next.
Attach a policy:
Choose AmazonS3ReadOnlyAccess (or create a custom policy if you need more permissions like upload/delete).
Click Next, then Create role.
Give your role a name, e.g.,
EC2S3AccessRole.Step 2: Attach the IAM Role to Your EC2 Instance
Go to the EC2 Dashboard: https://console.aws.amazon.com/ec2/
Select your instance.
Choose Actions > Security > Modify IAM Role.
Select the role you just created (
EC2S3AccessRole) and click Update IAM Role.Step 3: Verify IAM Role is Attached
SSH into your EC2 instance and run:
It should return the IAM role name. This confirms it's properly attached.
Step 4: Install AWS CLI on EC2 (if not installed)
For Amazon Linux 2:
For Ubuntu:
Step 5: Use AWS CLI to Access S3 Bucket
Run commands like:
No need to configure
aws configureif the IAM role is attached. The instance will automatically use the role credentials.Optional: Create a Custom IAM Policy
If you want more control than
AmazonS3ReadOnlyAccess, here’s a sample policy to allow full access to a specific bucket:Your EC2 instance can now read from (or write to) your S3 bucket securely using the IAM role — no access keys required.