简体   繁体   English

GCP / Cloud Functions 是否有像 AWS Lambda Layers 这样的库概念?

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

Does GCP / Cloud Functions have a library concept anything like AWS Lambda Layers? GCP / Cloud Functions 是否有类似 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?意思是,如果我们想减少代码重复并为我们的 Google Cloud Functions 推出一个公共库层,我们该怎么做?

No, you haven't this kind of feature with Cloud Function.不,Cloud Function 没有这种功能。 I have 3 immediate alternatives in mind我有 3 个直接的选择

  • Use Cloud Run and build your own base image with the required dependencies/common piece of code使用 Cloud Run 并使用所需的依赖项/通用代码构建您自己的基础映像
  • Package your common code in a library that you import in each of your function. Package 您在每个 function 中导入的库中的公共代码。 But that requires to publish your library somewhere public, or private with extra work in the function packaging and deployment.但这需要在 function 打包和部署中将您的库发布到公共或私有的地方。
  • 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.在 function A 和 function B 中,您使用 Common 目录中的公用文件。 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当您部署 function 时,请在root目录中执行此操作并使用源目录指示在哪个目录中查找要部署的 function

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即使只有一个有效运行,也会部署所有 function 文件
  • You have to declare all the dependencies at the functionX level.您必须在 functionX 级别声明所有依赖项。 Even the dependencies for the Common directory.甚至是 Common 目录的依赖项。 That can be problematic in case of new dependencies in the common directory and if you have a lot of functions如果公共目录中有新的依赖项并且您有很多功能,这可能会出现问题

Google's Cloud Platform now has Artifact Registry.谷歌的云平台现在有 Artifact Registry。 This service can used to store NodeJS packages (not sure about other runtimes) that can be referenced from a GCP Cloud Function.此服务可用于存储可从 GCP Cloud Function 引用的 NodeJS 包(不确定其他运行时)。

You can read more about it at the link below:您可以在下面的链接中阅读更多相关信息:

https://cloud.google.com/artifact-registry/docs/nodejs/store-nodejs https://cloud.google.com/artifact-registry/docs/nodejs/store-nodejs

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

相关问题 AWS Lambda 层:它是否有冷启动与主要 lambda 分开? - AWS Lambda Layers: does it have a cold start separated from the main lambda? Localstack 是否支持 AWS Lambda 层 - Does Localstack support AWS Lambda layers GCP API 云功能网关默认服务帐户身份验证:“您的客户端无权访问请求的 URL” - GCP API Gateway to Cloud Functions default service account authentication: "Your client does not have permission to the requested URL"" AWS lambda 层 - AWS lambda layers google pub/sub 是否有像 kafka 这样的分区概念? - Does google pub/sub have concept of partition like kafka? 一个项目中的多个 Spring Cloud Functions 部署在 AWS Lambda - Multiple Spring Cloud Functions in one project for deployment on AWS Lambda 为什么 GCP Cloud Functions 会记录两个空行? - Why does GCP Cloud Functions log two blank lines? 一个项目中的多个 Spring Cloud Functions 部署在 AWS Lambda - Multiple Spring Cloud Functions in one project for deployment on AWS Lambda Cloud Vision API 是否有办法返回键值对作为响应,就像他们的 AWS Textract 对应物一样? - Does Cloud Vision API have a way to get back Key Value pair as response, like their AWS Textract counterpart? 在 lambda 函数的 IAM 策略条件下使用 aws:ResourceTag 不起作用 - Using aws:ResourceTag in conditions on an IAM policy for lambda functions does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM