{"id":17967844,"url":"https://github.com/simonprickett/google-cloud-functions-environment-variables","last_synced_at":"2025-08-16T15:32:09.719Z","repository":{"id":149219164,"uuid":"141200651","full_name":"simonprickett/google-cloud-functions-environment-variables","owner":"simonprickett","description":"Google Cloud Functions Environment Variables Demo","archived":false,"fork":false,"pushed_at":"2018-07-23T02:13:34.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T15:49:32.616Z","etag":null,"topics":["environment-variables","google-cloud","google-cloud-functions","google-cloud-platform","node","node-js","nodejs","secrets-management","serverless"],"latest_commit_sha":null,"homepage":"https://simonprickett.dev/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simonprickett.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-16T22:20:26.000Z","updated_at":"2023-01-02T12:04:14.000Z","dependencies_parsed_at":null,"dependency_job_id":"4a605ddc-b62b-4250-b3a6-516e9fc94d8a","html_url":"https://github.com/simonprickett/google-cloud-functions-environment-variables","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonprickett%2Fgoogle-cloud-functions-environment-variables","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonprickett%2Fgoogle-cloud-functions-environment-variables/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonprickett%2Fgoogle-cloud-functions-environment-variables/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simonprickett%2Fgoogle-cloud-functions-environment-variables/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simonprickett","download_url":"https://codeload.github.com/simonprickett/google-cloud-functions-environment-variables/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230043145,"owners_count":18163966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","google-cloud","google-cloud-functions","google-cloud-platform","node","node-js","nodejs","secrets-management","serverless"],"created_at":"2024-10-29T14:09:50.187Z","updated_at":"2024-12-17T00:14:52.832Z","avatar_url":"https://github.com/simonprickett.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Google Cloud Functions Environment Variables Test\n\nTest Google Cloud Function that uses environment variables (recently added functionality on Google Cloud).\n\nAlso be sure to check out the [Google documentation for this](https://cloud.google.com/functions/docs/env-var).\n\nI also wrote a [Medium article about this](https://medium.com/@simon_prickett/using-environment-variables-with-google-cloud-functions-e9948f70f6cd).\n\n## Setup\n\nEnsure that you have Google's `gcloud` commands installed and updated to the latest, including the `beta` commands.  Also ensure that you have a Google Cloud project set up with Cloud Functions enabled.\n\nIn Terminal, clone this repo and `cd` to the folder containing it.\n\nIn `index.js`, we have a function `helloEnvVars`.  This expects to be able to access three environment variables named:\n\n* `SUCH_SECRET`\n* `MANY_ENCRYPTS`\n* `SO_FINALLY_CAUGHT_UP_WITH_AWS`\n\nand will display their names and values in a HTML table.  As you can see, the value for each environment variable is retrieved in the same was as any other Node.js code:\n\n```\nprocess.env.VAR_NAME\n```\n\n## Deployment\n\nThis is a HTTP function so deploy as follows:\n\n```\ngcloud functions deploy helloEnvVars --trigger-http --project \u003cYour Google Cloud Project ID\u003e\n```\n\nThen test by pointing the browser at the URL that `gcloud` output at the end of the deployment, which looks something like this:\n\n```\nhttps://\u003cregion\u003e-\u003cprojectId\u003e.cloudfunctions.net/helloEnvVars\n```\n\nNote that all three environment variables show `UNKNOWN` for their value.\n\n## Setting Environment Variables\n\nGoogle provides two ways to supply values for the environment variables.\n\n### Individually from the Command Line\n\nSupply name/value pairs when deploying the function e.g. (note we have to use `beta` for this):\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --set-env-vars SUCH_SECRET=hello --project \u003cYour Google Cloud Project ID\u003e\n```\n\n### Multiple from the Command Line\n\nMultiple values can be provided together, note also have to be careful about quoting and escaping values:\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --set-env-vars SUCH_SECRET=\"Ssssh it's a secret\",MANY_ENCRYPTS=\"rTgHi0444452\\!\",SO_FINALLY_CAUGHT_UP_WITH_AWS=\"Oh yes\" --project \u003cYour Google Cloud Project ID\u003e\n```\n\n### From a YAML File\n\nEnvironment variables can also be set via a YAML file (which will overwrite all existing variable values and unset any that aren't listed in the file).\n\nExample `env.yaml`:\n\n```\nSUCH_SECRET: Ssssh it's a secret\nMANY_ENCRYPTS: rTgHi0444452!\nSO_FINALLY_CAUGHT_UP_WITH_AWS: Oh yes\n```\n\nDeploy as follows (note we have to use `beta` for this):\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --env-vars-file env.yaml --project \u003cYour Google Cloud Project ID\u003e\n```\n\nThen refresh the browser to see the new values.\n\n## Updating Environment Variables\n\nTo change the value of a deployed environment variable use `--update-env-vars`, which works like `--set-env-vars`:\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --update-env-vars SUCH_SECRET=\"Updated\",MANY_ENCRYPTS=\"Also updated\",SO_FINALLY_CAUGHT_UP_WITH_AWS=\"Updated too\" --project \u003cYour Google Cloud Project ID\u003e\n```\n\n(You can also use `--set-env-vars` to overwrite the values of existing environment variables and `--update-env-vars` to add set values for new environment values, so theese seem pretty interchangeable `¯\\_(ツ)_/¯`).\n\n## Deleting Environment Variables\n\nEnvironment variables are deleted as follows:\n\n### Selective Delete\n\n(Again note we have to use `beta` for this):\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --remove-env-vars SUCH_SECRET,MANY_ENCRYPTS --project \u003cYour Google Cloud Project ID\u003e\n```\n\n### Remove All\n\n(Again note we have to use `beta` for this):\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --clear-env-vars --project \u003cYour Google Cloud Project ID\u003e\n```\n\n## Notes\n\n* Environment variables are scoped by function, so are not shared between functions - you need to add separate environment variables for each function even if they are deployed together and/or exist in the same source code file\n* The maximum allowable size for environment variables is 32kb per function\n* You should probably add any YAML files that you use to store environment variable values in to your project's `.gitignore` file as you likely don't want these adding to source control!\n* A deployment failure will not update any environment variables, only a completely successful deployment updates environment variables\n* Once environment variables are set, deploying the function again without specifying any environment variable options will leave the current settings intact.  e.g. this does not change the values of any environment variables, nor unset any that previously existed: `gcloud functions deploy helloEnvVars --trigger-http --project \u003cYour Google Cloud Project ID\u003e`\n* The current values of all environment variables will be displayed in the output of `gcloud` every time you deploy a function\n\n## Gotchas\n\n### Escaping Values on Command Line\n\nDon't forget to quote and escape values when using `--set-env-vars` or `--update-env-vars`:\n\n```\ngcloud beta functions deploy helloEnvVars --trigger-http --set-env-vars SUCH_SECRET=\"Ssssh it's a secret\",MANY_ENCRYPTS=\"rTgHi0444452\\!\",SO_FINALLY_CAUGHT_UP_WITH_AWS=\"Oh yes\" --project \u003cYour Google Cloud Project ID\u003e\n```\n\n### Boolean and Int Types with YAML\n\nWhen deploying environment variables in a YAML file, be careful of values that can be coerced to `boolean` or `int` types by the YAML parser e.g.:\n\n```\nSO_FINALLY_CAUGHT_UP_WITH_AWS: true\n```\n\nThese cause deployment errors:\n\n```\nERROR: gcloud crashed (ValidationError): Expected type \u003ctype 'unicode'\u003e for field value, found True (type \u003ctype 'bool'\u003e)\n```\n\nWorkaround is to make the value a string:\n\n```\nSO_FINALLY_CAUGHT_UP_WITH_AWS: \"true\"\n```\n\nThe same applies for numeric values, for example:\n\n```\nMANY_ENCRYPTS: 12\n```\n\nwill error on deploy, but:\n\n```\nMANY_ENCRYPTS: \"12\"\n``` \n\nwill not.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonprickett%2Fgoogle-cloud-functions-environment-variables","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimonprickett%2Fgoogle-cloud-functions-environment-variables","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimonprickett%2Fgoogle-cloud-functions-environment-variables/lists"}