简体   繁体   English

如何从谷歌云 Function 的代码更改环境变量的值?

[英]How to change the value of an Environment variable from the code of Google Cloud Function?

I'm trying to understand how to use environment variables properly for Google Cloud Functions .我试图了解如何为Google Cloud Functions正确使用环境变量。

I've deployed a function and I want to write and read it's environment variable (eg: XXX ).我已经部署了一个 function 并且我想写入和读取它的环境变量(例如: XXX )。

if not os.environ.get("XXX"):
    print("XXX_SET_DEFAULT")
    os.environ.setdefault("XXX","1")
else:
    print("XXX_SET")
    os.environ["XXX"] = "1"
print("XXX_GET", os.environ["XXX"])

After triggering the function, the value initially set in the Runtime environment variables list does not change.触发function后,Runtime环境变量列表中初始设置的值没有变化。

Although the behavior is little bit different, because when I deploy the variable with the function with a dummy value I can see XXX_SET string in the logs, but when I don't deploy the variable first, for the first run I can see XXX_SET_DEFAULT , then for the second run there is XXX_SET .虽然行为有点不同,因为当我使用虚拟值部署变量 function 时,我可以在日志中看到XXX_SET字符串,但是当我不先部署变量时,第一次运行时我可以看到XXX_SET_DEFAULT ,然后第二次运行有XXX_SET

I've set the variables with (and from the UI too):我已经设置了变量(也来自用户界面):

gcloud functions deploy my_func \
    ...
    --set-env-vars XXX=xxx

Questions :问题

  • Is it possible to set this variable from the python code somehow?是否可以通过 python 代码以某种方式设置此变量?
  • What makes this difference in the logs ( XXX_SET_DEFAULT, XXX_SET vs. XXX_SET, XXX_SET )是什么在日志中造成这种差异( XXX_SET_DEFAULT, XXX_SETXXX_SET, XXX_SET
  • Why the value of this variable doesn't get set from python code?为什么这个变量的值没有从 python 代码中设置?
    • is it somehow related to the multiple instantiations of the function (when needed)?它是否以某种方式与 function 的多个实例相关(需要时)?
  • What is the difference between Runtime environment variables and Build environment variables ? Runtime environment variablesBuild environment variables有什么区别?
    • can I deploy the variables between the Build environment variables somehow?我可以以某种方式在Build environment variables之间部署变量吗?
  • What are the best practices for using the Environment Variables with cloud functions at Google?在 Google 将环境变量与云功能结合使用的最佳做法是什么?

You are not meant to change environment variables in GCFs.您无意更改 GCF 中的环境变量。 They are there to set configurations.他们在那里设置配置。

Here are some basics:以下是一些基础知识:

  • Functions run in (docker) containers.函数在 (docker) 容器中运行。 These ensure that they always run in a clean and consistent environment.这些确保它们始终在干净且一致的环境中运行。
  • Build environment variables are for when the container is built. Build environment variables用于构建容器时。 Unless you are messing with how you build, you will not need these.除非你搞砸了你的构建方式,否则你将不需要这些。
  • Runtime environment variables are for when the function runs in the container. Runtime environment variables用于 function 在容器中运行时。 These are the ones you can access with your code.这些是您可以使用代码访问的内容。

What this means, is every time you run a function, it'll get a new container, with all the variables set to whatever you've set them in configuration.这意味着,每次运行 function 时,它都会得到一个新容器,所有变量都设置为您在配置中设置的值。

Now, it is possible that GCF in the background reuses some of the containers, and you may see some persistance but you cannot rely on this.现在,后台的 GCF 可能会重用一些容器,你可能会看到一些持久性,但你不能依赖它。

In conclusion:综上所述:

  • Environment variables are there to provide a simple configuration option for your code, that can be changed without having to change your code.环境变量可以为您的代码提供一个简单的配置选项,可以在不更改代码的情况下进行更改。
  • In general, it's better to treat them as read-only, even though you can change them, those changes will be discarded when the container where your code runs is discarded.通常,最好将它们视为只读,即使您可以更改它们,当您的代码运行的容器被丢弃时,这些更改也将被丢弃。
  • If you want to persist data between functions, use a database.如果要在函数之间保留数据,请使用数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Function:将秘密公开为环境变量? - Google Cloud Function: Expose Secret as Environment Variable? 根据另一个环境变量设置 Google Cloud Run 环境变量值 - Set Google Cloud Run environment variables value based on another environment variable Google App Engine 如何从灵活环境转变为标准环境 - How do change from Google App Engine from Flexible Environment to Standard environment 如何从命令行调用 Google Cloud Function - How to Call Google Cloud Function From the Command Line 如何暂停 Google Cloud Composer 环境? - How to pause a Google Cloud Composer Environment? 无法从 App Engine 标准环境连接到 Google 云 SQL - Cannot connect to Google cloud SQL from App Engine Standard Environment 如何在云 function 环境中将 webdriver_manager 的路径更改为自定义路径 - How to change path to webdriver_manager to custom path in the cloud function environment 谷歌云 Function:如何继续超时 function - Google Cloud Function : how to continues timeout function 如何更改 Google Cloud 中的区域/区域? - How to change Region / Zone in Google Cloud? 如何更改 Google Cloud SQL 中的日期样式? - How to change datestyle in Google Cloud SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM