Serverless 简明教程

Serverless - Include/Exclude

我们已经在“部署函数”一章中看到,要想从现有项目向 AWS Lambda 部署函数,我们需要修改函数以将 eventcontext 作为参数,并且需要在项目文件夹中添加一个 serverless.yml 文件,其中定义函数。然后执行 serverless deploy 即可完成这项工作。

We’ve already seen in the 'Deploying a function' chapter that to deploy functions from an existing project to AWS lambda, you need to modify the functions to take event and context as arguments and you need to add a serverless.yml file in the project folder, with the functions defined.Then hitting serverless deploy does the job.

在很多情况下,特别是当需要将某些函数从一个大型现有项目迁移到 AWS Lambda,你就会面临着一个空间挑战。如果你的项目足够大,那么你很可能超过 AWS 对 Lambda 函数设定的空间限制(250 MB,包括应用程序代码及其依赖项)。

Quite frequently, especially if you are asked to migrate some functions from an extensive existing project to AWS Lambda, you are faced with a size challenge.If your project is large enough, you may very well exceed the size limits imposed by AWS on Lambda functions (250 MB, including the application code and its dependencies).

而一些依赖项如 NumPy 本身就占据很多空间。例如,NumPy 约为 80 MB,SciPy 也差不多,以此类推。在这样的情况下,你用于应用程序代码的空间非常有限,而且需要一种方法来从 Lambda 部署包中排除不必要的文件。幸运的是,Serverless 使这项工作变得非常轻松。

And some dependencies like NumPy occupy a lot of space themselves. For instance, NumPy is ~80 MB large, SciPy is also in the same ballpark, and so on.In such scenarios, you have very limited space left for the application code and require a method of excluding unnecessary files from the lambda deployment package. Luckily, serverless makes this quite easy.

Include and exclude fields

你可以用“排除”标签指定希望从部署构建中排除的文件和文件夹,你可能已经猜到这一点。默认情况下,包含排除部分未指定的所有文件/文件夹。那么“包含”标签又有什么用?好吧,如果你希望一个文件夹通常被排除,但希望仅包含该文件夹中的几个文件或子文件夹,你可以通过“包含”标签指定这些文件/子文件夹。这样,该文件夹中的所有其他文件都将被排除,而仅“包含”部分中指定的文件保留。下面的例子将对此进行更好的解释。

As you would have guessed, you can specify files and folders that you want to exclude from the deployment build using the 'exclude' tag. All the files/ folders not specified in the exclude section are included by default. Then what is the use of the 'include' tag? Well, if you want a folder to be excluded in general, but want to include just a few files or sub-folders within that folder, you can specify those files/sub-folders within the 'include' tag. This way, all other files within that folder will be excluded, and only those specified within the 'include' section will remain. The example below will explain this better.

service: influx-archive-pipeline

provider:
   name: aws
   runtime: python3.6
   stage: prod
   region: us-east-2
   profile: yash-sanghvi
   timeout: 900
   memorySize: 1024

# you can add packaging information here
package:
   include:
      - src/models/config.py
      - src/models/lambda_apis/**
      - src/models/scheduled_lambdas/**

   exclude:
      - docs/**
      - models/**
      - notebooks/**
      - references/**
      - reports/**
      - src/data/**
      - src/visualization/**
      - src/models/**
      - utils/**

functions:
   user_details_api:
      handler: src/models/lambda_apis/user_details_api.sync_user_details
      events:
         - http:
            path: details/{user_id}
            method: get
            integration: lambda
            cors: true

   monitoring_lambda:
      handler: src/models/scheduled_lambdas/monitoring_lambda.periodic_monitoring
      events:
         - schedule: cron(15 16 * * ? *)

如上方的 serverless.yml 文件所示,我们排除了包含 serverless.yml 的根文件夹中的大多数文件夹。我们甚至排除了 src/models 文件夹。但是,我们希望包含 src/models 中的 2 个子文件夹和 1 个文件。因此,它们已被专门添加到“包含”部分。请注意,默认情况下将包含任何不属于排除部分的文件/文件夹。

As you can from the above severless.yml file, we are excluding most of the folders within the root folder that contains the serverless.yml. We have even excluded the src/models folder. However,we want to include 2 sub-folders and 1 file within src/models. Therefore, those have been specifically added in the 'include' section. Please note that any file/ folder that is not a part of the exclude section will be included by default.

请注意两个 Lambda 函数的路径。它们都位于 src/models。虽然 src/models 默认被排除,但这些函数专门位于 include 部分中提及的子文件夹中。因此它们将毫无问题地执行。如果我添加一个位于 src/data 中的函数,并且该函数将不被允许,因为 src/data 中的所有内容都已被排除。

Note the path of the two lambda functions. They both lie in src/models. While src/models is excluded by default, these functions specifically lie in sub-folders that are mentioned in the include section. Therefore they will execute without any issues. If I added a function lying in, say, src/data, that would not be allowed, because all the contents of src/data have been excluded.

请注意,指定 / * indicates that all content (files/sub-folders) within that folder gets covered. Thus if the docs folder contains 10 sub-folders and 12 files, all of which need to be excluded, then *-docs/ 即可。我们不需要分别提及其中的每个文件/文件夹。

Please note that specifying */ indicates that all content (files/sub-folders) within that folder gets covered. Thus if the docs folder contains 10 sub-folders and 12 files, all of which need to be excluded, then *-docs/ does the job. We don’t need to mention each file/folder separately.