aws lambda使用_如何为AWS Lambda构建和使用图层

aws lambda使用

AWS Lambdas are brilliant! They simplify deploying serverless applications. They allow us to really quickly build prototypes and automatically scale. One of the issues with having every function as a separate entity is that you need to include common code into every single Lambda.

AWS Lambdas很棒! 它们简化了无服务器应用程序的部署。 它们使我们能够真正快速地构建原型并自动缩放。 将每个功能都作为单独的实体存在的问题之一是,您需要在每个Lambda中都包含通用代码。

If you have to do the same thing the same way three times, its time to automate it — Automation Rule of Three
如果您必须以相同的方式做三遍相同的事情,那么该将其自动化的时间到了—自动化三分法则

层数 (Layers)

Lambda Layers have been created to solve this repeated code issue. The way they work is that you deploy your common code into a layer. This can be your common code or NPM packages that you always use. When you connect this layer to one of your Lambdas, you can access all the common code from inside your Lambda.

已创建Lambda图层来解决此重复的代码问题。 它们的工作方式是将通用代码部署到一个层中。 这可以是您常用的通用代码或NPM软件包。 将这一层连接到Lambda之一时,您可以从Lambda内部访问所有通用代码。

This means that you don’t have to copy the same file into every Lambda folder or create your own ‘common’ repo that you require.

这意味着您不必将相同的文件复制到每个Lambda文件夹中,也不必创建所需的“公用”存储库。

设置图层 (Setting up a Layer)

Since a layer is just a collection of code, we can start by creating a new folder for this layer. I like to have all my layers in a folder next to my Lambdas folder. We can create a new layer folder called DemoLayer which needs to have a folder for the runtime that it is going to use. For this example, we are going to use nodejs, so we create that folder.

由于层只是代码的集合,因此我们可以从为该层创建一个新文件夹开始。 我想将所有图层都放在Lambdas文件夹旁边的文件夹中。 我们可以创建一个名为DemoLayer的新图层文件夹,该文件夹需要有一个要使用的运行时文件夹。 对于此示例,我们将使用nodejs,因此我们创建该文件夹。

mkdir -p lambdaLayers/DemoLayer/nodejs

Using our terminal, we can navigate to the DemoLayer folder and initialize NPM.

使用我们的终端,我们可以导航到DemoLayer文件夹并初始化NPM。

cd lambdaLayers/DemoLayer/nodejs
npm init

Accept all the default values in the NPM setup, and you’ll have the package.json file generated in your folder.

接受NPM设置中的所有默认值,然后在文件夹中生成package.json文件。

For our first layer, we are going to import the moment library. This is the same process as you would use to add any NPM package to the layer.

对于我们的第一层,我们将导入时刻库。 这与将任何NPM软件包添加到图层所用的过程相同。

npm install --save moment
部署我们的层 (Deploying our Layer)

Now that we have the common code in our folder we need to deploy it. To do this, we need to zip up the whole folder and then upload it as a Lambda Layer. To zip up the folder content you can navigate into the DemoLayer folder and run a zip command on the content of the folder.

现在,我们在文件夹中有了通用代码,我们需要对其进行部署。 为此,我们需要压缩整个文件夹,然后将其上传为Lambda图层。 要压缩文件夹内容,您可以导航到DemoLayer文件夹并在该文件夹的内容上运行zip命令。

cd ../
zip -r demoLayer.zip ./*

You should now see a demoLayer.zip file inside your folder. We can now go the AWS Console create our layer.

现在,您应该在文件夹内看到一个demoLayer.zip文件。 现在我们可以去AWS控制台创建我们的图层。

In the AWS console, navigate to AWS Lambda, and on the left-hand side, we should have options including Layers.

在AWS控制台中,导航到AWS Lambda,然后在左侧,我们应该有包括图层在内的选项

Inside the layers menu, we have the option to create a new layer. Clicking this opens up the setup options where we can give the layer a name, a description, upload the zip file we just created and select the runtimes.

在图层菜单中,我们可以选择创建一个新图层。 单击此按钮将打开设置选项,在这里我们可以给层命名,描述,上载刚刚创建的zip文件并选择运行时。

测试图层 (Testing the Layer)

With the layer created we can test that it all works. To do this, we can create a new Lambda called DemoWithLayer that runs on node 8.10. Inside this Lambda we can add this code:

创建了图层后,我们可以测试所有图层是否正常工作。 为此,我们可以创建一个新的Lambda,名为DemoWithLayer ,它在节点8.10上运行。 在此Lambda中,我们可以添加以下代码:

const moment = require('moment');

exports.handler = async (event) => {
    let momentNow = moment.now();
    
    const response = {
        statusCode: 200,
        body: JSON.stringify({momentNow}),
    };
    return response;
};

We can test what happens when we run this without the layer by creating a test event. In the top right of the Lambda console, click select a test event and then configure test events. This opens a config window where we create the JSON blob that is sent to the handler. As we don’t use the event object, we can pass the default values, give this test a name and create it.

我们可以通过创建测试事件来测试在没有层的情况下运行此代码时发生的情况。 在Lambda控制台的右上方,单击“ 选择测试事件” ,然后配置测试事件。 这将打开一个配置窗口,我们在其中创建发送到处理程序的JSON Blob。 由于我们不使用事件对象,因此可以传递默认值,为该测试命名并创建它。

We can now click the Test button to run our Lambda. Doing this results in this message:

现在,我们可以单击“ 测试”按钮来运行Lambda。 这样做会产生以下消息:

This is because our Lambda doesn’t have the moment module installed. We can now add our new layer to the Lambda and rerun the test.

这是因为我们的Lambda没有安装moment模块。 现在,我们可以将新层添加到Lambda并重新运行测试。

To add a layer, click the Layers button underneath the DemoWithLayer button. Scroll to the bottom of the page to the Referenced layers section and click the add a layer button. In the popup, we can select our DemoLayer from the dropdown and selecting the highest version.

要添加图层,点击DemoWithLayer按钮下方图层按钮。 滚动到页面底部的“ 参考图层”部分,然后单击“ 添加图层”按钮。 在弹出窗口中,我们可以从下拉菜单中选择DemoLayer并选择最高版本。

Add this to the Lambda and make sure to save the Lambda changes. When we rerun the test, we get a success response. You can use this process to remove a lot of the common packages from your Lambdas.

将其添加到Lambda并确保保存Lambda更改。 当我们重新运行测试时,我们会收到成功的响应。 您可以使用此过程从Lambda中删除很多常见的程序包。

翻译自: https://www.freecodecamp.org/news/lambda-layers-2f80b9211318/

aws lambda使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值