aws工具_完整的AWS开发工具包到底有多贵?

平元明
2023-12-01

aws工具

by Yan Cui

崔燕

完整的AWS开发工具包到底有多贵? (Just how expensive is the full AWS SDK?)

If you’re not familiar with how cold start works within the context of AWS Lambda, then read this post first.

如果您不熟悉冷启动在AWS Lambda上下文中的工作方式,请首先阅读此文章

When a Node.js Lambda function cold starts, a number of things happen:

当Node.js Lambda函数冷启动时,会发生许多事情:

  • the Lambda service has to find a server with enough capacity to host the new container

    Lambda服务必须找到具有足够容量的服务器来承载新容器
  • the new container is initialized

    新容器已初始化
  • the Node.js runtime is initialized

    Node.js运行时已初始化
  • your handler module is initialized, which includes initializing any global variables and functions you declare outside the handler function

    您的处理程序模块已初始化,其中包括初始化您在处理程序函数之外声明的所有全局变量和函数

If you enable active tracing for a Lambda function, you will be able to see how much time is spent on these steps in X-Ray. Unfortunately, the time it takes to initialize the container and the Node.js runtime are not recorded as segments. But you can work out from the difference in duration.

如果启用Lambda函数的主动跟踪,您将能够看到在X-Ray中这些步骤花费了多少时间。 不幸的是,初始化容器和Node.js运行时所花费的时间没有记录为段。 但是您可以从持续时间的差异中得出结论。

Here, Initialization refers to the time it takes to initialize the handler module.

这里, Initialization是指初始化处理程序模块所花费的时间。

The above trace is for the function below, which requires the AWS SDK and nothing else. As you can see, this simple require added 147ms to the cold start.

上面的跟踪适用于以下功能,该功能需要AWS开发工具包,而无需其他任何功能。 如您所见,这个简单的require为冷启动增加了147ms。

const AWS = require('aws-sdk')module.exports.handler = async () => {}

Consider this the cost of doing business when your function needs to interact with AWS resources. But, if you only need to interact with one service (e.g. DynamoDB), you can save some initialization time with this one-liner.

当您的功能需要与AWS资源进行交互时,请考虑这是开展业务的成本。 但是,如果您只需要与一项服务(例如DynamoDB)进行交互,则可以使用这种单一代码节省一些初始化时间。

const DynamoDB = require('aws-sdk/clients/dynamodb') const documentClient = new DynamoDB.DocumentClient()

It requires the DynamoDB client directly without initializing the whole AWS SDK. I ran an experiment to see how much cold start time you can save with this simple change.

它直接需要DynamoDB客户端,而无需初始化整个AWS开发工具包。 我进行了一项实验,以查看通过此简单更改可以节省多少冷启动时间。

Credit goes to my colleague Justin Caldicott for piquing my interest and doing a lot of the initial analysis.

我的同事贾斯汀·卡尔迪科特 ( Justin Caldicott)引起了我的兴趣,并做了很多初步分析,这归功于我。

In addition to the AWS SDK, we often require the XRay SDK too and use it to auto-instrument the AWS SDK. Unfortunately, the aws-xray-sdk package also has some additional baggage that we don’t need. By default, it supports Express.js apps, MySQL and Postgres. If you are only interested in instrumenting the AWS SDK and http/https modules then you only need the aws-xray-sdk-core.

除了AWS开发工具包外,我们通常也需要XRay开发工具包,并使用它来自动安装AWS开发工具包。 不幸的是, aws-xray-sdk软件包也有一些我们不需要的额外行李。 默认情况下,它支持Express.js应用程序,MySQL和Postgres。 如果您仅对检测AWS开发工具包SDK和http / https模块感兴趣,则只需要aws-xray-sdk-core

方法 (Methodology)

I tested a number of configurations:

我测试了许多配置:

Each of these functions is traced by X-Ray. Sample rate set to 100%, so we don’t miss anything. We are only interested in the duration of the Initialization segment as it corresponds to the time for initializing these dependencies.

