# How to Create an EC2 Instance

### Table of Contents

1. What is EC2?
    
2. Key features of EC2
    
3. Hands-on Lab Overview
    
4. Hands-on Lab
    
    * Create an EC2 with AWS Management Console;
        
    * Create an EC2 with AWS CLI;
        
5. References
    

### What is EC2

***Amazon Elastic Compute Cloud (EC2)*** is a web service provided by Amazon Web Services (AWS) that allows users to rent virtual servers, known as instances, in the cloud. EC2 provides resizable computing capacity, enabling users to scale their computing resources up or down as needed.

With EC2, users can quickly deploy virtual instances of various configurations, including different operating systems, CPU, memory, storage, and network capacity. EC2 instances are highly flexible and can be customized to meet specific requirements, making them suitable for a wide range of applications and workloads.

### Key features of EC2

1. ***Scalability***: EC2 allows users to easily scale their infrastructure up or down based on demand. Instances can be launched or terminated on demand, and additional instances can be added to handle increased traffic or workload.
    
2. ***Flexibility***: Users have a wide selection of instance types to choose from, each optimized for different use cases such as general-purpose computing, memory-intensive applications, or graphics-intensive workloads.
    
3. ***Security***: EC2 provides various security features, including secure access control mechanisms, virtual private cloud (VPC) integration, and the ability to configure firewalls and security groups to control network access.
    
4. ***Availability and reliability***: EC2 instances are hosted in multiple availability zones within AWS regions, offering high availability and fault tolerance. Users can design their applications to distribute instances across multiple availability zones for increased resilience.
    
5. ***Pay-as-you-go pricing***: EC2 follows a pay-as-you-go pricing model, where users pay only for the computing capacity they consume. This makes it cost-effective, as users can scale their resources according to their needs, avoiding upfront infrastructure costs.
    

### Hands-on Lab Overview

In this hands-on, we will create an EC2 instance with a *t2.micro* instance type using 2 approaches: AWS Management Console and AWS CLI.

### Hands-on Lab

