aws lambda_您需要了解的有关AWS Lambda的所有信息

aws lambda

“Serverless” has been the buzz word for several years now, with many applications choosing to implement the serverless approach. The term originated from the idea that the infrastructure used to run your backend code does not need to be provisioned and managed by you and your team. This significantly lessens the time it takes to get your application production-ready as well as the time and effort required to maintain your infrastructure. In 2014, Amazon Web Services released a product that would eventually become a gem in the wide pool of serverless solutions; that product is known as Lambda. In this article, we’ll take a look at why Lambda is worth your attention as well as the disadvantages you’ll want to consider, we’ll walk through the most prominent features of this service and explore its inner workings.

多年来,“无服务器”一直是流行语,许多应用程序选择实施无服务器方法。 该术语源自这样的想法,即您和您的团队不需要供应和管理用于运行后端代码的基础结构。 这大大减少了使您的应用程序投入生产所需的时间,以及维护基础结构所需的时间和精力。 2014年,Amazon Web Services发布了一种产品,该产品最终将成为众多无服务器解决方案中的瑰宝。 该产品被称为Lambda。 在本文中,我们将研究为什么Lambda值得您关注以及您要考虑的缺点,我们将逐步介绍该服务的最突出特征并探索其内部工作原理。

它是什么? (What is it?)

As a brief overview, AWS Lambda is a function-based computing service that takes the efforts of provisioning and maintaining its infrastructure out of your hands. With Lambda, you don’t need to worry about scaling your infrastructure and removing unnecessary resources as this is all handled for you. We’ll take a deeper dive into how this service works, but first let’s take a look at why this tool is a worthy addition to your stack.

简要概述一下,AWS Lambda是一项基于功能的计算服务,使您无需手动配置和维护其基础架构。 使用Lambda,您无需担心扩展基础架构和删除不必要的资源的麻烦,因为这一切都为您处理。 我们将更深入地研究此服务的工作方式,但首先让我们看一下为什么该工具值得您添加。

优点 (Advantages)

Many of the advantages of using AWS Lambda relates to the advantages of adopting the serverless-approach in general. As mentioned in the intro, a major benefit of going serverless is the time and effort saved from creating and maintaining your infrastructure. AWS provisions and manages the infrastructure your Lambda functions run on, scales the instances to handle times of excessive load, and implements proper logging and error handling. Anyone that’s been involved in the creation or maintenance of infrastructure will understand the gravity of this advantage. Not only is there a large amount of time involved in building a system that suits the needs of your application, there is also a considerable amount of time required to maintain that system as your application evolves. Time saved means quicker time to market for your application, greater agility as your team is able to most faster, and more time spent on more important tasks such as bug fixes or new features.

通常,使用AWS Lambda的许多优势与采用无服务器方法的优势有关。 正如介绍中所提到的,无服务器的主要好处是节省了创建和维护基础架构的时间和精力。 AWS会配置和管理您的Lambda函数运行的基础架构,扩展实例以处理过多负载的时间,并实施适当的日志记录和错误处理。 参与基础结构创建或维护的任何人都将了解此优势的重要性。 构建适合您应用程序需求的系统不仅需要花费大量时间,而且随着应用程序的发展,维护该系统也需要大量时间。 节省时间意味着可以更快地将应用程序推向市场,因为团队可以更快地实现更大的敏捷性,并且将更多的时间花费在更重要的任务上,例如错误修复或新功能。

As for why AWS Lambda is one of the most popular serverless solutions, AWS has done a very good job of ensuring Lambda accommodates for applications at scale as well as applications in early stages. For applications with large amounts of load, AWS allows you to run your Lambda function simultaneously with other Lambda functions; meaning, you won’t need to worry about clogged up queues. Not only that, multiple instances of the same Lambda function can be provisioned and run at the same time. Both advantages ensures that no matter how much load your application is under, Lambda will be able to handle it. Another advantage of using AWS Lambda is that you only pay for what you need; accomodating for applications that are not yet at scale or have widely differing loads. AWS charges you for the number of requests your Lambda functions recieve and the time it takes to execute those requests per 100ms. Despite its wide array of advantages, there isn’t a single solution that exists without its share of disadvantages and AWS Lambda is no exception.

