For this post, we’ll explore how we can connect the virtual machine(EC2 instance) from windows. We’ll use a simple method of OpenSSH Client from Apps & features. One of the prerequisites for the connection is the Keypair(either in .pem or .ppk format). “A key pair, consisting of a public key and a private key, is a set of security credentials that you use to prove your identity when connecting to an Amazon EC2 instance.” — source. For now, let’s consider the .pem format as it can be used directly by OpenSSH.
Let’s start the experiment by creating a free tier account in AWS. Followed by creating “Key Pairs” under EC2 -> Network & Security -> Key Pairs. The window looks like this as below,
Once we click on the ‘Create key pair’ a .pem file will be created and as well as downloaded which we’ll be using during the SSH process. While creating an instance in EC2, we can choose this key pair for that machine. Below is the screen we get while launching a new instance,
Once the instance is started, it will be in the running state as below,
Windows 10 — OpenSSH
Before we start the connection, we need to copy certain information from the EC2 (i.e) click the checkbox which is at the left side of the ‘test_instance’ and then choose the connect button at the top. Once we are in the connect to the instance, we can select the ‘SSH client’.
Now we have the required details. The next step is the SSH process, go to the Apps & features -> Optional features -> OpenSSH Client(installation). Navigate to the folder where we have stored the .pem file,
Type ‘cmd’ in the top (i.e) exactly on the test,
This will open a command prompt and we just need to paste the SSH command that we have just copied from the ec2 instance -> connect option (i.e) ssh -i “test.pem” ec2-user@ec2–54–205–52–183.compute-1.amazonaws.com. Once we type in the details, it will prompt for whether we would like to continue, by clicking on ‘yes’ the ec2 instance will get connected and we are in the VM now.
We can create new directories by giving the usual commands such as ‘mkdir’ and also list out the details present in the folder by giving ls. We will see how we can copy a directory from local to VM and then access python files that are present in the folder and execute them in the virtual machine. I have created a simple folder in the desktop “main_dir” and inside the folder have a python code(one-liner) that has only a print statement.
print(‘This is a main.py file copied from the local desktop to the EC2 instance’)
We’ll navigate to the folder where the ‘.pem’ file is located and from there we can type ‘cmd’. Then using the SCP command, we can copy the folder from local to the VM.
scp -i test.pem -r C:\Users\91965\Desktop\main_dir firstname.lastname@example.org:new_dir
pem file = test.pem, local folder = C:\Users\91965\Desktop\main_dir and VM folder = new_dir. Once we execute the above command, the folder would be available in the EC2 instance.
To run the python file, we can issue python main.py.
[ec2-user@ip-172-31-82-162 new_dir]$ python main.py
This is a main.py file copied from the local desktop to the EC2 instance
If we want to perform the reverse operation (i.e) saving files from VM to local, we just need to switch the places of the respective folders. This command would be specifically helpful when we are training a machine learning model in VM and want to transfer the learnt weights to the local folder.
C:\Users\91965\Desktop\test>scp -i test.pem -r email@example.com:new_dir C:\Users\91965\Desktop\new_dir