Test Connection

Checking Connection

ℹ️ Overview In this section, we’ll establish SSH connections to both EC2 instances and verify network connectivity. We’ll use Visual Studio Code with the Remote-SSH extension for a modern development experience.

There are several ways to connect to EC2 instances. You can follow the instructions to connect to EC2 using PuTTY. In this lab, we will use Visual Studio Code to establish the connection.

Setup Visual Studio Code

  1. Download Visual Studio Code

    • Visit Visual Studio Code
    • Download and install the appropriate version for your operating system

    Create VPC

  2. Install Remote-SSH Extension

    • Open Visual Studio Code
    • Click on the Extensions icon in the left sidebar
    • Search for Remote - SSH
    • Click Install

    Create VPC

Connect to EC2 Public Instance

  1. Access the EC2 page

    • Select Instances
    • Select EC2 Public
    • Click Connect

    Create VPC

  2. In the Connect to instance dialog

    • Select SSH client tab
    • Copy the SSH command from the Example section

    💡 Tip: The command format is:

    ssh -i "aws-keypair.pem" ec2-user@<public-ip-address>
    

    Create VPC

  3. Navigate to Key Pair Directory

    • Open a terminal or command prompt
    • Change to the directory where you saved your key pair file
    cd /path/to/your/keypair
    
  4. Set Key Pair Permissions (Linux/Mac only)

    • Update permissions to restrict access to the key file
    chmod 400 aws-keypair.pem
    

    ⚠️ Security Note: AWS requires the key pair file to have restricted permissions (read-only for owner) before it can be used for SSH connections.

    Create VPC

  5. Connect via SSH in VS Code

    • Press F1 or Ctrl+Shift+P to open the command palette
    • Type Remote-SSH: Connect to Host
    • Enter the SSH command you copied earlier
    • Select Linux as the platform when prompted

    Create VPC

  6. Verify Connection

    • Once connected, open a terminal in VS Code
    • You should see the EC2 Public instance prompt

    Create VPC

Test EC2 Public Internet Connectivity

  1. Test the internet connection by pinging a public IP:

    ping 8.8.8.8 -c5
    

    ✅ Expected Result: You should receive 5 successful ping responses, confirming internet connectivity.

    Create VPC

Connect to EC2 Private Instance

💡 Connection Architecture:

Your Computer → SSH → EC2 Public (via public IP)
                        ↓
                      SSH → EC2 Private (via private IP)
  1. Get EC2 Private IP Address

    • In the EC2 console, select Instances
    • Select EC2 Private
    • In the Details tab, note the Private IPv4 address

    Create VPC

  2. Test Connectivity from EC2 Public to EC2 Private

    • From your SSH session on EC2 Public, ping the private instance:
    ping <EC2-Private-IP> -c5
    

    ✅ Expected Result: Successful ping responses confirm network connectivity between the instances.

    Create VPC

Transfer Key Pair to EC2 Public (Windows Users)

ℹ️ Why Transfer the Key? EC2 Private doesn’t have a public IP, so we can’t SSH directly from our computer. We need to SSH from EC2 Public to EC2 Private, which requires the key pair file to be on EC2 Public.

  1. Convert PEM to PPK Format (Windows only)

    • Launch puttygen.exe
    • Click Load
    • Select your aws-keypair.pem file

    Create VPC

  2. Save as PPK Format

    • Click OK on the success message
    • Click Save private key
    • Save as aws-keypair.ppk

    Create VPC

  3. Confirm Key Conversion

    • You should now have both .pem and .ppk files

    Create VPC

  4. Upload Key to EC2 Public using PSCP

    • Open Command Prompt
    • Navigate to the folder containing your key files
    • Run the following command (replace with your EC2 Public IP):
    pscp -i aws-keypair.ppk aws-keypair.pem ec2-user@<EC2-Public-IP>:/home/ec2-user/
    

    💡 Command Breakdown:

    • -i aws-keypair.ppk: Authentication key for PSCP
    • aws-keypair.pem: File to upload
    • ec2-user@<IP>: Target user and host
    • :/home/ec2-user/: Destination directory

    Create VPC

Transfer Key Pair to EC2 Public (Linux/Mac Users)

  1. Upload Key using SCP (Linux/Mac alternative)

    • Open a terminal
    • Navigate to your key pair directory
    • Run:
    scp -i aws-keypair.pem aws-keypair.pem ec2-user@<EC2-Public-IP>:/home/ec2-user/
    

SSH to EC2 Private Instance

  1. Verify Key Upload

    • In your EC2 Public SSH session, list files:
    ls -la
    
    • You should see aws-keypair.pem in the output

    Create VPC

  2. Set Key Permissions

    • Update permissions for the key file:
    chmod 400 aws-keypair.pem
    

    🔒 Security Requirement: AWS requires key files to have restricted permissions (read-only for owner) before they can be used for authentication.

    Create VPC

  3. SSH to EC2 Private

    • Connect using the private IP address:
    ssh -i aws-keypair.pem ec2-user@<EC2-Private-IP>
    
    • Type yes when prompted to accept the host key

    Create VPC

Test EC2 Private Internet Connectivity

  1. Test Internet Connection

    • From EC2 Private, try to ping a public website:
    ping amazon.com -c5
    

    ❌ Expected Result: The ping will fail or hang because EC2 Private has no route to the internet yet.

    Create VPC

💡 What’s Next? In the next section, we’ll create a NAT Gateway to enable outbound internet access for EC2 Private while maintaining security by blocking inbound connections from the internet.

🏗️ Current Architecture:

  • ✅ EC2 Public: Can access internet (has public IP + Internet Gateway)
  • ❌ EC2 Private: Cannot access internet (no public IP, no NAT Gateway yet)
  • ✅ EC2 Public ↔ EC2 Private: Can communicate via private IPs

⚠️ Keep Connection Open: Leave your SSH session to EC2 Private open so you can verify internet connectivity after creating the NAT Gateway in the next section.