关于AWS Lambda为什么是最受欢迎的无服务器解决方案之一的原因,AWS在确保Lambda适应大规模应用程序以及早期应用程序方面做得非常出色。 对于具有大量负载的应用程序,AWS允许您同时运行Lambda函数和其他Lambda函数。 意思是,您无需担心队列阻塞。 不仅如此,可以供应同一Lambda函数的多个实例,并同时运行它们。 这两个优点确保了无论您的应用程序承受多少负载,Lambda都将能够处理它。 使用AWS Lambda的另一个优势是,您只需要支付所需的费用即可。 适用于尚未规模化或负载差异很大的应用。 AWS向您收取Lambda函数收到的请求数以及每100毫秒执行这些请求所花费的时间。 尽管有很多优点,但没有一个解决方案没有缺点,而且AWS Lambda也不例外。

缺点 (Disadvantages)

Moving the task of maintaining your infrastructure away from your team and in the hands of a provider results in less control and flexibility, which is the biggest disadvantage of the serverless approach. On top of that, services that help implement the serverless approach come with their own set of infrastructure-related limitations; in Lambda’s case, these limitations are the following:

将维护基础架构的任务从团队移交给供应商,会导致控制和灵活性降低,这是无服务器方法的最大缺点。 最重要的是,有助于实施无服务器方法的服务具有与基础架构相关的自身限制。 在Lambda的情况下,这些限制如下:

  • Functions will timeout after 15 minutes.

    功能将在15分钟后超时。
  • The amount of RAM available ranges from 128MB to 3008MB with a 64MB increment between each option.

    可用的RAM量范围从128MB到3008MB,每个选项之间的增量为64MB。
  • The Lambda code should not exceed 250mb in size, and the zipped version should be no larger than 50mb

    Lambda代码的大小不应超过250mb,压缩版本的大小不应超过50mb
  • There is a limit of 1,000 requests that can run concurrently, any request above this limit will be throttled and will need to wait for other functions to finish running.

    最多可同时运行1,000个请求,超过该限制的任何请求都将受到限制,并且需要等待其他功能完成运行。

Whether or not these limitations will impact your application is dependant on the nature of your Lambda functions; usually, the solution is to refactor your Lambda functions to improve their efficiency. If any of these limitations begin to impact your Lambda functions, the first thing to do is to investigate why and whether your functions could be improved. For example, is the reason your function is timing out is because there’s inefficient algorithms involved? Are there any unnecessary dependencies in your Lambda code, causing its size to exceed the limit?

这些限制是否会影响您的应用程序,取决于Lambda函数的性质。 通常,解决方案是重构Lambda函数以提高其效率。 如果这些限制中的任何一项开始影响Lambda函数,则要做的第一件事就是研究为什么以及是否可以改进您的函数。 例如,您的函数超时的原因是因为所涉及的算法效率低下吗? 您的Lambda代码中是否有任何不必要的依赖关系,导致其大小超出限制?

Cost was mentioned in our list of advantages, but although you only pay for what your application requires this does not necessarily result in a cost effective solution; during times of high load, the cost of the same infrastructure on AWS EC2 or other services may be cheaper. The price of other services based on your application’s needs should be considered especially if your application experiences consistently high load. The final disadvantage worth mentioning is the small latency time between when an event occurs and when the function runs. This small latency times only occurs in some cases — during a cold start. In most cases, these latency times are so miniscule that it’s hardly an issue but it’s still worth considering if your application is already bordering towards potential load problems; I’ll talk about cold starts in more detail in a later section.

在我们的优势列表中提到了成本,但是尽管您只为应用程序所需的费用付费,但这并不一定会带来具有成本效益的解决方案。 在高负载期间,AWS EC2或其他服务上相同基础架构的成本可能会更便宜。 应特别考虑如果您的应用程序始终承受高负载,则应根据您的应用程序的需求考虑其他服务的价格。 值得一提的最后一个缺点是事件发生与函数运行之间的等待时间短。 这种小的等待时间仅在某些情况下发生-在冷启动期间。 在大多数情况下,这些延迟时间是如此之小,以至于几乎没有问题,但是仍然值得考虑一下您的应用程序是否已经面临潜在的负载问题。 我将在后面的部分中详细讨论冷启动。

pros and cons of lambda

它是如何工作的? (How does it work?)

If you’ve decided that Lambda may be a worthy addition to your stack, the next step is to understand the inner workings of a Lambda function. On a very basic level, serverless applications are made up of 2 or 3 components; these are event sources, functions and (in some cases) services. An event source encapsulates anything that can invoke a function, such as uploads to an S3 bucket, changes to data state or requests to an endpoint. When any one of the designated events occurs, your Lambda function will run in its own container. The resources allocated to that container and the number of containers used is determined by the size of the data and the computational requirements of the function, this is all handled by AWS. Once the request is completed, your Lambda function will either return a result back to the invocation source or a connected service, or it could make changes to a connected service (such as a database).

如果您确定Lambda可能是您的堆栈中值得添加的东西,那么下一步就是了解Lambda函数的内部工作原理。 从根本上讲,无服务器应用程序由2或3个组件组成; 这些是事件源,功能和(在某些情况下)服务。 事件源封装了可以调用功能的所有内容,例如上载到S3存储桶,更改数据状态或向端点发出请求。 当发生任何指定事件时,您的Lambda函数将在其自己的容器中运行。 分配给该容器的资源和使用的容器数量由数据大小和函数的计算要求确定,所有这些都由AWS处理。 请求完成后,您的Lambda函数将结果返回给调用源或连接的服务,或者可以对连接的服务(例如数据库)进行更改。

components of a serverless application

Before you can run a Lambda function, you’ll need to create one and to successfully do so, you’ll need a basic understanding of what’s involved. A Lambda function consists of 3 or 4 parts; the handler function, the event object, the context object and in some cases a callback function. The handler function is the function that will be executed upon invocation, this can either be async or non-async. Asynchronous functions take an event object and a context object whereas non-asynchronous functions take both these objects and a callback function. The event object contains data that was sent when the function’s event was triggered, this includes information such as the request body and the uri; the data that is passed through depends on the invocation service. The context object contains runtime information such as the function name, function version and log group. The callback function is only passed through to synchronous handlers and it takes two arguments: an error and a response. Once the Lambda function is created and pushed up to AWS, it is compressed along with its dependencies and stored in an S3 bucket.

在运行Lambda函数之前,您需要创建一个函数并成功创建一个函数,您需要对所涉及的内容有基本的了解。 Lambda函数由3或4部分组成; 处理函数,事件对象,上下文对象以及某些情况下的回调函数。 处理程序函数是将在调用时执行的函数,它可以是异步的也可以是非异步的。 异步函数采用事件对象和上下文对象,而非异步函数采用这些对象和回调函数。 事件对象包含触发函数事件时发送的数据,其中包括诸如请求正文和uri的信息。 传递的数据取决于调用服务。 上下文对象包含运行时信息,例如函数名称,函数版本和日志组。 回调函数仅传递给同步处理程序,它带有两个参数:错误和响应。 一旦创建Lambda函数并将其推送到AWS,它就会连同其依赖项一起压缩并存储在S3存储桶中。

For non-async handlers, function execution continues until the event loop is empty or the function times out.
对于非异步处理程序,函数将继续执行,直到事件循环为空或函数超时为止。

层数 (Layers)

Once you start to build your Lambda functions you’ll notice that there’s pieces of logic that could be shared between multiple functions, this is when layers can come in handy. Layers allow you to reuse code across several functions without needing an additional invocation. Once you’ve identified a piece of code that could be reused, implement it as a layer and attach it to the functions that need it. A layer is created in the same fashion as a Lambda function, with slight configuration changes which depend on the method you’ve chosen to deploy your functions (via AWS’s GUI, using the serverless framework, or AWS’s CLI tool). This is also true for adding a layer to a function; it can easily be done via AWS’s GUI, by adding a few extra lines to the serverless.yml file if you’re using the serverless framework, or with a single command using their CLI tool. The use of layers can help improve the maintainability and cleanliness of your Lambda functions as you’ll be able to significantly lessen the amount of code necessary for each function as well as the amount of duplicate code across your application.

一旦开始构建Lambda函数,您会发现在多个函数之间可以共享逻辑,这时可以派上用场了。 层使您可以跨多个功能重用代码,而无需额外的调用。 一旦确定了可以重用的代码段,就将其实现为一层并将其附加到需要它的功能上。 层以与Lambda函数相同的方式创建,仅需进行少量配置更改,具体取决于您选择的用于部署功能的方法(通过AWS的GUI,使用无服务器框架或AWS的CLI工具)。 对于向函数添加层也是如此。 您可以通过AWS的GUI轻松完成此操作,如果您使用的是无服务器框架,则可以在serverless.yml文件中添加一些额外的行,或者使用其CLI工具使用一个命令即可。 分层的使用可以帮助改善Lambda函数的可维护性和整洁度,因为您将可以大大减少每个函数所需的代码量以及整个应用程序中重复的代码量。

版本和别名 (Versioning and Aliases)

AWS not only allows you to save different versions of your Lambda functions but also allows these versions to coexist and run at the same time, this gives consumers of your functions the flexibility to upgrade to newer versions as they please. Aliases are used as a pointer to a particular version of a Lambda function, there is a long list of use cases in which they can be utilized in; two of which is worth mentioning. Firstly, instead of updating the version of a function everywhere it’s called, you could use an alias in these areas and update the version that the alias points to. Secondly, aliases have the ability to point to two versions and give you the flexibility to determine the percentage of traffic to be sent to each version. This can be very useful if you and your team wanted to test a new version of a function with a small percentage of your traffic before releasing the new version universally.

AWS不仅允许您保存Lambda函数的不同版本,而且允许这些版本共存并同时运行,这使函数的使用者可以灵活地随意升级到较新版本。 别名用作Lambda函数特定版本的指针,在很多种用例中都可以使用它们。 其中两个值得一提。 首先,可以在这些区域中使用别名并更新该别名指向的版本,而不是在调用该函数的任何地方都更新该版本。 其次,别名具有指向两个版本的能力,使您可以灵活地确定要发送到每个版本的流量的百分比。 如果您和您的团队希望在通用发行新功能之前用很少的流量来测试该功能的新版本,这将非常有用。

alias use cases

权限 (Permissions)

Relatively speaking, your Lambda functions are considerably secure by default; your function can’t talk to other services nor can it be invoked by any client, you’ll have to enable it to do so. Permissions surrounding your Lambda functions fall into two buckets: execution policies and resource-based policies. Execution policies determine which services and resources a Lambda function has access to, as well as which event sources can trigger a Lambda function to run. Resource-based policies grant other accounts and AWS services access to your Lambda resources, these include functions, versions, aliases and layer versions.

相对而言,默认情况下,您的Lambda函数相当安全。 您的函数不能与其他服务通信,也不能被任何客户端调用,您必须启用它才能这样做。 Lambda函数的权限分为两个部分:执行策略和基于资源的策略。 执行策略确定Lambda函数可以访问哪些服务和资源,以及哪些事件源可以触发Lambda函数运行。 基于资源的策略向其他帐户和AWS服务授予对Lambda资源的访问权限,其中包括函数,版本,别名和层版本。

弹性 (Resilience)

AWS helps to ensure that your Lambda function is able to handle faults without impacting your entire application using a set of features they’ve included into Lambda. The most notable features have already been mentioned in this article and those are Lambda’s scalability, versioning and ability to run concurrently. A couple of other features that contribute to the service’s resilience is their use of multiple availability zones and the ability to reserve concurrency. By default, AWS runs your Lambda functions in multiple availability zones, this ensures that your functions are not impacted if a single zone is down; the same cannot be said for services such as EC2 where this behaviour must be set by the developer. With Lambda, developers have the ability to set reserved concurrency for a particular function which ensures that it can always scale to (but not exceed past) a set number of concurrent invocations despite the number of requests other functions are consuming — note that AWS will still adhere to the upper limit of 1,000 requests, which means requests for other functions will be throttled.

