pythongoogle-cloud-platformgoogle-cloud-functionsserverlesscode-duplication

How to share similar code between Cloud Functions?


I have multiple Cloud Functions in Google Cloud Platform that are very similar, changing only a few parameters and variable values ​​between them.

Is there a way to tackle this problem, organizing them like Python modules in order to reduce code duplication? Sharing the same variable and functions between them, maybe?

I've thought about using environment variables but they do not offer the purpose that I want.

And none of this other two questions here and here have any guidance on that.


Solution

  • Sharing this as a community wiki for the benefit of others

    As mentioned by @guillaume blaquiere

    If the code is the same and can be parameterized, Env Vars are the right solution. if it's more a package, a libraries, that you use as base of your development, and slightly adapt it for each function, you must package your code in a module, deploy it on Artifact Registry and reference it in you r requirements.txt file. cloud.google.com/artifact-registry/docs/python