Access S3 buckets from EC2 instances
We’ll follow few steps to access S3 bucket from EC2 instance with the help of ROLE that grants access to the bucket. For this we’ll use IAM to create the role.
Quick steps below:
a) Create the Role with “s3fullaccess”
b) Launch Instance and attach the created role with Instance.
c) Use awsCLI to access S3 bucket from Instance.
1. Create the Role with “s3fullaccess”:
Select the IAM service after you have logged into your AWS console. Now create the ROLE, As we know, we will use this role for EC2 instance, so you must choose EC2 instance and this role should have AmazonS3FullAccess. You can see the following Screenshot (a) for better understanding.
Now attach the permissions policy, I have selected the “AmazonS3FullAccess”. You can choose “AmazonS3ReadOnlyAccess” according to your requirement and then click NEXT. Check following ScreenShot.
We have attached the policy, the next step is to set the Role Name. Choose only relevant name for the IAM ROLE so that in future it would easy to remember. As I’ve set “S3FullAccess” Role name. Now we’ve completed the first step. Now we’ll jump to the next step.
2. Launch Instance and attach the created role with Instance:
You can see in below screenshot, I have marked three different fields with red outline that we need to choose or configure.
First of all choose the EC2 instance, I’m going to select “AMI” machine that comes with an AWS CLI by default. Now click on the “Select” button. It’ll push you on next tab “Choose Instance Type”, Here you need to choose Instance Type. I selected “Free tier eligible” and click on next.
On the third tab (Configure Instance) we need to configure the IAM ROLE see marked spot on screenshot and I selected “S3FullAccess” as we have already created this IAM ROLE on first step. Rest all setting remain same therefore click on “next” for the rest of the tabs and launch EC2 machine. Download the pair key on your local machine. Now you can connect your machine with PUTTY if you are using window and If you have linux based machine then you can connect with SSH protocol.
3. Use awsCLI to access S3 bucket from Instance:
Now login to your AMI EC2 machine with the help of PUTTY or SSH protocol to access the S3 buckets, I have pasted the screenshot (e) where I have created the two buckets.
I used following commands for this:
~]# aws s3 mb s3://webnersolutions ~]# aws s3 mb s3://webner ~]# aws s3 ls
I have used the aws s3 mb command to create a new bucket. To list all buckets or their contents, use the aws s3 ls command, check my third command above.
If you want to remove a bucket, use the aws s3 rb command very carefully. For example,
~]# aws s3 rb s3://webnersolutions
Now login to AWS console again and click on S3 service, You will see your created buckets there.
This is all about how we can access the S3 bucket from EC2 instance with IAM ROLE. We don’t need any AWS access key ID and AWS secret access key for this.
[…] usually need to add some images to it. Suppose we have a Laravel project where we are showing some images from s3 or somewhere […]