Does GCP / Cloud Functions have a library concept like AWS Lambda Layers?

Viewed 1287

Does GCP / Cloud Functions have a library concept anything like AWS Lambda Layers? Meaning, if we wanted to reduce code duplication and roll out a common library layer for our Google Cloud Functions, how would we do it?

1 Answers

No, you haven't this kind of feature with Cloud Function. I have 3 immediate alternatives in mind

  • Use Cloud Run and build your own base image with the required dependencies/common piece of code
  • Package your common code in a library that you import in each of your function. But that requires to publish your library somewhere public, or private with extra work in the function packaging and deployment.
  • Use a project structure like this (I use usually this)

Project tree

root
  |
  - Common
  |
  - Functions
    |
    - function A
    |
    - function B

In function A and function B you use the common file in the Common directory. When you deploy your function, do it in the root directory and use the source directory to indicate in which directory to look for the function to deploy

gcloud functions deploy --source=function/functionA ...

Trade Off

  • All the files/directory in the root path are copied. All the function file are deployed even if only one is effectively run
  • You have to declare all the dependencies at the functionX level. Even the dependencies for the Common directory. That can be problematic in case of new dependencies in the common directory and if you have a lot of functions
Related