Visual Studio Code must be installed on the local device. Download and install the application.
Start the application.
The extension to Visual Studio Code for Remote – SSH must be installed.
Select the extensions browser from the icons on the left. It is the bottom icon in the figure below.
Type remote into the search box and it will appear in the results.
Click install. The extension installs itself in a second or two.
Find the instance you want to set up for SSH in the AWS Console EC2 Dashboard. Select the instance, and in the instance details pane select the Security
tab.
The security group may be attached to many instances, and if that is the case it may be better to create an entirely new security group just for this. This procedure assumes that you don't mind giving SSH access to your own IP address for all instances that are using the security group.
Click the link for the security group attached to the instance that you wish to connect to.
You can see there are currently no rules to allow access to the instance. Edit the inbound rules.
Add a new rule to allow our SSH connection.
Select SSH
from the type drop-down.
Select My IP
from the source drop-down. Note that if you are on a VPN, your public IP address will be different from usual, and if you disconnect and reconnect from the VPN, your public IP address may change. For this reason, it is recommended not to use a VPN when connecting to instances using SSH, or detecting your public IP address.
Save the rules.
We are finished, and can see the rule for inbound SSH traffic from your own IP address in the list of rules. Your IP address may change periodically, because it's not static, or because you connect or disconnect from VPN, or because you change VLAN (like from an employee network to the public or guest network, or because you change location (from one Marriott to another, for example). In that case, delete the old rule with your previous IP address, and repeat the process to use your new IP address.
Next, we will provide information on how we want Visual Studio Code to connect to our EC2 instance.
Click on the green remote icon in the bottom left corner of the application.
Pick Remote-SSH: Open Configuration File…
Pick the one under the home directory (user folder in Windows) for user setup or the one in /etc/ssh
(C:\ProgramData\ssh
in Windows) for all users.
This file will be created and opened in the editor for you.
Edit it as follows:
- Set the alias to whatever friendly name you like, e.g.
player-auth
(only because that's the project I am using my EC2 instance for) - Set the hostname to the Public IPv4 DNS name we were given for our EC2 instance, visible in the list of running instances in the AWS console.
- Set the user to
ec2-user
for Amazon Linux 2, mac1.metal etc. (Other OS may need a different user) - Add an indented line 5, with the label IdentityFile and the location of the keypair file. Especially if there are spaces in a Windows path, the filepath will need to be delimited in quotes.
Don’t forget to save the file when you have finished editing.
This will then enable Remote-SSH to find the credentials for your EC2 instance.
Next, we will connect to our EC2 instance.
Click on the green remote icon in the bottom left corner of the application.
Pick Remote-SSH: Connect Current Window to Host…
Select the friendly host name for the EC2 instance.
And pick the operating system on the EC2 instance.
You will be prompted to accept the fingerprint of the server, pick Continue.
The connection will be completed, as shown in the green corner icon.
If you get an error about an invalid pipeline, then the connection failed and you should check the SSH config file. The Visual Studio Output window may contain clues. One possible cause is that the keypair file doesn't exist at the path given in the SSH config file. Another is that you are connected to a VPN, and you are using a public IP address is not set up correctly in the EC2 security group for the instance. A variant of that is that you were connected to VPN when setting the public IP address for the Security group but you are not connected now, or your public IP address has changed when you disconnected and reconnected.
When you are connected to the instance using SSH, you will be able to open the bash terminal with the shortcut CTRL+`