How to avoid plain text environment variables in a Google Cloud Function and instead pass them secretly?

The recommended way to manage secrets in Cloud Function is mounting the secrets from Secret Manager. This documentation explains very well how to set it up: https://cloud.google.com/functions/docs/configuring/secrets

In a nutshell:

  1. Create your secrets under Secret Manager;

enter image description here

  1. Edit your Cloud Function -> Advanced Options -> Security;
  2. Map the secrets you would like to be available during runtime;
  3. Grant the role roles/secretmanager.secretAccessor to the service account binded to the Cloud Function;
  4. Once done, you can use the secrets as environment variable (like you are used to and mentioned in your description);

enter image description here