Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sarthak VarshneyRank #46 · 32K reputationPosted May 27, 2025, 1:05 AM EDT
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:
sudo yum update -y sudo yum install aws-cli -yFor Ubuntu:
sudo apt update sudo apt install awscli -yStep 5: Use AWS CLI to Access S3 Bucket
Run commands like:
aws s3 ls s3://your-bucket-name/ aws s3 cp s3://your-bucket-name/filename.txt . aws s3 sync s3://your-bucket-name/ /home/ec2-user/s3-files/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:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "s3:*", "Resource": [ "arn:aws:s3:::your-bucket-name", "arn:aws:s3:::your-bucket-name/*" ] } ] }Your EC2 instance can now read from (or write to) your S3 bucket securely using the IAM role — no access keys required.