How to access s3 from ec2 using IAM role

This tutorial will explain How to access S3 from ec2 using IAM role.

 

IAM Role

An IAM role is an Identity and access management entity that defines a set of permissions for making AWS service requests. It does not have any credentials and cannot make direct requests to AWS services.

IAM roles are meant to be assumed by authorized entities, such as IAM users, applications, or an AWS service such as EC2.

 

Create IAM role to access S3 bucket

 

Step 1- Create Identity and access management Role

Log into AWS account and navigate to Services—-> IAM under Security, Identity, & Compliance——> Roles

Click on Create Role

 

 

Click on EC2 under Common Use case . If it is not there then Click on EC2 under “Or select a service to view its use cases” and Select EC2  under Select Your Use case.

Then Click on Next: Permission

 

 

Attach Permissions Policies

You can select an existing predefined IAM policy such as AmazonS3FullAccess/AmazonS3ReadOnlyAccess or you can create a custom IAM policy and attach here.

 

Add Tags(Optional)

Add Tag for your role.

 

 

 

Enter Role Name and Role Description and Click on Create Role

 

Attach iam role to ec2 instance

 

Step 2 – Attach Identity and access management role to ec2 instance

Navigate to Services —-> EC2——-> INSTANCES-———->Instances

Select the Instance(Test_Server) —————-> Click on Actions ——-> Instance Settings ————> Attach/Replace IAM Role

 

Search for the  IAM Role that you created and Click on Apply

Access S3 Bucket from EC2 Instance

 

Step 3 – Access S3 Bucket from EC2 Instance

Now connect your Windows/Linux EC2 Instance and access your S3 Bucket directly.

 

For Linux Instance ( Amazon Linux)

 

Install AWS CLI and connect s3 bucket

$ sudo apt-get install awscli -y

After aws cli is installed , you can directly access S3 bucket with  attached Identity and access management role. You don’t need to do AWS configure.

 $ aws s3 ls bucketname

 $ aws s3 cp filename.txt s3://bucketname/

 

For Windows Instance

 

Download and Install AWS CLI for windows from the link – AWS CLI for Windows

 

Once Installed , Open CMD with Run as Administrator

By default AWS CLI is installed on c:Program FilesAmazon|AWSCLIV2 for 64 bit and c:Program Files (x86) Amazon|AWSCLIV2 for 32 bit Operating System.

I am assuming your O/S is 64 bit , hence run the following command.

 

c:/> cd c:Program FilesAmazon|AWSCLIV2

c:Program FilesAmazon|AWSCLIV2> awscli – – version

c:Program FilesAmazon|AWSCLIV2> aws s3 ls bucketname

c:Program FilesAmazon|AWSCLIV2> aws s3 cp filename.txt s3://bucketname/

 

Click to tweet this tip !

 

Thanks for reading this tutorial about How to access s3 from ec2 using Identity and access management role . If you think this article is really helpful, please do support us to  share this article with others as well directly or via Social media.

Please also share your valuable feedback, comment, or any query in the comment box.I will really happy to resolve your queries.

Thank You

 

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

Leave a Reply

Your email address will not be published. Required fields are marked *