Reputation: 14764
I have an application running in Compute Engine on Google Cloud Platform which reads system environmental variables.
I wonder what is the way to put them in my instance so that the application will be able to read them in runtime.
Here is how I create an instance:
gcloud compute instances create ${PROJECT_ID} \
--image-family debian-9 \
--image-project debian-cloud \
--machine-type g1-small \
--scopes "userinfo-email,cloud-platform" \
--metadata-from-file startup-script=${SCRIPT} \
--metadata release-url=${BUCKET_URL} \
--zone ${ZONE} \
--tags http-server
I have some security credentials, e.g. API keys, passwords, etc. which I want to upload to my instance and expose them as env vars to be read by my application.
Is there any console available for that, flag or command to automate this?
Upvotes: 4
Views: 7826
Reputation: 10711
You can do it by connecting over SSH once you have created the instance.
It is explained in set default values in environment variables.
For example, use the export command to set the zone and region variables like:
$ export CLOUDSDK_COMPUTE_ZONE="us-central1-a"
$ export CLOUDSDK_COMPUTE_REGION="us-central1"
To make these environment variables permanent:
Alternative 1: Using bashrc file
include these export commands in your
~/.bashrc
file
you can use nano or vim to put the variables
sudo nano ~/.bashrc
then restart your terminal and check
$ env
Alternative 2: Using start up script
You can also use the export command within a start up script to let your metadata to become the environment variables.
Upon creating your instance you may put it directly or via a file like this:
gcloud compute instances create vm-1 \
--metadata-from-file startup-script=$HOME/startup.sh \
--zone=us-west1-a
If the instance is already running, follow the instructions to set a startup script on a running instance .
Please remember that if you use the method of this start up script then you will need to run the script manually each time you set new variables.
Whatever method you choose, make sure your $ env
setting is working correctly.
Better cek it again by restarting your instance within the shell or using the stop and start button in your console.
Upvotes: 2