* **Create an EC2 instance with AWS Management Console.**
    
    1. Sign in to the AWS Management Console ([https://console.aws.amazon.com/](https://console.aws.amazon.com/)).
        
    2. Open the EC2 service by searching for "EC2" in the search bar or navigating to the "Compute" section.
        
    3. In the EC2 Dashboard, click on the "Launch Instance" button to start the instance creation process.
        
        ![](https://i.imgur.com/ZQC18jV.png align="center")
        
    4. Choose a name and an Amazon Machine Image (AMI) for your instance. An AMI is a pre-configured template that contains the necessary operating system and software. You can select an AMI based on your preferred operating system and requirements.
        
        ![](https://i.imgur.com/NHDgsIe.png align="center")
        
    5. Select the *t2.micro* instance type. In the "Choose Instance Type" section, search for "t2.micro" and click on the "Select" button next to it.
        
    6. Create a key pair. A key pair is used for secure remote access to your instance. You can either select an existing key pair or create a new one. If you create a new key pair, make sure to download and securely store the private key file (.pem) as it will be needed to connect to the instance later.
        
        ![](https://i.imgur.com/KoSovu9.png align="center")
        
    7. Configure network settings and security groups. Security groups act as virtual firewalls that control inbound and outbound traffic for your instance. You can create a new security group or select an existing one. Make sure to allow SSH (port 22) and HTTP (port 80) access if you intend to connect to the instance via SSH and HTTP.
        
        ![](https://i.imgur.com/DgZQqkr.png align="center")
        
    8. Add storage if needed. By default, a t2.micro instance comes with a default EBS (Elastic Block Store) volume. You can specify the size and type of storage based on your requirements.
        
        ![](https://i.imgur.com/qj9TrDV.png align="center")
        
    9. Click on "Advanced Details". Add additional user data scripts. This script is going to be executed when the instance first starts and only once in the whole lifecycle of the instance.
        
        ![](https://i.imgur.com/Jx3NVdA.png align="center")
        
    10. Click on the "Launch" button to create the instance.
        
* **Create an EC2 with AWS CLI**
    

To create an EC2 instance with AWS CLI, get the following credentials from the AWS Management console:

* VPC ID;
    
* Subnet ID;
    
* AMI ID;
    

1. **VPC ID** which is required to create a security group.
    
    Go to the EC2 dashboard, Account attributes, and click on the Default VPC. In my case, the VPC ID is: `vpc-0385759f81fda61b2`
    
    ![](https://i.imgur.com/8WZ0vp6.png align="center")
    
2. **One** **Subnet ID** that is required to launch EC2 instance\*\*:\*\*
    
    Inside the VPC Management Console, click on the Subnets and insert your VPC ID in the search bar to get a list of all the subnets associated with that VPC.
    
    ![](https://i.imgur.com/rKqJdX1.png align="center")
    
    I’m going to use the following subnet ID for this tutorial: `subnet-04dc67df9c06478d5`.
    
    ![](https://i.imgur.com/4dnSyao.png align="center")
    
3. Next, get the **AMI ID** to be used with EC2 CLI.
    
    AWS AMI ID (Amazon Machine Image ID) is a unique identifier for a pre-configured virtual server image that serves as a template for launching EC2 instances. It contains the information required to start an instance, such as the operating system, application software, and other configurations.
    
    To get the AMI Id, Go to `EC2 Dashboard --> AMI Catalog` and choose the base image you need from AWS along with the AMI Id as shown below.
    
    I am going to use the AWS Ubuntu AMI ID `ami-053b0d53c279acc90`
    
    ![](https://i.imgur.com/2DJjvVH.png align="center")
    
4. Next, we *create a security group*. For that, we need a security group ID to be attached to the EC2 instance. Create the security group id using the following command with your VPC ID `vpc-0385759f81fda61b2:`
    
    ```plaintext
    aws ec2 create-security-group \
    --group-name "demo-sg" \
    --description "AWS EC2 CLI Demo SG" \
    --vpc-id "vpc-0385759f81fda61b2"
    ```
    
    Note down the **security group ID from the output**. The output would look like the following.
    
    ![](https://i.imgur.com/X2evgF6.png align="center")
    
    Then, **add inbound (ingress) firewall rules to the security group**. Replace `sg-00573a4cc8c688d61` with your security group ID.
    
    ```plaintext
    aws ec2 authorize-security-group-ingress \
    --group-id "sg-00573a4cc8c688d61" \
    --protocol tcp \
    --port 22 \
    --cidr "0.0.0.0/0"
    ```
    
    ![](https://i.imgur.com/nk1aFLY.png align="center")
    
5. Next, create an **SSH key pair**.
    
    *Key pairs* are required to authenticate when you connect to the EC2 instance.
    
    Use the following command to create a key pair:
    
    ```plaintext
    aws ec2 create-key-pair \
    --key-name demo-key \
    --query "KeyMaterial" --output text > ~/.ssh/demo-key
    ```
    
6. Finally, create an EC2 instance.
    
    We have the following pre-defined values to use with the following AWS CLI command:
    
    * **VPC-ID:** vpc-0385759f81fda61b2
        
    * **Subnet-ID:** subnet-04dc67df9c06478d5
        
    * **AMI-ID:** ami-053b0d53c279acc90
        
    * **Security Group ID:** sg-00573a4cc8c688d61
        
    * **Key name:** demo-key
        
    
    ```plaintext
    aws ec2 run-instances \
    --image-id ami-053b0d53c279acc90 \ 
    --count 1 \
    --instance-type t2.micro \
    --key-name demo-key \
    --security-group-ids sg-00573a4cc8c688d61 \
    --subnet-id subnet-04dc67df9c06478d5 \
    --block-device-mappings "[{\"DeviceName\":\"/dev/sdf\",\"Ebs\":
    {\"VolumeSize\":30,\"DeleteOnTermination\":false}}]" \
    --user-data file://path/to/script/user-data.sh
    ```
    
    The output:
    
    ![](https://i.imgur.com/5qWFRAd.png align="center")
    

### References

1. [What is Amazon EC2?](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts.html#ec2-features)
    
2. [Amazon EC2 AMI Locator](https://cloud-images.ubuntu.com/locator/ec2/)
    
3. [AWS run-instances](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/run-instances.html)
    
4. [Create, display, and delete Amazon EC2 key pairs](https://docs.aws.amazon.com/cli/latest/userguide/cli-services-ec2-keypairs.html)
    
5. [How to Use AWS CLI to Create an EC2 Instance](https://devopscube.com/use-aws-cli-create-ec2-instance/)
