Overview
Lab Details- This lab walks you through the steps to create an AWS S3 bucket and demonstrates how to access the bucket using AWS CLI commands from EC2 instance and IAM roles.
- Duration: 30 minutes
- AWS Region: US East (N. Virginia) us-east-1
Introduction
IAM Policy:
- An IAM (Identity and access management) policy is an entity in AWS, that enables you to manage access to AWS services and resources in a secure fashion.
- Policies are stored on AWS in JSON format and are attached to resources as identity-based policies.
- You can attach an IAM policy to different entities such as an IAM group, user, or role.
- IAM policies gives us the power of restricting users or groups to only use the specific services that they need.
Policy Types:
There are two important types of policies:- Identity-Based-Policies
- Resource-Based-Policies
Identity-Based-Policy
- Identity-based policies are policies that you can attach to an AWS identity (such as a user, group of users, or role).
- These policies control what actions an entity can perform, which resources they can use, and the conditions in which they can use said resources.
- Identity-based policies are further classified as:
- AWS Managed Policies
- Custom Managed Policies
AWS Managed Policies
- AWS Managed policies are those policies that are created and managed by AWS itself.
- If you are new to IAM policies, you can start with AWS managed policies before managing your own.
Custom Managed Policies
- Custom managed policies are policies that are created and managed by you in your AWS account.
- Customer managed policies provide us with more precise control than AWS managed policies.
- You can create and edit an IAM policy in the visual editor or by creating the JSON policy document directly.
- You can create your own IAM policy using the following link: https://awspolicygen.s3.amazonaws.com/policygen.html
Resource-Based-Policy
- Resource-based policies are policies that we attach to a resource such as an Amazon S3 bucket.
- Resource-based policies grant the specified permission to perform specific actions on particular resources and define under what conditions these policies apply to them.
- Resource-based policies are in line with other policies.
- There are currently no AWS-managed resource-based policies.
- There is only one type of resource-based policy called a trust policy, which is attached to an IAM role.
- An IAM role is both an identity and a resource that supports resource-based policies.
IAM Role
- An IAM role is an AWS IAM identity (that we can create in our AWS account) that has specific permissions.
- It is similar to an IAM user, which determines what the identity can and cannot do in AWS.
- Instead of attaching a role to a particular user or group, it can be attached to anyone who needs it.
- The advantage of having a role is that we do not have standard long-term credentials such as a password or access keys associated with it.
- When resources assume a particular role, it provides us with temporary security credentials for our role session.
- We can use roles to access users, applications, or services that don’t have access to our AWS resources.
- We can attach one or more policies with roles, depending on our requirements.
- For example, we can create a role with s3 full access and attach it to an EC2 instance to access S3 buckets.
Simple storage service(S3)
- Amazon S3 is a simple storage service that we can use to store and retrieve any amount of data, at any time, from anywhere on the web.
- It gives developers and users access to highly scalable, reliable, fast, inexpensive data storage infrastructure.
- S3 guarantees 99.9% availability at any point in time.
- S3 has been designed to store up to 5 TB of data.
- S3 is global, meaning you can create a bucket in any region and access it from anywhere. Due to this, the name of the bucket should be a unique one.
- The S3 bucket objects, as well as the bucket, can be deleted at any time by the user.
- We can limit access to our bucket by granting different permissions for different users.
- S3 also comes with additional features such as versioning, static website hosting, server access logging and life cycle policy for storing objects, eand many others.
Architecture diagram

Task details
- Sign in to AWS Management Console
- Create an IAM Role and policy for EC2 Instance
- Launch an EC2 instance
- Viewing the S3 bucket
- Access the S3 bucket via EC2 instance.
- Validation of the lab
Launching the lab environment
- To launch the lab environment, Click on the Launch lab button.
- Please wait until the cloud environment is provisioned. It will take less than a minute to provision.
- Once the Lab is started, you will be provided with IAM user name, Password, Access Key, and Secret Access Key.
You can only start one lab at any given time
Lab guide
Lab steps
Task 1: Sign in to AWS Management Console
- Click on the Open console button, and you will get redirected to AWS Console in a new browser tab.
-
On the AWS sign-in page,
- Leave the Account ID as default. Never edit/remove the 12 digit Account ID present in the AWS Console. otherwise, you cannot proceed with the lab.
- Now copy your User Name and Password in the Lab Console to the IAM Username and Password in AWS Console and click on the Sign in button.
- Once Signed In to the AWS Management Console, Make the default AWS Region as US East (N. Virginia) us-east-1.
Task 2: Create an IAM Role and attach policy to it for EC2 Instance
In this task, we are going to create an IAM role and attach policy to the EC2 Instance.- Navigate to the Services menu at the top, then click on IAM in the Security, identity, & Compliance section.
- Navigate to Roles from the left navigation menu.
- Click on Create Role button.

- In Select trusted entity: choose AWS Service

- Under the use case click on the drop-down button to search the Ec2 and then Choose Allows Ec2 instances to call AWS services on your behalf and then click on Next.

- In the Permissions policies section click the search bar type AmazonS3ReadOnlyAccess, select the checkbox next to the policy name, and then click the Next button

- Under Review: Enter EC2Role in the Role Name.
- Click on Create role button.
Task 3: Launching EC2 Instance
In this task, we are going to create an EC2 Instance by providing the required configurations like name, key pair, instance type, security groups and role access the AWS S3 service.- Make sure you are in the US East (N. Virginia) us-east-1 Region.
- Navigate to Services menu in the top, then click on EC2 in the Compute section.
- Click on Instances from the left side bar and then click on Launch instances button.
- Name : Enter S3EC2server

- For Amazon Machine Image (AMI): Choose Amazon Linux 2023 kernel-6.1 AMI.

- For Instance Type: select t2.micro

- For Key pair: Select Create a new key pair Button
- Key pair name: WhizKey
- Key pair type: RSA
- Private key file format: .pem
- Select Create key pair Button.

- In Network Settings Click on Edit button:
- Auto-assign public IP: Enable
- Select Create new Security group
- Security group name : Enter S3server-SG
- Description : Enter Security Group to allow traffic to EC2
-
To add SSH
- Choose Type: Select SSH
- Source: Select Anywhere
- Click on Advanced details.
- In IAM instance Profile : Select EC2Role which we have created in Task 2.
- Keep Rest thing Default and Click on Launch Instance Button.
- Select View all Instances to View Instance you Created
- Launch Status: Your instances are now launching, Navigate to Instances page from left menu and wait the status of the EC2 Instance changes to running.
- You can tell that the instance is running by checking the instance status (example below).

- Select the instance and copy the Public IPv4 address and save it in any text editor.
Task 4: Viewing the S3 Bucket
In this task, we are going to verify the bucket already pre-created for you.- Navigate to the Services menu at the top. Click on S3 in the Storage section.
- You can see a bucket with a name similar to whizlabs<RANDOM-NUMBERS>.

Task 5: Accessing the S3 bucket via EC2 Instance
In this task, we are going to access the S3 bucket via the EC2 instance and will upload the files to S3 via EC2 shell.- To SSH into the server, please follow the steps in SSH into EC2 Instance.
-
Once logged in, switch to the root user:
-
Run the below command to find your S3 bucket via CLI.
-
You will see output similar to the image below, which shows that we are able to access the S3 bucket with the help of role attached to the EC2 instance.

-
Create a new text file and upload it to the bucket via AWS CLI (using the following set of commands):
You need to enter your bucket name.
- Check for the new file in the S3 bucket.

- Repeat step 5 and create some more files like new.txt, smile.txt and upload it to the S3 bucket using below commands:
You need to enter your bucket name.
- You can confirm the files uploaded to S3 bucket by navigating to the bucket in the AWS console.

-
You can also list the files uploaded to S3 bucket via CLI from the EC2 instance with the following command:
You need to enter your bucket name.

Task 6: Validation Test
- Once the lab steps are completed, please click on the Check my work button on the right side panel.
- This will validate the resources in the AWS account and displays whether you have completed this lab successfully or not.
- Sample output :

Completion and conclusion
- You have successfully created an IAM role to access S3 by granting S3 full access.
- You have created an EC2 instance with an IAM role attached.
- You have uploaded a file to an S3 bucket via CLI from the EC2 instance.
- You have uploaded a file to an S3 bucket from the AWS console.
End lab
- Sign out of AWS Account.
- You have successfully completed the lab.
- Once you have completed the steps, click on End lab in the IP Lab Portal.
What gets checked
When you press Check my work, the platform verifies each of these:- Check IAM Role Attached to EC2 — Check whether an IAM role is attached to the EC2 instance.
- Create an AWS S3 Bucket with Policy — Check whether S3 bucket is created and bucket policy added or not
- check s3 object — Check whether an object is uploaded to the S3 bucket.
- Launch an EC2 Instance — Check whether an EC2 Instance is launched or not.