Creating an EC2 Instance and Connecting via SSH with IAM User
Tags: aws, cloud, terraform
Published: 2024-06-28 01:45:41

**Introduction**

Creating and managing cloud infrastructure can be streamlined with AWS and Terraform. This blog post will guide you through the process of creating an EC2 instance and an IAM user using the AWS Management Console, and then connecting to the instance via SSH.

**Prerequisites**

Before we begin, ensure you have the following:

- An AWS account.
- Basic understanding of AWS services and SSH.

**Step 1:** **Creating an EC2 Instance in AWS Management Console**

1. Log in to the AWS Management Console.
2. Navigate to the EC2 Dashboard:
      Click on "Services" in the top menu and select "EC2" under the  
      "Compute" section.
3. Launch an Instance:
      Click the "Launch Instance" button.
4. Select an Amazon Machine Image (AMI) of your choice.
5. Choose an instance type (e.g., t2.micro for free tier eligibility).
6. Configure the instance details as needed.
6. Add storage as required.
7. Add tags to help identify your instance.
8. Configure the security group to allow SSH access (port 22).
9. Review and launch the instance.
10. Download the Key Pair:
       When prompted, create a new key pair or use an existing one.
       Download the key pair file (e.g., my-key-pair.pem) and save it              
       securely.

**Step 2:**** Creating an IAM User in AWS Management Console**

1. Navigate to the IAM Dashboard:
      Click on "Services" in the top menu and select "IAM" under the 
      "Security, Identity, & Compliance" section.
2. Create a New User:
      Click the "Add user" button.
      Enter a username (e.g., example-user).
      Select the "Programmatic access" checkbox to provide access via the 
       AWS CLI, SDKs, etc.
3. Set Permissions:
      Attach existing policies directly or create a new policy to grant 
      necessary permissions.
4. Review and Create User:
      Review the settings and click "Create user."
      Download the access key ID and secret access key, as you will not 
       be able to view them again.

**Step 3:** **Connecting to the EC2 Instance via SSH**

1. Locate the Public IP Address:
      In the EC2 Dashboard, select your instance.
      Note the public IPv4 address from the instance details.
2. Set Permissions on the Key Pair File:  
      Open your terminal and navigate to the directory containing the key 
      pair file.
3. Run the following command to set the correct permissions:

```
chmod 400 my-key-pair.pem
```

Connect to the Instance:

Use the following SSH command to connect to your instance:

```
ssh -i "my-key-pair.pem" ec2-user@<your_instance_public_ip>
```

Replace <your_instance_public_ip> with the public IP address of your instance.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e8wwd3tm1sxstbfvy94x.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swz0bvhpscjtm4u03low.png)

**Conclusion**

You've successfully created an EC2 instance and an IAM user using the AWS Management Console and connected to the instance via SSH. This process highlights the basic steps to get started with AWS EC2 and IAM, laying the groundwork for more complex configurations and automations.