X-Ray跟踪这些功能中的每一个。 采样率设置为100%,因此我们不会错过任何东西。 我们只对Initialization段的持续时间感兴趣,因为它对应于初始化这些依赖项的时间。

The no AWS SDK case is our control group. We can see how much time each additional dependency adds to our Initialization duration.

no AWS SDK案例是我们的控制组。 我们可以看到每个其他依赖项增加了我们Initialization时间的时间。

To collect a statistically significant sample set of data, I decided to automate the process using Step Functions.

为了收集具有统计意义的样本数据集,我决定使用“步函数”使过程自动化。

  • The state machine takes an input { functionName, count }.

    状态机接受输入{ functionName, count }

  • The SetStartTime step adds the current UTC timestamp to the execution state. This is necessary as we need the start time of the experiment to fetch the relevant traces from X-Ray.

    SetStartTime步骤将当前UTC时间戳添加到执行状态。 这是必要的,因为我们需要实验的开始时间才能从X射线中提取相关迹线。

  • The Loop step triggers the desired number of cold starts for the specified function. To trigger cold starts, I programmatically update an environment variable before invoking the function. That way, I ensure that every invocation is a cold start.

    Loop步骤触发指定功能所需的冷启动次数。 为了触发冷启动,我在调用函数之前以编程方式更新了环境变量。 这样,我确保每次调用都是冷启动。

  • The Wait30Seconds step makes sure that all the traces are published to XRay before we attempt to analyze them.

    在我们尝试对其进行分析之前, Wait30Seconds步骤确保所有跟踪已发布到XRay。

  • The Analyze step fetches all the relevant traces in XRay and outputs several statistics around the Initialization duration.

    Analyze步骤将获取XRay中的所有相关迹线,并输出有关Initialization持续时间的一些统计信息。

Each configuration is tested over 1000 cold starts. Occasionally the XRay traces are incomplete (see below). These incomplete traces are excluded in the Analyze step.

每种配置均经过1000次冷启动测试。 有时XRay迹线不完整(请参见下文)。 这些不完整的痕迹在“ Analyze步骤中被排除。

Each configuration is also tested with WebPack as well (using the serverless-webpack plugin). Thanks to Erez Rokah for the suggestion.

每个配置也都已通过WebPack(使用serverless-webpack插件)进行了测试。 感谢Erez Rokah的建议。

结果 (The Results)

These are the Initialization times for all the test cases.

这些是所有测试用例的Initialization时间。

Key observations:

关键观察:

  • WebPack improves the Initialization time across the board.

    WebPack全面缩短了Initialization时间。

  • Without any dependencies, Initialization time averages only 1.72ms without WebPack and 0.97ms with WebPack.

    没有任何依赖关系,WebPack的Initialization时间平均仅为1.72ms,WebPack的平均Initialization时间为0.97ms。

  • Adding AWS SDK as the only dependency adds an average of 245ms without WebPack. This is fairly significant. Adding WebPack doesn’t improve things significantly either.

    如果不使用WebPack,则将AWS开发工具包添加为唯一的依赖项将平均增加245毫秒。 这是相当重要的。 添加WebPack也不会明显改善。
  • Requiring only the DynamoDB client (the one-liner change discussed earlier) saves up to 176ms! In 90% of the cases, the saving was over 130ms. With WebPack, the saving is even more dramatic.

    仅需要DynamoDB客户端(前面讨论过的单线更改)最多可节省176ms! 在90%的情况下,节省时间超过130毫秒。 借助WebPack,节省的费用甚至更大。
  • The cost of requiring the XRay SDK is about the same as AWS SDK.

    要求XRay SDK的成本与AWS SDK差不多。
  • There’s no statistically significant difference between using the full XRay SDK and XRay SDK Core. With or without WebPack.

    使用完整的XRay SDK和XRay SDK Core在统计上没有显着差异。 有无WebPack。

Originally published at theburningmonk.com on March 23, 2019.

最初于2019年3月23日发布在theburningmonk.com上。

翻译自: https://www.freecodecamp.org/news/just-how-expensive-is-the-full-aws-sdk-3713fed4fe70/

aws工具

 类似资料: