How to Create and Launch an EC2 Instance with IAM Role Attachment Using AWS Instance Connect
Published: 2024-06-27 17:24:56

## Introduction :
AWS EC2 (Elastic Compute Cloud) instances are virtual servers that provide scalable 
computing capacity in the cloud. In this use case , we will walk through the steps to create an 
EC2 instance, launch it using AWS Instance Connect, and attach an IAM role to the instance.

## Prerequisites:
1. An AWS account.
2. AWS CLI installed and configured on your local machine.
3. Necessary permissions to create EC2 instances and IAM roles.

## Step 1: Create an IAM Role
1.**Navigate to the IAM Console :** Open the AWS Management Console and navigate to the 
IAM service.
2.**Create a New Role :** Click on "Roles" in the left sidebar and then "Create role". Select 
"AWS service" as the trusted entity type and choose "EC2" for the service that will use this 
role. Click "Next: Permissions".
3.**Attach Policies :** Attach the necessary policies. For example, if you want your instance to 
access S3, attach the `AmazonS3ReadOnlyAccess` policy .Click "Next: Tags" (optional) and 
then "Next: Review".
4.**Name and Create the Role:** Provide a name for the role, such as `EC2S3ReadOnlyRole`, 
and click "Create role".

## Step 2: Create an EC2 Instance:
1.**Navigate to the EC2 Console:** Open the AWS Management Console and navigate to the 
EC2 service.
2.**Launch an Instance:** Click on "Instances" in the left sidebar and then "Launch Instances". 
3.**Configure Instance Details:** Choose an Amazon Machine Image (AMI). For this use case 
, we will use the Amazon Linux 2 AMI. And the instance type is ‘t2.micro’ Click "Next: 
Configure Instance Details".
4.**Attach IAM Role:** In the "IAM role" dropdown, select the role you created
earlier(‘EC2S3ReadOnlyRole’).Configure other settings as needed and click "Next: Add 
Storage".
5.**Add Storage:** Specify the storage size and type, then click "Next: Add Tags".
6.**Add Tags (optional)**:  Add tags to organize your instances. For example, add a tag with 
the key ‘Name’ and value ‘MyEC2Instance’. - Click "Next: Configure Security Group".
7.**Configure Security Group:** Create a new security group or select an existing one. Ensure 
SSH access is allowed from your IP address by adding a rule with the following details:
Type: SSH
 - Protocol: TCP
 - Port Range: 22
 - Source: My IP

8.**Review and Launch:** Review your instance configuration and click "Launch". Select an 
existing key pair or create a new one to connect to your instance and click "Launch 
Instances".

## Step 3: Connect to the EC2 Instance Using AWS Instance Connect
1.**Navigate to the EC2 Console:** Go to the EC2 service and click on "Instances".
2.**Select Your Instance:** Select the instance you just launched.
3.**Connect Using AWS Instance Connect:** Click on the "Connect" button at the top of the 
page. Select "EC2 Instance Connect" and click "Connect".
4.You are now connected to your EC2 instance using AWS Instance Connect!

## **Conclusion**
In this use case , we demonstrated how to create an EC2 instance, attach an IAM role, and 
connect to the instance using AWS Instance Connect. This process is essential for securely 
managing your AWS resources and leveraging the power of EC2 for scalable cloud 
computing