spa Home Products Services Blog Contact menu

Connecting to AWS EC2 instance from Windows 10

Prerequisite

Create an EC2 instance on AWS console. During instance creation, AWS will give you an option to generate and download the private key for that instance. Store this key in a safe place and do not lose it since the process of recovering the key is difficult and for instances with instance backed storage it is impossible.

In addition, you should creat a public IP and associate it with your instance if you are running a Web server on the EC2 instance. You can create a new IP from the Elastic IPs page on the EC2 Dashboard and associate it with your instance from the Action menu on the Instances page. You can use this IP address to set up an A record in your DNS server and point your custom domain to the Web server hosted on the EC2 instance. There is a two-fold advantage to using the IP address for the domain settings. First, your instance may terminate and Web server may get moved so that it now has a new server name. You can then re-associate the IP address with the new instance and you do not have to change your DNS settings. The other advantage is that you can temporarily associate the IP with another EC2 instance with a simple maintainence Web page and perform upgrades and other maintainence on your Web server.

SSH Connection

Connecting to your EC2 instance requires setting up an SSH session. Windows 10 now comes with the ssh command built-in. You can create a SSH session using the private key you downloaded during instance creation. Amazon documentation asks you to change the permissions on the SSL certificate to "400". Easy enough if you are on unix/linux but how do you achieve this in Windows 10? You can change permissions using the Windows properties dialog box but we prefer the command line. To get started, open the cmd window, change directory to where the private key is located and type the following commands. Replace privatekey.pem with name of your private key file and laptop\yourusername with the output of the whoami command

    # Reset to remove explict permissions
    C:\credentials>icacls.exe privatekey.pem /reset
    Processed file: privatekey.pem
    Successfully processed 1 file;Failed processing 0 files

    # Find out your user name 
    C:\credentials>whoami
    laptop\yourusername

    # Give current user explicit read-permission
    C:\credentials>icacls.exe privatekey.pem /GRANT:R laptop\yourusername:R
    Processed file: privatekey.pem
    Successfully processed 1 file;Failed processing 0 files

    # Disable inheritance and remove inherited permissions
    C:\credentials>icacls.exe privatekey.pem /inheritance:r
    Processed file: privatekey.pem
    Successfully processed 1 file;Failed processing 0 files

            

Once you have set the right permissions on the private key, connect to your EC2 instance using the following command:

    C:\crendentials>ssh -i "privatekey.pem" ec2-user@ec2instancename
          

You can find the ec2instancename in the Public DNS (IPv4) column in the instances table displayed on the Instances page. The instance name will look something like ec2-54-201-154-17.compute-1.amazonaws.com.

Transferring File

You can use WinSCP (https://winscp.net) to transfer files between the local machine and the EC2 instance. WinSCP offers a nice user interface for trasferring files both ways. It requires the private key to be in the Putty format but the latest version will convert the Amazon PEM key and use it to establish the secure connection.