Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
35 views
in Information Technology by (150k points)
Can you describe how to set up an IAM role for an EC2 instance to access an S3 bucket?

Please log in or register to answer this question.

1 Answer

+1 vote
by (150k points)

To set up an IAM role for an EC2 instance to access an S3 bucket, you need to create an IAM role with the necessary permissions and then associate that role with the EC2 instance.

  1. Create the IAM Role:

    • Go to the IAM console.
    • Choose "Roles" and then "Create role."
    • Choose "AWS service" and then "EC2" as the trusted entity.
    • Attach the policy with S3 access, e.g., AmazonS3ReadOnlyAccess.
  2. Attach the IAM Role to the EC2 Instance:

    • Go to the EC2 console.
    • Select the instance and choose "Actions" > "Security" > "Modify IAM Role."
    • Select the newly created IAM role and apply it.

Example IAM Policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::example-bucket/*"
        }
    ]
}

Example Code to Access S3 from EC2 Instance:

import boto3

s3 = boto3.client('s3')
response = s3.list_objects_v2(Bucket='example-bucket')

for obj in response['Contents']:
    print(obj['Key'])

Related questions

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...