Home » How to Create EC2 Instances and Access

How to Create EC2 Instances and Access

Banner 1

Introduction

Launching an Amazon EC2 instance is a foundational task for cloud engineers and system administrators using default wizard settings without proper configuration can expose virtual machines to security vulnerabilities or unnecessary costs.

In this step-by-step tutorial, we will demonstrate how to provision an Amazon Linux 2023 EC2 instance, set up SSH security rules, and connect remotely from Linux, macOS, or Windows environments..

What is Elastic Compute EC2?

Amazon Elastic Compute Cloud (Amazon EC2) is a scalable web service that provides secure, resizable compute capacity in the cloud.

EC2 instances allow you to:

  • Customize Compute Resources: Choose specific CPU, memory, and storage profiles suited to your workload.
  • Support Multiple Operating Systems: Run Linux distributions (Amazon Linux, Ubuntu, Red Hat) or Windows Server.
  • Scale Dynamically: Increase or decrease capacity on-demand with pay-as-you-go pricing.

Prerequisites

  • An active AWS Management Console account.
  • A terminal client (Terminal for macOS/Linux or Command Prompt/PuTTY/PowerShell for Windows).

Step-by-Step Implementation Guide

Step 1: Launch EC2 Instances

  • Go to AWS Console Management account
    • To setup EC2 instances, you will need an AWS account. If you don’t have one, go to AWS official website and sign up.
    • Once you have an account
    • Navigate to the AWS Management Console.
    • Log in using your AWS credentials.
  • Launch an EC2 Instance
    • Go to EC2 Dashboard
    • From the AWS Management Console, find and click on EC2 under the “Compute” section to open the EC2 Dashboard.
    • Click the Launch Instance button to start the process of create EC2 instance.
    • Name your server: My-Demo-Server
  • Select an Amazon Machine Image (AMI)
    • Amazon provides several pre-configured AMIs that you can choose from based on your use case.
    • For example, you can select a Ubuntu Server if you need a Linux-based system or Windows Server for a Windows-based system. In this demo session I selected Amazon Linux.
    • Click Select on the AMI you prefer
  • Choose an Instance Type
    • Here, you select the hardware configuration for your EC2 instance (e.g., CPU, RAM).
    • AWS offers various instance types such as t2.micro, t3.medium, etc. For general usage, you can select the t2.micro instance, which is eligible for the free tier if you are a new user.
  • Key Pair
    • You will be prompted to select a Key Pair for SSH access (we will cover this in the next step).
    • Choose “Create a new key pair,” name it, and download the private key file (.pem). Keep this file safe, as you’ll need it to access your instance.

Note: Save your .pem file in a secure local directory. AWS never stores private keys and cannot re-issue lost key files. If lost, you will lose SSH access to your instance.

  • Network Setting
    • In this section, you can configure additional settings like the network settings, and security groups. For now, the default settings should work well.
  • Configure Security Group
    • This step is important as it involves configuring the firewall for your EC2 instance.
    • Create a new security group or select an existing one. A typical security group for web applications would allow SSH (port 22) for Linux instances or RDP (port 3389) for Windows instances and Http (80) and TPS (443) for internet access and select source type anywhere.
    • Make sure to allow inbound SSH access if you want to connect to your instance via terminal.
  • Add Storage
    • By default, a root volume (EBS) is created. You can increase the size if needed, but the default size is typically sufficient for most users.
  • Add Tags (Optional)
    • Tags help you identify and organize your resources in AWS.
    • You can add a tag like Name: MyEC2Instance for easier management.
  • Review and Launch
    • Review all your selections and click Launch.

Step 2: Accessing EC2 Instance

  • For Linux Instances (via SSH)
    • Open your terminal (Linux/macOS) or use an SSH client (Windows).
    • Change Permissions of the Key Pair (for Linux/macOS users)Run the following command to set the correct permissions for the downloaded .pem file
Multi Copy Code Blocks
bash

chmod 400 /C/user/jarvuy/downloads/my-demo-keyp-pair.pem
    
  • Find Your Instance’s Public IP Address
    • Go back to the EC2 dashboard and click on Instances.
    • Select the instance you just launched and find the Public IPv4 address listed.
    • Connect to the Instance
    • In the terminal, use the following command to connect to your EC2 instance:
Multi Copy Code Blocks
bash

ssh -i my-demo-key-pair.pem ec2-user@172.162.122.89
    
  • Replace /path/to/your-key.pem with the path to your key pair file, and your-ec2-public-ip with the public IP address of your EC2 instance.
  • For Ubuntu instances, the default username is usually ubuntu. For Amazon Linux, it’s ec2-user.
Multi Copy Code Blocks
bash

ssh -i ~/Downloads/my-key.pem ec2-user@54.123.45.67
    
  • Accept the SSH Key Fingerprint
    • The first time you connect to the instance, you will be prompted to confirm the authenticity of the host. Type yes to proceed.
  • You are now connected to your EC2 instance!
  • For Windows Instances (via RDP)
    • Download an RDP Client
      • If you are using Windows, the default Remote Desktop Protocol (RDP) client is already installed. For macOS and Linux, you can use a tool like Microsoft Remote Desktop.
    • Obtain the Administrator Password
      • From the EC2 console, select your instance, and click on Connect.
      • Click the Get Password button and upload your .pem key file to decrypt the password.
    • Connect Using RDP
      • Open the RDP client and enter the Public IP address of the EC2 instance.
      • When prompted, use the Administrator username and the decrypted password to log in.

Step 3: Fixing the Common SSH ‘Unprotected Private Key File’ Error

  • If you try to connect via SSH from a Linux or Mac terminal using a freshly downloaded key, you will likely encounter this error
Multi Copy Code Blocks
bash

Permissions 0644 for 'key.pem' are too open
    
  • To fix this, you must restrict the file permissions before running the SSH command. Run the following command in your terminal
Multi Copy Code Blocks
bash

chmod 400 your-key-pair.pem
    
  • This ensures that only your current user has read permissions for the key, which is a mandatory security requirement for SSH

Step 4: Terminate the Instance (Optional)

If you’re done with the instance and want to avoid unnecessary charges, it’s important to terminate it:

  • Go to the EC2 dashboard.
  • Select your instance.
  • Click on the Actions dropdown and select Instance State > Terminate.

Best Practice Tip

  • Never open Port 22 to 0.0.0.0/0 (Anywhere) in a production environment, as it invites brute-force attacks from bots worldwide. Always select ‘My IP’ to restrict access to your specific network location

Troubleshooting Common EC2 Connection Errors

Diagnose and resolve common connectivity issues when accessing EC2 instances:

Error 1: “Permissions 0644 for ‘key.pem’ are too open”

  • The Cause: SSH clients automatically reject private key files that have permissive read/write permissions accessible by other system users.
  • The Fix: Run chmod 400 your-key.pem in Linux/macOS terminals. On Windows PowerShell, restrict file ACL inheritance so only the current user account has read access.

Error 2: “Connection Timed Out” During SSH Attempts

  • The Fix: Open EC2 Console -> Security Groups -> Edit Inbound Rules -> Ensure Port 22 is open to your current IP address (My IP).
  • The Cause: The Security Group attached to your instance is missing an inbound rule for Port 22, or your local public IP address changed.

Leave a Reply

Your email address will not be published. Required fields are marked *