Skip to content

Run vscode server on GCP through iap. Start instance when in ssh connection, and stop if inactive.

License

Notifications You must be signed in to change notification settings

martinwang2002/gcp-vscode-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GCP vscode server

run vscode server on GCP

Idea taken from https://github.com/aws-samples/cloud9-to-power-vscode-blog

Summary

This repo provides samples to connect GCP instance via

Currently, I uses powershell to start my linux server. Feel free to add new features, like bash script, specifiy zone, and etc.

Pros and cons

  • Save money (The server only runs when you are online)
  • Takes time to cold start the server

Steps:

  1. 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.
  2. Install gcloud on your client server

  3. Run gcloud init

    • Input your account and region.
  4. 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.

  5. 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.

  1. 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 to RUNNING, the RUNNING status may not indicate that the server is ready to handle ssh connections. You may receive Error 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.)
  2. 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 the SHUTDOWN_TIMEOUT=5 is triggered when no process is running
  3. Run crontab -e and put * * * * * YOUR_PATH_TO/stop-if-inactive.sh

  4. 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
    

    see https://unix.stackexchange.com/a/241886

  5. Now, enjoy with vscode remote ssh.

Extra nots

If you wanna to test ssh connection, use ssh vscode-dev-server -vvv to debug.

About

Run vscode server on GCP through iap. Start instance when in ssh connection, and stop if inactive.

Topics

Resources

License

Stars

Watchers

Forks