Skip to content
Eren Gaygusuz
Go back

Using a Virtual Machine on AWS EC2

Amazon EC2 is one of the most common AWS services for creating and managing virtual machines in the cloud. In this guide, I create a Linux EC2 instance, connect to it from the browser with EC2 Instance Connect, connect again from Windows Command Prompt over SSH, and fix the PEM file permission error that can appear on Windows.

The example uses a Free Tier eligible Amazon Linux instance type. AWS screens can change over time, but the main flow stays the same: choose an operating system image, create or select a key pair, launch the instance, and connect securely.

AWS EC2 virtual machine overview

Table of contents

Open Table of contents

Creating an EC2 Instance

Open the AWS Console in your browser. After signing in with your AWS account, use the search bar to find the EC2 service.

AWS Management Console home screen

This screen shows AWS Console sign-in screen.

AWS Console sign-in screen

Here, we search for EC2 in AWS Console.

Searching for EC2 in AWS Console

On the EC2 dashboard, start the instance creation flow with Launch instance.

EC2 dashboard Launch instance button

Give the instance a clear name. Then choose an Amazon Machine Image. In this example, Amazon Linux is selected because it is Free Tier eligible.

EC2 instance name and Amazon Linux image selection

For a basic test instance, choose a Free Tier eligible instance type such as t2.micro. Keep the architecture as 64-bit. To connect to the instance later, create a new key pair.

EC2 instance type and key pair selection

Enter a name for the key pair and create it. AWS downloads a .pem file to your computer. Keep this file safe, because it is required for SSH access.

Creating a new EC2 key pair

This screen shows Downloaded EC2 PEM key file.

Downloaded EC2 PEM key file

For the network settings, keep SSH access enabled. For a learning setup, the default values are enough, but in a real environment you should restrict inbound access as much as possible.

EC2 network settings

Review the storage settings and launch the instance.

EC2 storage settings and Launch instance button

When AWS finishes creating the instance, a success message appears.

EC2 instance creation success message

Connecting from the Browser

Return to the instance list. The new instance appears in the table, and you can track its status from the Instance state column.

EC2 instances list with the new instance

This screen shows EC2 instance state column.

EC2 instance state column

Open the instance details page and click Connect.

EC2 instance details Connect button

In the connection window, choose the EC2 Instance Connect tab and continue with Connect.

EC2 Instance Connect tab

If the connection succeeds, AWS opens a browser-based terminal session. This is the quickest way to test that the instance is running and reachable.

Browser terminal connected to EC2 instance

Connecting over SSH from Windows

For a local SSH connection, you need the PEM file downloaded while creating the key pair. In this example, the file is on the desktop. Open Command Prompt as administrator.

Opening Windows Command Prompt as administrator

Move to the folder that contains the PEM file. If it is on the desktop, the command looks like this:

cd C:\Users\USERNAME\Desktop

Here, we change directory to Desktop in Command Prompt.

Changing directory to Desktop in Command Prompt

Go back to the EC2 instance details page and copy the Public IPv4 DNS value.

Copying the EC2 Public IPv4 DNS address

The SSH command contains four parts:

An example command is:

ssh -i ExampleInstance.pem ec2-user@ec2-XX-XX-XXX-XXX.eu-central-1.compute.amazonaws.com

After running the command, the terminal connects to the EC2 instance.

SSH connection to EC2 instance from Command Prompt

Fixing PEM File Permission Errors on Windows

If the PEM file is stored in another folder or has broad inherited permissions, SSH may reject it with an UNPROTECTED PRIVATE KEY FILE warning. In that case, Windows file permissions must be narrowed so only your user can read the key.

SSH unprotected private key file warning

Right-click the PEM file and open Properties.

Opening PEM file Properties

Go to the Security tab and open Advanced.

PEM file Security tab and Advanced button

Disable inheritance and choose the option to remove inherited permissions from the file.

Disabling inherited permissions on the PEM file

Add a new permission entry for your Windows user.

Adding a new permission entry

Click Select a principal.

Select a principal link

Enter your Windows username and use Check Names. When Windows resolves the name, confirm with OK.

Selecting the Windows user for PEM file permissions

Allow only the required read permissions, such as Read & execute and Read.

Read permissions for the PEM file

Apply the permission changes and close the dialogs.

Applying PEM file permission changes

At the end, the PEM file should show only the restricted permission entries you need.

Final PEM file permission state

Run the SSH command again from the folder that contains the PEM file. This time, the connection should succeed.

Successful SSH connection after fixing PEM permissions

Summary

In this flow, we created an Amazon Linux instance on EC2, generated a PEM key pair, connected to the instance from the browser, and connected again from Windows over SSH. We also fixed the common PEM permission warning by removing inherited permissions and allowing only the current Windows user to read the key file.

When you are finished testing, stop the instance if you will use it later or terminate it if you no longer need it. Leaving EC2 instances running can create unexpected cost.


Share this post:

Comments


Previous Post
Downloading Unity Asset Store Packages to Another Folder
Next Post
Deploying an ASP.NET Core Web API on AWS EC2