AWS EC2: Creating, Connecting and Managing Your Instances
Tags: aws, cloud, ec2, devops
Published: 2024-07-09 20:45:22

If you have read the last article, you must have some idea about an EC2 instance. Amazon EC2 instances are basically virtual machines but in the cloud. This guide will walk you through creating your EC2 instance, securely connecting to it via SSH, and managing it effortlessly using the AWS Command Line Interface (CLI). But before that here are some features of EC2:

### Features of AWS EC2

* **Flexibility**: Choose from a wide range of instance types with varying CPU, memory, storage, and networking capabilities.
    
* **Scalability**: Easily scale up or down based on demand, ensuring optimal performance and cost efficiency.
    
* **Security**: Control access with security groups and manage authentication using key pairs for secure SSH access.
    
* **Integration**: Seamlessly integrates with other AWS services like S3, RDS, and VPC for comprehensive cloud solutions.
    
* **Cost Management**: Pay only for the resources you use with flexible pricing options and cost-effective billing.
    

AWS offers 12 months of free tier access to every new user. So if you want to experiment with it, go ahead, but be aware about the billing cycles. Amazon EC2 empowers businesses and developers to deploy applications quickly and efficiently, making it a cornerstone of cloud computing infrastructure. Now lets get started.

### Creating Your EC2 Instance

#### Step 1: Select an AMI

Think of the Amazon Machine Image (AMI) as your instance’s operating system. Here’s how to get started:

* **Visit the EC2 Dashboard**: Log in to AWS and go to the EC2 Dashboard.
    
* **Create Your Instance**: Click on "Launch Instance" and choose an AMI. Popular choices include Amazon Linux and Ubuntu.
    

#### Step 2: Select the Instance Type

Choose the instance type that best fits your needs. For example, `t2.micro` is great for testing and small applications. Be careful here and only select the ones with free tier eligible tags or you will be charged for it.

* **Select Instance Type**: Pick the hardware configuration that matches your workload.
    

#### Step 3: Create Key Pair

Create a key pair that will be used to connect to the EC2 instance. So save it somewhere safe.

* **Generate Keypair and save it**: This will be used to securely connect to your EC2 instance.
    

#### Step 4: Configure Networking and Add Storage

Set up networking, storage, and any additional configurations. If you are just getting started, you can keep it as default and move on.

* **Configure Instance Details**: Decide on networking settings and add storage as needed.
    

#### Step 5: Launch Your Instance

Review your configuration and launch your instance. Don’t forget to create or select a key pair for SSH access.


![AWS EC2 Instance Running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaf5ujxf7fgieoxjafxf.jpeg)

### Connecting to Your EC2 Instance via SSH

Now that your instance is up and running, it’s time to connect to it securely:

1. **Prepare Your Key Pair**: Have your `.pem` key pair file ready.
    
     Modify permissions in order to execute 

       ```chmod 600 keypair.pem```
       
        
3. **Connect via SSH**:
    
    Find your instance’s Public DNS (IPv4) in the EC2 Dashboard.
        
    Use SSH to connect.
     
        ssh -i /path/to/key-pair.pem ubuntu@<Public-IP-Address>
        

![Connecting to EC2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f0tz9t09lbbr06l6tzq.png)


![Ubuntu Neofetch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb27bx42shs4kh0uby7o.png)

### Managing EC2 Instances with AWS CLI

Harness the power of AWS CLI to manage your EC2 instances efficiently:

1. **Install and Configure AWS CLI**: If not already installed, set up AWS CLI with your AWS credentials.
    
2. **Launch an Instance**: Use a single command to launch an instance:
    
    ```bash
    aws ec2 run-instances \
        --image-id ami-0a0e5d9c7acc336f1 \
        --count 1 \
        --instance-type t2.micro \
        --key-name MyKeyPairName \
        --security-group-ids sg-0a49d3aa3grs60 \
        --subnet-id subnet-05617gfhu90a934c1e
    ```
    
   
![AWS EC2 CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eoz4sh3bqjzl1mkuhp8n.jpeg)
    
3. **Check Instance Status**:
    
    ```bash
    aws ec2 describe-instances --instance-ids i-009ee048cd14619de
    ```
    
4. **Terminate an Instance**:
    
    ```bash
    aws ec2 terminate-instances --instance-ids i-009ee048cd14619de
    ```
    

### Conclusion

So with that we now have basic understanding of how to create an EC2 instance both through UI and through CLI, connecting to EC2 instances and managing the instances. This Knowledge will be helpful whether you are on a DevOps track or trying to learn about AWS in general. I will share more about AWS as I learn more about it. See you in the next one.