4

Google Cloud Functions Tutorial : Using Environment variables

 3 years ago
source link: https://rominirani.com/google-cloud-functions-tutorial-using-environment-variables-20b4f0f82aa0
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Google Cloud Functions Tutorial : Using Environment variables

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

At the time of writing this article, support for Environment variables in Google Cloud Functions is in Beta.

As you start work your way through Cloud Functions, one of things that you will notice as you write them is that you often end up accessing other systems or define some constants, etc. You might want to connect to an external system for which you need to have a hostname, service name, etc.

The questions to ask are:

  1. Is it possible to define a Cloud Function and associate one or more Environment variables with it at the time of deployment?
  2. Will the Cloud Functions runtime make it available to our function when it runs it?
  3. Can we read these Environment variables directly in our code?
  4. Can we use the standard gcloud functions command or the GCP Console to manage the Environment variables?

The answer to all of the above questions is yes ! Enter Environment Variables in Cloud Functions.

Using process.env

Let us take a look at the existing environment variables that are already present. This is not something you want to do too much about but it comes in handy to understand which of these are reserved environment variables and used in some way or the other by Google Cloud Functions runtime.

exports.helloEnvVariables = (req, res) => {
res.status(200).send(process.env);
};

If you deploy the above Cloud Function, you should get an output that looks like this. It gives you an interesting amount of information.

{
“X_GOOGLE_CODE_LOCATION”: “/user_code”,
“WORKER_PORT”: “8091”,
“X_GOOGLE_SUPERVISOR_INTERNAL_PORT”: “8081”,
“X_GOOGLE_WORKER_PORT”: “8091”,
“FUNCTION_IDENTITY”: “[email protected]”,
“X_GOOGLE_LOAD_ON_START”: “false”,
“X_GOOGLE_FUNCTION_REGION”: “us-central1”,
“GCLOUD_PROJECT”: “mygcpproject”,
“FUNCTION_NAME”: “myEnvVarFunction”,
“X_GOOGLE_FUNCTION_MEMORY_MB”: “128”,
“SUPERVISOR_HOSTNAME”: “169.254.8.129”,
“X_GOOGLE_GCLOUD_PROJECT”: “mygcpproject”,
“PATH”: “/usr/local/bin:/usr/bin:/bin”,
“FUNCTION_REGION”: “us-central1”,
“PWD”: “/user_code”,
“FUNCTION_TRIGGER_TYPE”: “HTTP_TRIGGER”,
“FUNCTION_TIMEOUT_SEC”: “60”,
“X_GOOGLE_FUNCTION_TRIGGER_TYPE”: “HTTP_TRIGGER”,
“NODE_ENV”: “production”,
“SHLVL”: “1”,
“X_GOOGLE_FUNCTION_NAME”: “myEnvVarFunction”,
“X_GOOGLE_ENTRY_POINT”: “helloVersion”,
“X_GOOGLE_FUNCTION_IDENTITY”: “[email protected]”,
“DEBIAN_FRONTEND”: “noninteractive”,
“CODE_LOCATION”: “/user_code”,
“X_GOOGLE_GCP_PROJECT”: “mygcpproject”,
“FUNCTION_MEMORY_MB”: “128”,
“GCP_PROJECT”: “mygcpproject”,
“X_GOOGLE_SUPERVISOR_HOSTNAME”: “169.254.8.129”,
“PORT”: “8080”,
“SUPERVISOR_INTERNAL_PORT”: “8081”,
“X_GOOGLE_FUNCTION_TIMEOUT_SEC”: “60”,
“ENTRY_POINT”: “helloVersion”,
“OLDPWD”: “/var/tmp/worker/”,
“_”: “/usr/bin/env”,
“HOME”: “/tmp”
}

Specifying your own Environment Variables

To associate Environment Variables with your Cloud Function, you can do it in multiple ways:

  1. Use the GCP Console to define them at the time of creating/updating a Google Cloud Function
  2. Use gcloud functions command to set and update them individually, a list of them or via a configuration file.

In this tutorial, we will use the GCP Console, but if you would like to do the same via the gcloud functions command, you can read up the documentation here: Using environment variables.

Let us go ahead and create a Google Cloud Function via the GCP Console. I am assuming that you know that by now via this series and are able to bring up the Create Function form on your own.

If you scroll right down, you will find a More link. Click that and the Environment Variables section should show up as shown below:

Image for post
Image for post

Click on the Add variable button to add variables as you want. We will define four variables as shown below:

Image for post
Image for post

To use any specific environment variable value in your Google Cloud Function, you will have to use the process.env statement as shown below:

exports.helloEnvVariables = (req, res) => {
let minVer = process.env.MIN_VER;
let maxVer = process.env.MAX_VER;
let maxResults = process.env.MAX_RESULTS;
let hostName = process.env.HOSTNAME;
res.status(200).send("Function version:" + maxVer + "." + minVer);
};

If you deploy and run this function, it will send back the following response text:

Function version:1.2

A few points around using Environment Variables

Using Environment Variables in Google Cloud Functions is straight forward but there are a few things to keep in mind:

  1. The size of the variable names and values is limited to 32KiB. Additionally, there are reserved names for Environment variables and you saw that in the first example that we ran.
  2. Once you have deployed a Google Cloud Function with Environment variables or you are associated Environment variables for the first time for an existing Google Cloud Function, you cannot just apply any changes to Environment variables without deploying the function. In other words, any change to Environment variables requires a full function deploy.
  3. Extending point (2), keep in mind that when you deploy a new version of the function with updated Environment variables, the changes may not be immediately available on the next invocation of your function. I strongly recommend this talk by Jason Polites (Twitter: @jplts ). If you are working with Google Cloud Functions — do spend time with this

4. The scope of the Environment variables is limited to your specific Cloud function. You cannot share it across multiple functions.

5. Environment variables are not the place to put sensitive data. So do not keep credentials (usernames, passwords, keys) or anything that you do not want others to get a hold off. Take a look at this article by Seth Vargo (Twitter: @sethvargo) on managing secrets in serverless.

Here is an excellent article on using process.env and specifically I would recommend the section “When to use it” to understand common use cases that are a good fit for Environment variables.

6. In case you are providing Environment variables to your Cloud Functions via a configuration file at the time of gcloud functions deploy , do remember to add that file potentially to your .cloudignore and .gitignore files.

Reference

Official Documentation on Environment Variables → https://cloud.google.com/functions/docs/env-var#using_environment_variables


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK