内部类转lambda
Cloud provides an abstraction to a lot of complex stuff. And it is good, but I like the details, the internals. With an aim to see how lambda service works I watched an awesome video from AWS on the Lambda service. This post tries to summarize things.
云为许多复杂的事物提供了抽象。 很好,但是我喜欢内部细节。 为了了解lambda服务的工作原理,我观看了AWS关于Lambda服务的精彩视频。 这篇文章试图总结一下。
When we talk about lambda, the majority of us mean the function itself. But lambda is a service namespace with various resources in the scope of that namespace. The below image is a representation of the lambda service and its resources.
当我们谈论lambda时,我们大多数人指的是函数本身。 但是lambda是一个服务命名空间,在该命名空间的范围内具有各种资源。 下图是lambda服务及其资源的表示。
The reason I have listed this is due to the the fact that different resources behave differently and they combine together at times to solve the problem at hand. It's important to know which resource we are dealing with and how they behave. We all know that the lambda function can be tied to a VPC and we can enforce condition in IAM Role/Policy to only allow certain action if they are coming from within VPC or through the VPC endpoint. But the resource, Event Source Mapping, is not the same as Function, so if we want the lambda to be triggered when there is a message in the queue and we used the same restriction of endpoint it fails. As the mapping of polling is done by service not by the function itself (we’ll see below how that happens).
我之所以列出此原因,是由于以下事实:不同的资源行为不同,并且它们有时会组合在一起以解决当前的问题。 了解我们正在处理的资源以及它们的行为方式非常重要。 我们都知道lambda函数可以绑定到VPC,并且我们可以强制执行IAM角色/策略中的条件,以仅允许某些操作(如果这些操作来自VPC或通过VPC端点)。 但是资源(事件源映射)与函数不同,因此,如果我们希望在队列中有消息时触发lambda,并且我们使用相同的终结点限制,则会失败。 由于轮询的映射是由服务而不是函数本身完成的(我们将在下面看到情况如何)。
Event Source Mapping — Creates a mapping between an event source and an AWS Lambda function. Lambda(service) reads items from the event source and triggers the function(resource).
事件源映射-在事件源和AWS Lambda函数之间创建映射。 Lambda(service)从事件源读取项目并触发function(resource) 。
Layer — A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. This can be reused across function and hence reduces the function’s deployment package.
层—层是一个ZIP归档文件,其中包含库,自定义运行时或其他依赖项。 可以跨功能重复使用此功能,因此可以减少功能的部署包。
Function — This is the beast that we will tame in further.
功能-这是我们将进一步驯服的野兽。
Permission — Its a resource policy which say’s who can access the function.
权限-它是一项资源策略,说明谁可以访问该功能。
Version — Its a snapshot of the the function code, runtime, environment variables. This helps us in having multiple versions of same function and do testing.
版本—它是功能代码,运行时,环境变量的快照。 这有助于我们拥有相同功能的多个版本并进行测试。
Alias — A Lambda alias is a pointer to a specific function version
别名-Lambda别名是指向特定功能版本的指针
Event Destination — This allows us to send asynchronous invocation record to another service with request and response in json format
事件目标-这使我们可以将请求和响应以json格式发送到另一个服务以异步调用记录
So now we know what other resources are there in Lambda service and what they do. Let us move to function itself and see what it is.
因此,现在我们知道Lambda服务中还有哪些其他资源以及它们的作用。 让我们继续发挥作用,看看它是什么。
So when we create a lambda function we supply the runtime (python or Go or Node.js). We also mention an entry point for our code generally its called handler. In python, the definition looks like below.
因此,当我们创建lambda函数时,我们会提供运行时(python或Go或Node.js)。 我们还提到了代码的入口点,通常称为处理程序。 在python中,定义如下所示。
def lambda_handler(event, context):
def lambda_handler(事件,上下文):
Here event and context are two parameters. The event is the payload as a python dictionary object. Context is the details of the runtime and initialization objects. Context is available between invocations. This is an instance of LambdaContext class defined in /var/runtime/awslambda/bootstrap.py. Before we see the folders of interest in the runtime, let us see how a function is organized from infra wise. There has been a change in this from 2018 to 2019 as we now know that firecracker does the heavy lifting.
这里,事件和上下文是两个参数。 事件是作为python字典对象的有效负载。 上下文是运行时和初始化对象的详细信息。 调用之间可以使用上下文。 这是在/var/runtime/awslambda/bootstrap.py中定义的LambdaContext类的实例。 在我们看到运行时感兴趣的文件夹之前,让我们看一下如何从下面组织函数。 从2018年到2019年,这种情况发生了变化,因为我们现在知道鞭炮可以完成繁重的工作。
From the above image, we can infer that there are two levels of isolation that is provided. One by guest OS and another by KVM and Firecracker(which does the hardware emulation). It utilises cgroups, namespaces, seccomp-bpf, iptables, and chroot. Also, we see lambda does run on EC2. Each function gets its own runtime, guest kernel, and Firecracker instance. While the underlying instance is shared.
从上面的图像中,我们可以推断出提供了两个隔离级别。 一个是由来宾操作系统提供的,另一个是由KVM和Firecracker (由其进行硬件仿真)提供的。 它利用cgroups,名称空间,seccomp-bpf,iptables和chroot。 此外,我们看到lambda确实可以在EC2上运行。 每个函数都有自己的运行时,来宾内核和Firecracker实例。 而基础实例是共享的。
OK, so we have the function created. Function creation and updating is control plane API. Lambda offers a different data plane api to invoke the lambda. Let's see what happens when we invoke the function. We see a response :). Cool what happens behind the scene. A micro VM is provisioned, the runtime is loaded, ENVs are set, function code is downloaded to /var/task. And then its executed. It seems simple, lets understand how AWS does that with the below diagrams.
好的,我们已经创建了函数。 功能创建和更新是控制平面API。 Lambda提供了不同的数据平面api来调用lambda。 让我们看看调用函数时会发生什么。 我们看到一个响应:)。 很酷,幕后发生了什么。 设置了微型VM,已加载运行时,已设置ENV,功能代码已下载到/ var / task。 然后执行它。 看起来很简单,下面的图表让我们了解AWS如何做到这一点。
Invocation flow for a Request-Response call.
请求-响应调用的调用流程。
Load balancer — distributes traffic to Front end servicesFront End Invoker — Authentication, Load Environment variable and metadata, Asks worker manager to reserve workerCounter Service — Confirm the concurrency, Quorum based, Multi AZWorker Manager — Manages resources, remove idle workersWorker — Container for Sandboxes, downloads function code, allocates memory, agent processing, reports its status to Placement ServicePlacement Service — Spin up new sandboxes, Monitors workers health
负载均衡器-将流量分配到前端服务前端调用程序-身份验证,负载环境变量和元数据,要求工作人员管理器保留工作人员计数器服务-确认并发,基于仲裁的多可用区工作人员管理器-管理资源,删除空闲工作人员沙箱容器,下载功能代码,分配内存,代理处理,将其状态报告给Placement Service Placement Service —启动新的沙箱,监视工作人员的健康状况
Invocation flow for an Asynchronous call from Event.
来自事件的异步调用的调用流。
State Manager/Stream Tracker — Manages Pollers and Events/stream resourcesPoller — consumes the message, retrieves event destination, trigger sync invoke, creates checkpoints, sends back completion data, batching, shard subscriptionLeasing Service — Assigns pollers to workersEvent Destination — Sends invocation request response to another service
状态管理器/流跟踪器-管理轮询器和事件/流资源轮询器-消费消息,检索事件目的地,触发同步调用,创建检查点,发送回完成数据,批处理,分片订阅租赁服务-将轮询器分配给工作人员事件目的地-发送调用请求对另一个服务的响应
Invocation flow of calls from Stream-based services.
来自基于流的服务的调用的调用流。
So now we know how internally lambda resources are provisioned. Let’s see some other details. We get a 512 MB space with our runtime. This is available to us at /tmp. This allows us to write to this file system. If we want we can install our package and make use of that. The below code installs boto3 at /tmp and then does some processing on that.
因此,现在我们知道如何内部配置lambda资源。 让我们看看其他细节。 运行时我们获得了512 MB的空间。 您可以在/ tmp上找到它。 这使我们可以写入此文件系统。 如果我们愿意,我们可以安装我们的软件包并加以利用。 以下代码将boto3安装在/ tmp上,然后对其进行一些处理。
There are pre-installed packages in the run time we can see the list using the below code.
在运行时有预装的软件包,我们可以使用以下代码查看列表。
The output of the above code is as follows:
上面代码的输出如下:
urllib3 — 1.25.9six — 1.15.0s3transfer — 0.3.3 python-dateutil — 2.8.1 jmespath — 0.10.0idna — 2.10docutils — 0.15.2 chardet — 3.0.4certifi — 2020.6.20 botocore — 1.17.17 boto3 — 1.14.17setuptools — 47.1.0 pip — 20.1.1
urllib3-1.25.9six-1.15.0s3transfer-0.3.3 python-dateutil-2.8.1 jmespath-0.10.0idna-2.10docutils-0.15.2 chardet-3.0.4certifi-2020.6.20 botocore-1.17.17 boto3-1.14。 17setuptools-47.1.0点-20.1.1
With the below code snippet we can get all available env variables and its value.
通过下面的代码片段,我们可以获得所有可用的env变量及其值。
Hope this was useful.
希望这是有用的。
翻译自: https://medium.com/@asrathore08/lambda-internals-exploration-ae6c21d9521e
内部类转lambda
