Skip to content

Instantly share code, notes, and snippets.

@alanmur
Last active September 15, 2021 22:52
Show Gist options
  • Save alanmur/2f7bc7163c545bfddf8d44518c17d06d to your computer and use it in GitHub Desktop.
Save alanmur/2f7bc7163c545bfddf8d44518c17d06d to your computer and use it in GitHub Desktop.
Connect to an EC2 instance with SSH

Connect to an EC2 instance with SSH

Step 1: Set up Visual Studio Code

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.

vsc001

Type remote into the search box and it will appear in the results.

vsc002

Click install. The extension installs itself in a second or two.

Step 2: Set up the instance security group to allow SSH connections to the instance

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.

sgp001

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.

sgp002

You can see there are currently no rules to allow access to the instance. Edit the inbound rules.

sgp003

Add a new rule to allow our SSH connection.

sgp004

Select SSH from the type drop-down.

sgp005

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.

sgp006

Save the rules.

sgp007

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.

Step 4: Set up the SSH configuration file

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.

vsc003

Pick Remote-SSH: Open Configuration File…

vsc004

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.

vsc005

This file will be created and opened in the editor for you.

vsc006

Edit it as follows:

  1. 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)
  2. 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.
  3. Set the user to ec2-user for Amazon Linux 2, mac1.metal etc. (Other OS may need a different user)
  4. 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.

vsc007

This will then enable Remote-SSH to find the credentials for your EC2 instance.

Step 5: Connect to the EC2 instance

Next, we will connect to our EC2 instance.

Click on the green remote icon in the bottom left corner of the application.

vsc008

Pick Remote-SSH: Connect Current Window to Host…

vsc009

Select the friendly host name for the EC2 instance.

vsc010

And pick the operating system on the EC2 instance.

vsc011

You will be prompted to accept the fingerprint of the server, pick Continue.

vsc012

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+`

vsc013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment