Do you know that you can SSH into EC2 instance from the VS Code? Yes, all you need is an SSH client and the private key file (.pem file) which you received when you launched your EC2 instance.
I recently set up a multi-host Hyperledger Fabric network (private blockchain) with multiple EC2 instances. I had to log in to each instance and do some coding. You know how difficult and frustrating it is to use the vi editor for development work.
So I was looking for an easy-to-use solution and came across this technique. Using this method, you can connect to your EC2 instance via VS Code and code seamlessly. Isn’t it cool? Let’s see how that is done in this article.
How to SSH into an EC2 instance
This article assumes that you already know what EC2 is and how to connect to an EC2 instance. If you’re just getting started or want to refresh your memory, we recommend reading our previous blog on this subject.
I also assume that you have a private key file .pem file with you before moving on. This article uses the ec2-demo.pem private key file created here in the previous blog.
If you are new to VS Code, refer to the official download page and install VS Code for your OS.
Install SSH Client
Next, install ‘Remote – SSH’ from VS Code extension. Refer to the below screen for the same.
After the successful installation of ‘Remote -SSH’, you will see this green icon at the bottom-left corner of the status bar.
Update SSH config file
Now, click on the green item which lists out the below options. From the list click on ‘Open SSH Configuration File’.
The next screen asks you which SSH configuration file you want to be updated. Select the one with C:\Users\<username>\.ssh\config file.
The above step opens the configuration file where you need to add the host machine details. In our example, we need to provide EC2 instance details.
Below is an example of how to update the config file for the EC2 instance we want to SSH into. After adding the details save the config file.
- Host: It can be any name. We have used the name ‘ec2-demo’.
- HostName: This is the Public IPv4 DNS of the EC2 instance. You can get this information from the EC2 instance details.
- User: This is the username of the EC2 instance. Since we are using Ubuntu OS, ubuntu will be our username.
- IdentityFile: This is the location of the .pem file. Note that for this example, I have copied the file to the current folder.
SSH into EC2
Now, you are all set to SSH into the EC2 instance. Click again on the green item from the bottom left corner of the status bar. It will list down the below the options and select ‘Connect to Host’.
After selecting ‘Connect to Host’, you will be shown the list of all the hostnames we have added in the config file. Here, we are shown only ec2-demo since we have added one host with the name ec2-demo.
As soon as you select ec2-demo, VS Code will try to connect to the EC2 instance. It will ask you to confirm the OS type. Since we are using Ubuntu, select Linux.
If you are successfully connected to an EC2 instance, you will see the hostname at the bottom left corner of the status bar.
Voila!! That’s it. You are successfully SSH’ed into the EC2 instance from VS Code. Now, you can use the VS code for all the development work needed in the EC2 instance from the comfort of your desktop/laptop.
To test everything is working fine, I have executed a simple ‘hello world’ Python script successfully. Refer to the below screenshot.
In this article, you understood how to SSH into an EC2 instance from your desktop/laptop. I hope you found the article useful.