Monday, October 30, 2023

VS Code Remote-SSH to AWS EC2 - disconnect and won't reconnect

Running VS Code 1.83.1 and Remote-SSH to AWS micro / free version EC2 instance of Ubuntu 20.04.6 LTS.

Found that after random intervals of low server stress the Remote-SSH session is disconnected and will not reconnect until the server is stopped and started.  Reloading VS Code does not help. Rebooting the EC2 instance does not help.  Remote IP / AWS security inbound rules are correct.

The fix for me was adding a swap file to the EC2 instance.

See here for the original post I used to set it up:
https://repost.aws/knowledge-center/ec2-memory-swap-file

Use the dd command to create a swap file on the root file system. The block size you specify should be less than the available memory on the instance or you receive a "memory exhausted" error.

I had success setting the swap file to 1 GB (128 MB x 8):

$ sudo dd if=/dev/zero of=/swapfile bs=128M count=8

Update the read and write permissions for the swap file:

$ sudo chmod 600 /swapfile

Set up a Linux swap area:

$ sudo mkswap /swapfile

Make the swap file available for immediate use by adding the swap file to swap space:

$ sudo swapon /swapfile

Verify that the procedure was successful:

$ sudo swapon -s

My results look like so:

Filename                                Type            Size    Used    Priority

/swapfile                               file            1048572 90880   -2

Start the swap file at boot time by editing the /etc/fstab file.

$ sudo nano /etc/fstab

Add the following new line at the end of the file, save the file, and then exit:

/swapfile swap swap defaults 0 0