aws lambda

    科技2023-12-05  110

    aws lambda

    Back in Nov 20014, Lambda functions brought in a revolution in the world of cloud computing. That introduced us to serverless computing in the true sense. The concept of independent pieces of computation that are fast and easily affordable, provided a new paradigm.

    早在20014年11月,Lambda函数就掀起了云计算领域的一场革命。 这使我们真正地了解了无服务器计算。 快速,轻松负担得起的独立计算概念提供了新的范例。

    Independent computing units were great. But soon, people realized that pieces of their code were really not independent. There was a lot that they needed to share. We need some common data structures, and utilities across the application. How do we provide them in Lambda functions?

    独立的计算单元很棒。 但是很快,人们意识到他们的代码确实不是独立的。 他们需要分享很多东西。 我们需要一些通用的数据结构以及整个应用程序中的实用程序。 我们如何在Lambda函数中提供它们?

    That led to some duplication of code. People found innovative ways of working around this — by having a unified code in one place, that is configured to deploy into several different units. So the same code was deployed into different lambda functions. That did work to an extent. But then the deployment complexity blew up.

    这导致了一些代码重复。 人们发现了解决此问题的创新方法,方法是在一个地方放置统一代码,该代码配置为部署到多个不同的单元中。 因此,将相同的代码部署到不同的lambda函数中。 在一定程度上确实有效。 但是随后部署的复杂性急剧上升。

    People thought of deploying everything for every lambda function. That works as well. But as the libraries and dependencies grew in size, we had to pay the price for it — in memory usage as well as the computation time.

    人们想到了为每个lambda函数部署所有内容。 那也一样。 但是,随着库和依赖项的大小增加,我们必须为此付出代价-内存使用率和计算时间。

    Considering all these problems, AWS introduced the concept of Lambda Layers — in the reInvent 2018.

    考虑到所有这些问题,AWS在reInvent 2018中引入了Lambda层的概念。

    什么是Lambda层 (What is a Lambda Layer)

    In simple words, a Lambda Layer is kind of an external library that can be directly included, and used in the Lambda function.

    简单来说,Lambda图层是一种外部库,可以直接包含在Lambda函数中并在其中使用。

    The Lambda Layer is provided as a zipped file that can be uploaded via code or directly onto the web console. A lambda function that needs the code in there, should be configured as such to use the layer.

    Lambda层以压缩文件的形式提供,可以通过代码上传或直接上传到Web控制台。 需要在其中使用代码的lambda函数应配置为使用该层。

    When such a lambda function runs, the contents of the layer are extracted into the /opt folder in the Lambda runtime environment. The layer need not be restricted to the language of the Lambda function. So long as it can be invoked and used by the code, it is just fine.

    运行此类lambda函数时,该层的内容将提取到Lambda运行时环境中的/ opt文件夹中。 该层不必局限于Lambda函数的语言。 只要可以被代码调用和使用,就可以了。

    Let us now check out a simple example of creating and using Lambda Layers. We will develop a simple NodeJS based Lambda function, and configure it to use a custom layer.

    现在让我们来看一个创建和使用Lambda图层的简单示例。 我们将开发一个简单的基于NodeJS的Lambda函数,并将其配置为使用自定义层。

    Creating the Lambda Layer

    创建Lambda层 Using it in the Lambda Function

    在Lambda函数中使用它

    创建Lambda层 (Create a Lambda Layer)

    The Lambda layer is a chunk of organized code that can be used in the Lambda functions we develop. Or it can contain a set of modules that are not available in the Lambda environment. For the demo, let us create a simple JavaScript Layer — that contains custom code as well as the axios module for invoking http requests.

    Lambda层是可以在我们开发的Lambda函数中使用的大量组织代码。 或者,它可以包含Lambda环境中不可用的一组模块。 对于演示,让我们创建一个简单JavaScript层-包含自定义代码以及用于调用http请求的axios模块。

    To do this, create a folder named nodejs. This folder name is sacrosanct if you are developing a JavaScript layer.

    为此,请创建一个名为nodejs的文件夹。 如果要开发JavaScript层,则此文件夹名称为sacrosanct。

    On the Linux prompt:

    在Linux提示符下:

    mkdir nodejs && cd nodejs

    Then create an Node project using npm init. Since this is a test project, we do not care for the particular package details. So we provide the -y to accept defaults

    然后使用npm init创建一个Node项目。 由于这是一个测试项目,因此我们不关心特定的软件包详细信息。 所以我们提供-y来接受默认值

    npm init -y

    Next, we install the moment.js module.

    接下来,我们安装moment.js模块 。

    npm install --save moment

    This generates a folder in there — node_modules. If you peep into it, you will see several folders and files that have landed in there.

    这将在其中生成一个文件夹-node_modules。 如果您窥视它,将会看到其中几个文件夹和文件。

    Next, we create a small JavaScript code — custtom.js

    接下来,我们创建一个小JavaScript代码-custtom.js

    moment=require("moment");exports.now = () => { console.log(moment().format()) };

    Add this into the node_modules folder. With all this in place, we zip the entire nodejs folder — where all this code was added.

    将此添加到node_modules文件夹中。 完成所有这些操作后,我们将整个nodejs文件夹压缩(在其中添加了所有这些代码)。

    And there.. we have created the Lambda Layer. Next, we have to push it into our AWS account.

    在那里,我们创建了Lambda层。 接下来,我们必须将其推送到我们的AWS账户中。

    We can create the lambda layer via the web console, or simple command line script. Doing it on the web console is simpler for newbies. But if you are serious about AWS, you have to break the barrier and get down to coding rather than configuring on UI. We will check out either of them here.

    我们可以通过Web控制台或简单的命令行脚本创建lambda层。 对于新手来说,在Web控制台上执行此操作更为简单。 但是,如果您认真对待AWS,则必须打破障碍,着手进行编码,而不是在UI上进行配置。 我们将在这里签出他们中的任何一个。

    使用AWS Web Console (Using the AWS Web Console)

    To start with, open the AWS Lambda dashboard with your account. On the left side, you will see options to view the Dashboard, Applications, Functions and last in the list is Layers. Click on that.

    首先,使用您的账户打开AWS Lambda仪表板 。 在左侧,您将看到用于查看仪表盘,应用程序,功能的选项,并且列表中的最后一个是“图层”。 点击那个。

    That will open the layers page. Will show you something like this:

    这将打开图层页面。 将显示以下内容:

    Now, click on the Create layer button

    现在,单击“创建图层”按钮

    And it will open a menu like this

    它会打开一个像这样的菜单

    We give it a good name and upload the zip file. We also need to choose the compatible runtime. In this case, we can pick the available nodejs runtimes. And finally we click Create.

    我们给它起一个好名字,然后上传zip文件。 我们还需要选择兼容的运行时。 在这种情况下,我们可以选择可用的nodejs运行时。 最后,我们单击创建。

    There, our lambda layer is created.

    在那里,创建了我们的lambda层。

    在命令行上 (On the Command Line)

    Console is fine. But it is not always good. In real production deployments, we expect hundreds (if not thousands) of such artifacts to be deployed. We cannot go on creating them one by one. Apart from being tedious and time consuming, it is also error prone. Source code is fast and repeatable. So we write a script to configure our layer. It is just a few lines of code, if you have the AWS CLI installed on your machine, just invoke one line of code to create a new Lambda layer out of this zip file.

    控制台很好。 但这并不总是很好。 在实际的生产部署中,我们期望部署数百(如果不是数千)此类工件。 我们不能一一创建它们。 除了繁琐和耗时之外,它还容易出错。 源代码是快速且可重复的。 因此,我们编写了一个脚本来配置我们的图层。 只需几行代码,如果您在计算机上安装了AWS CLI,则只需调用一行代码即可从该zip文件中创建一个新的Lambda层。

    aws lambda publish-layer-version --layer-name learnlayer --compatible-runtime nodejs12.x --zip-file fileb://layer.zip

    There, the lambda layer is published.

    在那里,lambda层被发布。

    Note, that Lambda Layers are always published with a version number. And the layer cannot be changed after it is published. If we modify it, we have to publish a new version number.

    请注意,Lambda图层始终使用版本号发布。 发布后,该层将无法更改。 如果我们修改它,我们必须发布一个新的版本号。

    This is very thoughtful. A layer is used in several lambda functions at a time. We do not want to surprise anyone when we modify the layer for a particular requirement. But, that is equally irritating when we actually want to modify the code for all the Lambda functions that use the layer.

    这是非常周到的。 一次一次在多个lambda函数中使用一个图层。 当我们针对特定需求修改图层时,我们不想让任何人感到惊讶。 但是,当我们实际上要为使用该层的所有Lambda函数修改代码时,这同样令人恼火。

    There are ways to work around it. We will check it in a section below.

    有一些方法可以解决它。 我们将在下面的部分中对其进行检查。

    使用Lambda层 (Using the Lambda Layer)

    Once we have the layer in place, we need to configure the Lambda function to use it. Once again, we have two options — to use the command line or the AWS Web Console.

    图层到位后,我们需要配置Lambda函数以使用它。 再次,我们有两个选项-使用命令行或AWS Web Console。

    Let us start with the web console.

    让我们从Web控制台开始。

    使用AWS Web Console (Using the AWS Web Console)

    If you are reading this blog and reached upto this point, I am sure you know what is a Lambda function, and how to create it — at least on the web console. So I will skip those details. Once you have the lambda configured, you can add this layer into it

    如果您正在阅读此博客并且到现在为止,我相信您至少在Web控制台上知道什么是Lambda函数以及如何创建它。 因此,我将跳过这些细节。 一旦配置了lambda,就可以将其添加到其中

    Click on the Layers in the Lambda configuration. That will show you the layer configuration, and a button to Add Layer. Click on that.

    单击Lambda配置中的“层”。 这将显示您的图层配置,以及添加图层的按钮。 点击那个。

    You will be taken to the page to choose the layer. You have options there, to choose the right layer from different sources. In this case, we will choose the Custom Layer. Because that is what we have. We see a dropdown — showing the layer we just created.

    您将被带到页面来选择图层。 您可以在那里选择,以从不同来源选择合适的图层。 在这种情况下,我们将选择“自定义层”。 因为那就是我们所拥有的。 我们看到一个下拉列表-显示我们刚刚创建的图层。

    Choose the lambda layer you just created and click on Add. There, you have configured the Lambda Function to use the new layer.

    选择您刚刚创建的lambda层,然后单击Add。 在那里,您已将Lambda函数配置为使用新层。

    在命令行上 (On the Command Line)

    Command line makes this very simple. We just modify an existing lambda function to add the new layer to it.

    命令行使这一过程非常简单。 我们只需修改现有的lambda函数即可向其中添加新层。

    aws lambda update-function-configuration --function-name TestLambda --layers arn:aws:lambda:us-east-1:869871132949:layer:learnlayer:1

    Lambda函数 (Lambda Function)

    To use this layer, we can have a simple code in the Lambda Function

    要使用此层,我们可以在Lambda函数中使用一个简单的代码

    const moment = require("moment");const custom = require("custom");exports.handler = async (event, context) => { console.log(moment().format()); custom.now(); const response = { statusCode: 200, body: JSON.stringify('Hello from Lambda!'), };return response;};

    The code uses the custom JavaScript code as well as the moment.js module that we had installed. Both work perfectly as expected and we get his in the logs

    该代码使用自定义JavaScript代码以及我们已安装的moment.js模块。 两者均按预期工作,我们将其记录在案

    2020-09-10T19:20:03.267Z b36bcc48-9b66-4437-82f8-4c4fe9459e2e INFO 2020-09-10T19:20:03+00:002020-09-10T19:20:03.267Z b36bcc48-9b66-4437-82f8-4c4fe9459e2e INFO 2020-09-10T19:20:03+00:00

    There! You just created a fully functional Lambda layer and used it in a Lambda Function. Of course, a real life Lambda Layer and a Lambda function would have a lot more functionality than this.

    那里! 您刚刚创建了一个功能齐全的Lambda图层,并将其用于Lambda函数中。 当然,现实生活中的Lambda层和Lambda函数将具有更多的功能。

    But the core concept remains the same. You can expand it with all your business logic.

    但是核心概念保持不变。 您可以使用所有业务逻辑进行扩展。

    Original: https://thewiz.net/aws_lambda___enhance_the_functionality_with_lambda_layers_1816.html

    原文: https : //thewiz.net/aws_lambda___enhance_the_functionality_with_lambda_layers_1816.html

    翻译自: https://medium.com/swlh/aws-lambda-enhance-the-functionality-with-lambda-layers-1f68b47c19e6

    aws lambda

    相关资源:四史答题软件安装包exe
    Processed: 0.011, SQL: 8