AWS使用Lambda随附的一组功能来帮助确保Lambda函数能够处理错误而不会影响整个应用程序。 本文中已经提到了最值得注意的功能,这些功能是Lambda的可伸缩性,版本控制和并发运行的能力。 有助于服务弹性的其他几个功能是它们对多个可用性区域的使用以及保留并发的能力。 默认情况下,AWS在多个可用性区域中运行您的Lambda函数,这确保了当一个区域关闭时,您的功能不会受到影响; 对于EC2之类的服务(开发人员必须设置此行为),不能说相同的话。 使用Lambda,开发人员可以为特定功能设置保留的并发性,从而确保尽管其他功能正在消耗大量请求,但该功能可以始终扩展到(但不超过)一定数量的并发调用。请注意,AWS仍会遵守1,000个请求的上限,这意味着对其他功能的请求将受到限制。

冷启动 (Cold Starts)

Cold starts occur when a function has been idle for a long enough period of time that its container has been completely terminated. A new container is provisioned when the function is invoked resulting in a small amount of latency. At times, an idle Lambda container is available to pick up new requests; if this is the case, provisioning a new container isn’t necessary — this is called a warm start. The period of time a Lambda function can be idle for before it gets terminated isn’t well-documented but an experiment in 2017 found that most functions were terminated after 45–60 minutes of inactivity; potentially earlier if resources are needed by other customers. The amount of time it takes for a function to start up is influenced by its scripting language, whether the function is outside of a VPC (if it is, start up time will be faster), how big the package size is and how much memory is allocated to the function. Whether or not your application will likely experience cold starts depends on the amount of variation between your load levels. A fairly constant amount of load will mean that your application will require the same number of containers most of the time, which results in more warm starts as a container will likely be available for most requests.

当某个功能闲置了足够长的时间以至于其容器已完全终止时,就会发生冷启动。 调用该函数时,会配置一个新容器,从而导致少量延迟。 有时,一个空闲的Lambda容器可用来接听新的请求。 如果是这种情况,则无需配置新容器-这称为热启动 。 Lambda函数在终止之前可以处于空闲状态的时间记录不充分,但2017年的一项实验发现,大多数功能在不活动45至60分钟后即被终止; 如果其他客户需要资源,则可能更早。 函数启动所需的时间受其脚本语言,该函数是否在VPC之外(如果是,则启动时间会更快),程序包大小和内存量的影响分配给该功能。 您的应用程序是否可能会出现冷启动取决于负载水平之间的变化量。 相当恒定的负载量意味着您的应用程序大多数时候将需要相同数量的容器,这将导致启动更热烈,因为大多数请求可能都可以使用容器。

There is a great number of use cases in which AWS Lambda functions would thrive in. Tasks that are self-contained and run for a short period of time are great candidates for a Lambda function. These include processing operations (such as formatting an image), automation tasks where an entire server isn’t required at all times (such as data backups) or functions used to support static websites (such as capturing emails). If you’re in the MVP stage of an application, Lambda is a great tool to utilise to get your application market-ready quicker and test your product faster. I don’t believe serverless must be an “all or nothing” approach; for example, it is very reasonable for teams to use services such as EC2 throughout most of their application while using Lambda in other areas (such as their landing page or other marketing efforts). Which ever use case it may be for you, I hope this article has convinced you to add AWS Lambda into your list of possible tools to utilise.

在许多使用案例中,AWS Lambda函数都将蓬勃发展。自包含且可在短时间内运行的任务是Lambda函数的最佳候选者。 其中包括处理操作(例如,格式化图像),自动化任务(其中不需要始终使用整个服务器)(例如,数据备份)或用于支持静态网站的功能(例如,捕获电子邮件)。 如果您处于应用程序的MVP阶段,则Lambda是一个很好的工具,可用于使您的应用程序更快进入市场,并更快地测试产品。 我不认为无服务器一定不是“全有还是全无”的方法。 例如,团队在整个应用程序的大部分时间内都使用EC2之类的服务,而在其他领域(例如其目标网页或其他营销工作)中使用Lambda是非常合理的。 希望它适合您的哪种用例,我希望本文已经说服您将AWS Lambda添加到可能使用的工具列表中。

资源资源 (Resources)

翻译自: https://medium.com/swlh/everything-you-need-to-know-about-aws-lambda-e0b214224b0f

aws lambda

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值