run vscode server on GCP
Idea taken from https://github.com/aws-samples/cloud9-to-power-vscode-blog
This repo provides samples to connect GCP instance via
- Identity-Aware Proxy
- Start instance when ssh connected
- Automatic stop after deactivation
Currently, I uses powershell
to start my linux server. Feel free to add new features, like bash
script, specifiy zone, and etc.
- Save money (The server only runs when you are online)
- Takes time to cold start the server
-
Create an instance as your dev server
- you can also use pre-existed isntance.
- Please include extrnal ip, or public internet access. Even though Identity-Aware Proxy should work if this server does not have an extrnal ip, we still need to download vscode server from internet.
-
Install
gcloud
on your client server -
Run
gcloud init
- Input your account and region.
-
Run
gcloud compute config-ssh
This will create files under
~/.ssh
│ config │ google_compute_engine │ google_compute_engine.ppk │ google_compute_engine.pub
Meanwhile, in the
config
file.# Google Compute Engine Section # # The following has been auto-generated by "gcloud compute config-ssh" # to make accessing your Google Compute Engine virtual machines easier. # # To remove this blob, run: # # gcloud compute config-ssh --remove # # You can also manually remove this blob by deleting everything from # here until the comment that contains the string "End of Google Compute # Engine Section". # # You should not hand-edit this section, unless you are deleting it. # Host YOUR_INSTANCE_NAME.us-central1-a.YOUR_PROJECT HostName YOUR_INSTANCE_IP IdentityFile ~/.ssh/google_compute_engine UserKnownHostsFile=~/.ssh/google_compute_known_hosts HostKeyAlias=compute.YOUR_INSTANCE_ID IdentitiesOnly=yes CheckHostIP=no # End of Google Compute Engine Section
You may omit this, since we just need the project level private key in
google_compute_engine
. -
Now put something like this in your
~/.ssh/config
Host vscode-dev-server
IdentityFile ~/.ssh/google_compute_engine
User YOUR_USERNAME
HostName YOUR_INSTANCE_NAME
ProxyCommand powershell -Command "~/.ssh/gcloud/start-dev-server.ps1" %h
vscode-dev-server
is your Host alias.
-
Copy the contents of
ssh/gcloud/start-dev-server.ps1
to~/.ssh/gcloud/start-dev-server.ps1
- When the server is started initially, i.e. from
TERMINATED
toRUNNING
, theRUNNING
status may not indicate that the server is ready to handle ssh connections. You may receiveError Code 4047. Either the instance doesn't exist, or the instance is stopped.
My solution is to sleep 30 seconds before ssh connection. (I do not have a workaround to this.)
- When the server is started initially, i.e. from
-
Copy
spot-if-inactive.sh
to your server, and edit if necessary.- When your close your vscode remote ssh session, the
server.sh
in server may still run for 3-5 minutes, and theSHUTDOWN_TIMEOUT=5
is triggered when no process is running
- When your close your vscode remote ssh session, the
-
Run
crontab -e
and put* * * * * YOUR_PATH_TO/stop-if-inactive.sh
-
When you reconnect to ssh with in 5 minutes and the shutdown has scheduled, you may receive system is going down. unprivileged users are not permitted to log in anymore. for technical details, see pam_nologin(8). Please edit
/etc/pam.d/sshd
and add# Disallow non-root logins when /etc/nologin exists. account [success=1 default=ignore] pam_succeed_if.so quiet uid eq YOUR_USER_ID account required pam_nologin.so
-
Now, enjoy with vscode remote ssh.
If you wanna to test ssh connection, use ssh vscode-dev-server -vvv
to debug.