Serverless 简明教程
Serverless - Plugins
随着 Serverless 的普及,人们自然会对针对利基用户案例的更多功能提出需求。这些需求由插件来满足。顾名思义,插件是可选的,你只需要安装所需的插件即可。在本篇章中,我们将了解如何访问可用于 Serverless 的多个插件,如何安装这些插件,以及如何在 serverless.yml 中引用这些插件。
As serverless grew in popularity, it was but natural that the demand for a lot more features for niche user-cases would rise. These demands are met by plugins. As the name suggests, plugins are optional and you only need to install the ones that you need. In this chapter, we will see how to access the several plugins available for serverless, how to install these plugins,and how to reference them within serverless.yml.
Browsing the list of plugins
可以在 www.serverless.com/plugins/ 中找到适用于 Serverless 的所有插件
All the plugins available for serverless can be found at www.serverless.com/plugins/
你可以在此处搜索插件。例如,如果你搜索“Python”,你将看到很多专门为 Python 运行时而开发的插件。它们按受欢迎程度排列。
You can search for a plugin here. For example, if you search for 'Python', you will see several plugins specifically developed for the python runtime. They are arranged in the order of popularity.

让我们来看看最受欢迎的 Python 插件(本文写作时):Python 需求。单击该插件。这将打开与此插件相关的详细文档。
Let’s look at the most popular python plugin (at the time of writing this post): Python Requirements. Click on that plugin. This opens up the detailed documentation related to this plugin.

本文件涵盖最重要的两个方面——安装插件并在 serverless.yml 中引用插件。这适用于任何插件。你只需打开其文档即可了解该插件的安装和使用方法。回到 Python 需求插件,文档声明此插件自动捆绑 requirements.txt 中的依赖项,并使其在你的 PYTHONPATH 中可用。
This documentation covers two of the most important aspects − installing the plugin and referencing it within serverless.yml. This is true for any plugin. You only need to open its documentation to know the installation and usage of that plugin. Coming back to the Python Requirements plugin, the documentation states that this plugin automatically bundles dependencies from requirements.txt and makes them available in your PYTHONPATH.
换句话说,如果你的 Lambda 函数需要额外的依赖项,如 pandas、numpy、matplotlib 等,你只需在 requirements.txt 文件中指定这些依赖项,该文件与你的 serverless.yml 文件位于同一个文件夹中。然后,此插件将完成剩下的工作。你甚至可以在 requirements.txt 中指定库的版本号。例如,这是 requirements.txt 样本文件的样子——
In other words, if your lambda function requires additional dependencies like pandas, numpy, matplotlib, etc., you need to just specify these dependencies in a requirements.txt file, that sits in the same folder as your serverless.yml file. Then this plugin will do the rest. You can even specify the version number of the libraries in requirements.txt.For instance, this is how a sample requirements.txt file looks like −
aws-psycopg2==1.2.1
boto
boto3==1.7.62
botocore==1.10.62
numpy==1.14.5
pandas==0.25.0
scipy==1.5.2
sqlalchemy==1.2.15
如你所见,你可以只提及其依赖项名称,或添加版本号(由 == 符号分隔)。当然,依赖项和应用程序代码的总大小不应超过 250 MB。因此,务必只包含你实际需要的依赖项。
As you can see, you can either mention just the dependency name, or add the version number along with it (separated by the == sign). Of course, the dependencies, along with the application code, should not exceed 250 MB in size. Therefore, it is essential that you include only those dependencies that you actually need.
现在,让我们回到我们的插件。我们已经准备好了 requirements.txt 文件。下一步是安装插件。打开你的命令提示符并导航到包含 serverless.yml 文件的项目文件夹。然后,按照文档说明,运行以下命令来安装插件 -
Now, let’s come back to our plugin. We have the requirements.txt file ready. The next step would be to install the plugin. Open your Command Prompt and navigate to the project folder containing the serverless.yml file. Then, following the documentation, run the following command to install the plugin −
sls plugin install -n serverless-python-requirements
实际上,如果你将 serverless-python-requirements 替换为任何其他插件名称,则以上命令仍然适用于大多数插件。但是,建议在安装新插件时遵循文档中给出的安装命令。运行上述命令后,你应该看到类似于下图中的消息 -
In fact, if you replace serverless-python-requirements with any other plugin name, the above command should still work for most plugins. However, it is recommended to follow the installation command given in the documentation whenever you install a new plugin. When you run the above command, you should see messages similar to those in the image below −

如你所见,在项目文件夹中创建了一个 packages.json 文件。如果你的项目文件夹中已经存在一个 packages.json 文件,则会对其进行编辑以包含上述插件。此外,serverless.yml 文件将自动进行编辑以包含已安装的插件。如果你现在打开 serverless.yml 文件,你应该看到添加了以下几行——
As you can see, a packages.json file got created in the project folder. If you had a packages.json file existing in your project folder, it would get edited to include the above plugin. Also,the serverless.yml file would automatically get edited to include the installed plugin. If you now open the serverless.yml file, you should see the following lines added −
plugins:
- serverless-python-requirements
这意味着 serverless.yml 中对插件的引用是自动完成的。此插件有几个设置,可以在文档中找到。我们将在下一章中介绍与“交叉编译”相关的设置。但现在,让我们看看使用此插件的效果。我已经在我 requirements.txt 中添加了 NumPy。这是我的 handler.py 文件的样子——
This means that the referencing of the plugin in serverless.yml is automatically done. There are several settings related to this plugin which can be found in the documentation. We will take up the setting related to 'Cross Compiling' in the next chapter. But for now, let us just see the effect of using this plugin. I’ve added numpy in my requirements.txt. This is how my handler.py file looks like −
import time
import numpy
def hello(event, context):
print("second update!")
time.sleep(4)
print(numpy.random.randint(100))
return("hello")
现在,让我将它部署到 Lambda。你应该看到类似于下图中的消息。关注包的大小。它现在大于 14 MB(这是压缩包的大小),而不是在添加插件之前的约 10 kB,因为 Numpy 依赖项也已打包在一起。
Let me now deploy this to lambda. You should see messages similar to those in the image below. Focus on the size of the package. It is now > 14 MB (this is the size of the zipped package) instead of ~10 kB before the plugin addition, because the numpy dependency is also getting bundled together.

这证明依赖项现在已与应用程序代码捆绑在一起。你可以使用 sls invoke local -f function_name 在本地测试它。如果你是一个 Windows 或 Mac 用户,很有可能在 AWS Lambda 控制台中测试已部署的 lambda 函数时会抛出一个错误,类似于下面给出的错误 −
This proves that the dependencies are now getting bundled along with the application code. You can test it locally using sls invoke local -f function_name.If you are a Windows or Mac user, chances are that testing out the deployed lambda function on the AWS Lambda Console will throw up an error, similar to the one given below −
Unable to import module 'handler':
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "/var/lang/bin/python3.8"
* The NumPy version is: "1.19.4"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
继续进入下一章以了解为什么发生此错误以及如何处理它。
Head on to the next chapter to learn more about why this error is occurring and how to deal with it.