Launch and Connect to AWS EC2 Instance
Tags: aws, cloud, cloudcomputing, iaas
Published: 2024-05-27 15:38:53

![Amazon EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6yvgonjn9u8ohb89kjg6.png)

### Introduction
Launching and connecting to an AWS EC2 instance is a fundamental task for any cloud-based development or application hosting. This guide will walk you through the steps to create and connect to an EC2 instance on AWS, and then verify the connection by running some basic commands.

### Prerequisites
Before starting, ensure you have an [AWS](https://aws.amazon.com/) account. ☁️

### Step 1: Launch an EC2 Instance
1. **Log in to your AWS Management Console**. 🔑

2. **Navigate to the EC2 Dashboard** and click on "Launch Instance". 🚀
![Launch Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w5ji07vcoya21ovsk1u.png)

3. **Configure the instance settings as follows** (leave other settings as default):
    - **Enter the name of the EC2 Instance** (e.g., "My EC2 Instance"). 🖥️
    ![Enter the name of the EC2 Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b83w1fj9xulbyd2a5xnf.png)
    - **Select `Ubuntu` as the Amazon Machine Image (AMI)**. 🐧
    ![Select Ubuntu as the AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/620fayke5vt5wxf21keq.png)
    - **Create a key pair to access the instance from the command line**. 🔑
    ![Create a key pair button click](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8kew1qlb4zgoyyysqh3l.png)
    - **Enter an appropriate name for the key pair**. 📝
    ![Key pair configurations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztc5pg16735f03rnrurr.png)
This will save the key pair file (named `ec2-instance.pem` in this case) to your local machine. 💾
  - **Configure the Security Group** - add a rule to allow HTTP traffic (port 80). 🔒
    ![Allow HTTP traffic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0uur1a63zqd8m8klxlgs.png)

4. **Review your settings and launch the instance**. ✅
![EC2 Instance summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9gpcrfjxdivdkij3gxwt.png)

### Step 2: Connect to Your EC2 Instance
After the instance is successfully running:

1. **Click on the `Instance ID` of the EC2 instance you just created**. 🆔
![EC2 Dashboard Running Instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m5u567v5ckm3hhe24jdf.png) ![Running Instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/huxdtz6twcjtgtax2twy.png)

2. **On the Instance summary page, click on the `Connect` button**. 🔌
![EC2 Instance summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v20j8jdfevcpjva9sce.png)

3. **Copy the command to connect to the EC2 instance**. 📋
![Connect to the EC2 Instance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkwr1bmq8765silbcc2b.png)

4. **Open a new local terminal from where the private key was saved**. 🖥️

5. **Run this command to ensure your key is not publicly viewable**:
    ```bash
    sudo chmod 400 "ec2-instance.pem"
    ```

6. **Paste the command copied from Step 3 to connect to your instance**:
    ```bash
    sudo ssh -i "ec2-instance.pem" ubuntu@ec2-18-208-219-229.compute-1.amazonaws.com
    ```

### Step 3: Verify Your EC2 Instance
After connecting to the EC2 instance, you can use commands as you would on your local machine. Here are some common commands to verify and manage your instance:

1. **Update the package list**:
    ```bash
    sudo apt update
    ```

1. **Upgrade the installed packages**:
    ```bash
    sudo apt upgrade -y
    ```

### Conclusion
You've successfully launched, connected to, and verified your AWS EC2 instance. This setup can now serve as the foundation for your web applications or development environment. 🌐

### Additional Resources
- [AWS EC2 Documentation](https://docs.aws.amazon.com/ec2/) 📖

If you have any questions or run into issues, feel free to leave a comment below.

Happy cloud computing! ☁️👩‍💻👨‍💻