具有自定义打包的无服务器框架 [英] Serverless Framework with custom packaging

查看:80
本文介绍了具有自定义打包的无服务器框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用无服务器框架,而且我似乎无法按照自己的方式打包函数.

I'm using the Serverless Framework and I can't seem to package my functions the way I want.

我当前的文件夹目录如下:

My current folder directory looks like this:

service/
  serverless.yml
  function1/
    package.json
    handler.js
    node_modules/
      ...
  function2/
    handler.py
    requirements.txt
    venv/
      ...

是否可以选择要包含在zip文件中的文件夹,并在zip文件中指定相应的目标路径?

Is it possible to cherry pick which folder I want to include in the zip file and also specify the corresponding destination path inside the zip file?

例如,我要压缩function1文件夹中的所有内容,但所有内容都应位于zip文件的根目录下.现在,所有生成的文件都位于zip文件中名为function1的文件夹中.

For example, I want to zip everything inside function1 folder but all of them should be at root level of the zip file. Right now all the generated files are in a folder called function1 in the zip file.

推荐答案

打包不能以这种方式工作(文档).您无法指定将在zip文件中保存的文件树.您只能在此zip文件中指定要包括排除的内容.

Packaging does not work this way (docs). You can't specify the file tree that will be saved inside the zip. You can only specify what you will include or exclude in this zip.

docs 链接中,您可以看到一个使用工件的选项.在这种情况下,您可以使用所需的规则开发自己的代码以进行zip压缩,并输出供无服务器框架使用的zip文件.有可能,但我希望您不需要.

In the docs link, you can see an option to use an Artifact. In this case, you could develop your own code to zip using exactly the rules that you want and output a zip file to be used by the Serverless Framework. It is possible, but I hope that you don't need this.

为什么要将文件夹"function1"的内容移至根目录?也许您真正的问题是:如何引用位于另一个文件夹的Lambda函数?" .在这种情况下,您可以在 serverless.yml 文件中使用以下内容:

Why do you want to move the contents of the folder "function1" to the root level? Maybe your true question is: "How can I reference my Lambda function that is located at another folder?". If this is the case, you could use the following in the serverless.yml file:

service: test

functions:
  func1:
    handler: function1/handler.func1
  func2:
    handler: function1/handler.func2

语法为文件夹/文件名.功能.

另一种解决方案是为每个文件夹创建一个 serverless.yml 文件.这种方法的问题是您将无法从功能1访问功能2.

Another solution would be to create one serverless.yml file for each folder. The problem with this approach is that you would not be able to access the function2 from the function1.

这篇关于具有自定义打包的无服务器框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