Running code from VSCode in GCP VM.

Finding a way out of Obstavle. Image: Google.

When working in a restricted environment there are multiple issues we faced like installing required software and connection through firewalls.

Similarly In this case I was facing an issue connecting the VSCode from local to the GCP VM.

why should you use VSCode when you have the convenience of the Jupyter notebook to work on GCP VM?

I am not a Data scientist but a Software developer. My Web application uses the ML script as the integrated logic for the backend.SO if a product that you want to run in production (run for thousands of times every day). In many organizations, you do not have the luxury of a dedicated production team. Even if you do have that luxury, it is good practice to refactor your code before passing it to the production team to ensure the product works as expected once deployed. Here you can use the VScode for every change done on your local and run it on your production GCP VM. After a lot of struggle, I created this way to connect VScode with GCP VM.

VSCODE ENVIRONMENT SETUP

  1. Install google-cloud-sdk following the gcp google-cloud-sdk install and configuration guide. Please ensure your gcloud is updated.

gcloud components update

Also, to check the gcloud configuration

gcloud info

2) Init gcloud

gcloud init

Insert your credentials, eventually follow the gcp google-cloud-sdk install and configuration guide.

Download and put the VSCODE portable version. directory in c:\Users\<your username>

4) Test if you can connect with putty running:

gcloud compute ssh <gcp-vm-name> — tunnel-through-iap — project= <gcp-project-name>

It should open a putty shell connection.

5) SSH KEYS — Copy for C:\Users\<your user>\.ssh

google_compute_engine

google_compute_engine.ppk

google_compute_engine.pub

6)copy to the directory C:\Users\<your username>\.user_keys

7) For uniformity, rename the key files as:

google_compute_engine -> Ashutosh.ppk.privkey

google_compute_engine.ppk -> Ashutosh.ppk

google_compute_engine.pub -> Ashutosh.ppk.pubkey

Or you can change accordingly the key file names in configuration.

Note: if Windows asks if you would like to change the extension reply yes.

8) Edit C:\Users\<your username>\.ssh_config end set your GCP username and the IDfile to point to your priv key file, as follow:

Host localhost-22

HostName 127.0.0.1

Port 22

User <your gcp username in virtualmachine >

IdentityFile C:\Users\<your username> \.user_keys\ashutosh.ppk.privkey

CheckHostIP no

IdentitiesOnly yes

StrictHostKeyChecking no

UserKnownHostsFile C:\Users\<your username> \.ssh_config\known_hosts

Host host-127.0.0.2–22

HostName 127.0.0.2

Port 22

User <your gcp username in virtualmachine >

IdentityFile C:\Users\<your username> \.user_keys\ashutosh.ppk.privkey

CheckHostIP no

IdentitiesOnly yes

StrictHostKeyChecking no

UserKnownHostsFile C:\Users\<your username>\.ssh_config\known_hosts

NOTE
The second host can be used if you need more than one host connection active.

9) Start vscode (it is preconfigured with all expansions needed) Edit start_connection to activate the connection to your virtual machine

C:\Users\<your username> \

rem call gcloud config set project <gcp-vm-project-name>

call gcloud config set project <gcp-vm-project-name>

@echo on

gcloud compute start-iap-tunnel <your-gcp-vm-name> 22 — local-host-port=localhost:22 — zone=europe-west4-a

rem gcloud compute start-iap-tunnel <your-gcp-vm-name> 22 — local-host-port=127.0.0.2:22 — zone=europe-west4-a

wait the connection open go to CMD and run

C:\Users\<your username>\

cd c:\Users\%USERNAME%\

SET PATH=c:\Users\%USERNAME%\**\git\bin;c:\Users\%USERNAME%\**\git\cmd;c:\Users\%USERNAME%\**\git\usr\bin;C:\Users\%USERNAME%\**\npp.7.9.5.portable.x64;%PATH%

rem VSCode-win32-x64–1.54.3\Code.exe

VSCode-win32-x64–1.55.0\Code.exe

10) Configure the Remote-SSH connection setting to point to your installation

F1 then type “Remote-SSH settings” select with the mouse

configure your username

close the setting panel

Done.

How to connect to a remote host

  1. press F1 then type “Remote-SSH connect to host” select with the mouse.

There are more than one configured Local Ips that you can use to connect to different VMs eventually in parallel sections.

1) Ensure your IAP connection is active and select the host.

2) Then select Linux as platform (just the first time connect to a new host).

WARNING
Please ensure to close the connection before closing the VSCode so that it does not try to reopen the connection automatically creating possible connection issues.

Click on the green bar and select “Close Remote Connection” in the menu top list box.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store