1

I am trying to set some environment variables for a user managed notebook in google cloud Vertex AI. I don't want to set this from a jupyter notebook itself because I want these environment variables to be available to anyone who opens a jupyter notebook from this notebook instance. This is what I have tried so far but nothing has worked:

  1. I have an existing user managed notebook. I ssh'd into the notebook vm and then set a environment variable, export TEST_VAR=TEST_VARIABLE_WAS_SET there. However, when I open a jupyter notebook from the console and do os.environ["TEST_VAR"], it gives a key error. So, I am assuming that this has something to do with the fact that the jupyter lab session that Vertex AI starts is in a different shell session or something similar. I also tried to add the following two metadata keys to the vm, and then restarted the vm, but it did not work:

gcloud compute instances add-metadata ${INSTANCE_NAME} --metadata startup-script-url=$GCS_BUCKET_NAME/script.sh

where script.sh is:

#!/bin/bash

export TEST_VAR=TEST_VARIABLE_WAS_SET

AND

gcloud compute instances add-metadata ${INSTANCE_NAME} --metadata container-env-file=$GCS_BUCKET_NAME/notebook-env.txt

where notebook-env.txt is

TEST_VAR=TEST_VARIABLE_WAS_SET
  1. I also tried to create a new instance of a user managed notebook from the cloud console. In that I tried to provide a script in the "Select a script to run after creation" and also through the "Metadata" option by providing, the key as startup-script-url and the value as the script location on google cloud storage. The script was the same startup script earlier.

So, how do I achieve this, for existing user managed notebooks and when I create new ones?

racerX
  • 930
  • 9
  • 25
  • Does [this](https://cloud.google.com/sdk/gcloud/reference/notebooks/environments/create) help you? It describes how you can create/set an environment for a notebook. – Eduardo Ortiz Jan 24 '22 at 17:15

1 Answers1

1

Based on the public documentation that I shared with you a couple days ago, what you would need to do to set your environment variables correctly and without a problem is to run your script (using --post-startup-script) after the notebook instance fully boots up. The path must be a URL or Cloud Storage path as mentioned (gs://path-to-file/file-name).

Consider below approach as an example;

–-post-startup-script=$GCS_BUCKET_NAME/script.sh
Eduardo Ortiz
  • 715
  • 3
  • 14