Published on

Use VS Code on an EC2

Follow these steps:

  1. Make sure you have VSCode installed.
  2. Make sure you have a running SSH server preferrable an Ubuntu VM running on an EC2 instance.
  3. An SSH Client on your local machine.

Steps to SSH on VSCode

  1. Install the Remote extension pack on your VSCode.
  2. In VSCode, press F1 to open the command palette, and type connect.
  3. Select Remote-SSH: Connect to Host. This will bring out options for you to add or configure SSH Hosts.
  4. Select configure SSH Hosts. This will give you options to create a config file. By default you can choose /Users/username/.ssh/config. This will open a new file for you were you can create an ssh config.

Note:

  • The Host is any name you want VSCode to use to recognize your file.
  • The HostName is the public DNS of your EC2 instance or public IP address of your server.
  • The User is the username of your server.
  • The IdentityFile is the path to your ssh key which you got when you created your EC2 instance.

Press F1 again to open the command palette. Select Remote-SSH and this time, you should see the Host you just created.

Select yours, and it will attempt connecting to it. When it has successfully connected, you will see SSH connected in the bottom left corner of your VSCode.

Congrats! You have successfully connected your remote server to VSCode. Now you can start working on those files you want.

Port Forwarding instructions

You can add port forwarding rule via iptables.

First list all the rules currently running on the NAT (Network Address Translation) table:

[ec2-user@ip-XX-XXX-XX-X ~]$ sudo iptables -t nat -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

We don't see anything because we are yet to add a rule forwarding packets sent to external port 80 to internal port 8000 (taking an example here; change it as per your convenience).

[ec2-user@ip-XX-XXX-XX-X ~]$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8000

List it again and you will see a new PREROUTING chain:

[ec2-user@ip-XX-XXX-XX-X ~]$ sudo iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- anywhere anywhere tcp dpt:http redir ports 8000

You will see the application that was running on port 8000 was responding on port 80 as well.

Whitelist IP on AWS

To whitelist an IP on AWS, all you have to do is follow these simple steps:

  1. Click the dropdown “Services” from the top-right menu.
  2. Find the “EC2” service section.
  3. Click the “Security Groups” option located in the left menu.
  4. Click “Create Security Group
  5. Here, you’ll set the information and rules for the group.
  6. Click “Add Rule” in the “Inbound” tab.
  7. Next, set “Type” under “All Traffic”, “Source” under “Custom” and the host/IP address for whitelisting in the text box.
  8. Once you have added a description, click “Create”.
  9. From the “EC2 Dashboard”, click “Running Instances”.
  10. Select the instance for which you want the rule to run.
  11. Click the dropdown “Actions”.
  12. Go to “Networking” and click “Change Security Groups”.
  13. Select the security group you have created
  14. Click “Assign Security Group”.