As it is widely known, Lambda functions and large runtimes are not an ideal match due to slower cold start times and higher memory requirements compared to native code. On the other hand, many developers find it more efficient to develop using Java and Python rather than system languages like Rust.
众所周知,Lambda函数和大型运行时并不是理想的匹配,因为与本机代码相比,Lambda函数的冷启动时间更慢,内存需求更高。另一方面,许多开发人员发现使用Java和Python进行开发比使用Rust等系统语言更有效。
However, now there’s a way to truly “cut costs and boost efficiency” without the need to rewrite the entire Lambda function in Rust instead of Python.
然而,现在有一种方法可以真正“削减成本并提高效率”,而不需要用Rust而不是Python重写整个Lambda函数。
01 Switching to Rust: Python’s AWS Complexity Reduced, Cost Cut by 75%!
01改用Rust:Python的AWS复杂性降低,成本降低75%!
Merdler Kravitz, an engineering manager at the cloud consulting company CloudEX, provided an example of a Lambda function. This function used Python to list S3 (Simple Storage Service) buckets. It turned out that the AWS SDK Boto3 for Python was too bloated. By replacing the functions requiring Boto3 with Rust, significant benefits were achieved in terms of cost and performance, even though Python was still used for other code.
云咨询公司CloudEX的工程经理Merdler Kravitz提供了一个Lambda函数的例子。此函数使用Python列出S3(简单存储服务)存储桶。事实证明,AWS SDK Boto3 for Python过于臃肿。通过将需要Boto3的函数替换为Rust,在成本和性能方面取得了显着的优势,即使Python仍然用于其他代码。
The key to replacing Python code with Rust lies in PyO3, which provides Rust bindings for Python. Attributes in Rust code expose functions as standard Python interfaces.
用Rust替换Python代码的关键在于PyO3,它为Python提供了Rust绑定。Rust代码中的属性将函数暴露为标准Python接口。
As a result, in the example presented by Merdler Kravitz, the cold start speed improved threefold, and the memory requirements were significantly lower. For 1GB of RAM, Rust and Boto3 code had similar performance, but for 256MB of RAM, the enhanced Rust version still performed well, while the other did not.
因此,在Merdler Kravitz提出的例子中,冷启动速度提高了三倍,内存需求也显著降低。对于1GB的RAM,Rust和Boto3代码具有类似的性能,但对于256MB的RAM,增强的Rust版本仍然表现良好,而另一个则没有。
Cost Benefits of Using Rust in Lambda: An Example from re:Invent
在Lambda中使用Rust的成本效益:re:Invent的一个例子
Especially on serverless platforms, reducing the required computing resources can lower costs. “Rust is generally cheaper than Python, roughly 1/3 to 1/4 of the latter,” he claimed.
特别是在无服务器平台上,减少所需的计算资源可以降低成本。Rust通常比Python便宜,大约是后者的1/3到1/4。
At the re:Invent conference, AWS also announced that its SDK for Rust is now ready for production. Earlier in November last year, the cloud g