DEV Community

Cover image for AWS EC2 for Beginners: How to Launch and Connect to Your First Instance
Mohd Ahsan
Mohd Ahsan

Posted on

AWS EC2 for Beginners: How to Launch and Connect to Your First Instance

An AWS EC2 instance is a virtual server that enables you to run applications and manage workloads on Amazon Web Services (AWS). EC2, or Elastic Compute Cloud, is a core AWS service offering scalable and resizable compute capacity in the cloud. EC2 instances provide flexible and scalable computing resources, making them an ideal solution for a diverse range of applications and workloads.

AWS EC2

Step-by-step guide to launching an EC2 instance and SSH into an EC2 instance. 🛠️



Step-1. Sign in to the AWS Management Console:

Step-2. Navigate to EC2 dashboard & Launch an Instance:

  • In the AWS Management Console, search for EC2 in the search bar and select it to go to the EC2 Dashboard.
  • On the EC2 Dashboard, click on the Launch Instance button. EC2 Launch

Step-3. Name & Tags:

  • Enter a name to identify your instance. For this example, name the instance MyServer (this helps in identifying our instance). EC2-Name

Step-4. Choose an Amazon Machine Image (AMI):

  • Select an AMI based on your needs (e.g., Amazon Linux 2, Ubuntu Server, Windows Server).
  • For this example, choose Ubuntu Server 24.04 LTS (Free Tier eligible). AMI

Step-5. Choose an Instance Type:

  • Select an instance type. For basic purposes, you can choose t2.micro or t3.micro (Free Tier eligible). These instance types provide a balance of compute, memory, and network resources. type

Step-6. Select or Create a Key Pair:

  • In the Key pair (login) section:
    • Choose Create a new key pair if you don’t have one. Provide a name, select the key pair type (usually RSA for SSH), and click Download Key Pair. This will download a .pem file. Keep this file safe, as it will be required to access your instance.
    • If you already have a key pair, select it from the list under Select an existing key pair. key-pair create

Step-7. Configure Network Settings:

  • Under Network settings, leave the default settings for now. You can adjust these based on your specific needs (e.g., VPC, Subnet, Auto-assign Public IP). Ensure that Auto-assign Public IP is enabled if you need to access your instance over the internet.
  • Create a new security group or select an existing one. A security group acts as a virtual firewall to control inbound and outbound traffic.
  • Add a rule to allow SSH access (port 22). Set the source to My IP to restrict access to your IP address only, which enhances security. network

Step-8. Configure Storage:

  • By default, an 8 GB (General Purpose SSD) root volume is allocated.
  • You can adjust the storage size or add additional volumes if necessary, depending on your use case. storage

Step-9. User Data (Optional):

  • Scroll down to the Advanced Details section on the "Configure Instance Details" page.
  • In the User Data field, enter the script you want to run when your instance launches.

    #!/bin/bash
    sudo apt update -y  # Updates all installed packages on Ubuntu
    sudo apt install apache2 -y  # Installs the Apache HTTP server
    systemctl start httpd  # Starts the Apache server
    systemctl enable httpd  # Enables Apache to start on boot
    echo "<h1>Welcome to your EC2 instance!</h1>" > /var/www/html/index.html  # Creates a basic web page
    

user-data

Step-10. Review and Launch:

  • Review all your instance settings. Make sure everything looks correct, including the AMI, instance type, key pair, and security group settings and Click Launch.
    review

  • Click View Instances to go to the Instances page, where you can see the status of your instance.
    instance

Step-11. Access Your EC2 Instance:

  • Locate the folder where you have downloaded the .pem file earlier.
  • Open a terminal (Git Bash on Windows, Terminal on macOS/Linux) to SSH into your instance.
  • Go back to your instances, and note down the Public IPv4 address (or Public IPv4 DNS) of your instance.
  • Use the SSH command to connect to your instance. Replace the placeholders with your actual key pair path and instance public IP or DNS:

    ssh -i /path/to/your-key-pair.pem ubuntu@your-public-dns
    

access

Default Usernames for Different AMIs:
- Amazon Linux: ec2-user
- Ubuntu: ubuntu
- RHEL: ec2-user or root
- CentOS: centos or ec2-user
- Debian: admin or root

Instance Management:

In AWS, you can manage the state of an EC2 instance by stopping or terminating it. However, if your workload is actively running on the instance, it is essential to back up your data before terminating it, as this action will permanently delete the instance and its associated data.

Below are examples of different instance states for an EC2 instance:

  1. Stopping an EC2 Instance: Halts the instance, saving its state and resources for later use without incurring additional instance costs.

  2. Rebooting an EC2 Instance: Restarts the instance without changing its state, useful for applying updates or troubleshooting.

  3. Terminating an EC2 Instance: Permanently deletes the instance and all associated data, releasing its resources back to AWS.

state



Additional Notes:

  • Make sure your instance's security group allows inbound SSH traffic from your IP.
  • Ensure that you’re connecting with the correct username associated with your AMI.
  • Run this command, if necessary, to ensure your key is not publicly viewable.

    chmod 400 /path/to/your-key-pair.pem
    



Follow these steps, and you should be able to successfully launch and SSH into your EC2 instance.

Top comments (0)