Upgrade to Pro — share decks privately, control downloads, hide ads and more …

re:Growth 2023 - Do pythons dream of “Ferris th...

Avatar for Kazuhiro Sasaki Kazuhiro Sasaki
December 26, 2023
290

re:Growth 2023 - Do pythons dream of “Ferris the Crab” ?

Rust勢注目セッションだった「“Rustifying” serverless: Boost AWS Lambda performance with Rust (COM306)」の内容を振り返りつつ、手元で動かしてみた様子をお話しします

https://classmethod.connpass.com/event/303468/

Avatar for Kazuhiro Sasaki

Kazuhiro Sasaki

December 26, 2023
Tweet

Transcript

  1. これから話すこと “Rustifying” serverless: Boost AWS Lambda performance with Rust (COM306),

    Efi Merdler-Kravitz Rust, renowned for its performance and security, brings considerable benefits to AWS Lambda applications. In this session, discover how to deploy Rust functions using AWS SAM and cargo-lambda, facilitating a smooth development process from your local machine. Explore how to integrate Rust into Python Lambda functions effortlessly using tools like PyO3 and maturin, along with the AWS SDK for Rust. Uncover how Rust can optimize Lambda functions, including the development of Lambda extensions, all without requiring a complete rewrite of your existing code base. • Pythonで実装していたAPI Gateway + LambdaのAPIの一部をRustで置き換えたら良い感じに速くなってコ ストも下がった • レイテンシは最大で4分の1、コストは最大3分の1 くらいになった • PyO3、 maturin、 Cargo Lambdaを使うと苦労せずに実装できるよ 紹介されていたサンプルアプリケーションの内容を詳しく見ていく。 実際に動作させたり、パフォーマンス測定してみる。
  2. セッションの概要 Rust, renowned for its performance and security, brings considerable

    benefits to AWS Lambda applications. In this session, discover how to deploy Rust functions using AWS SAM and cargo-lambda, facilitating a smooth development process from your local machine. Explore how to integrate Rust into Python Lambda functions effortlessly using tools like PyO3 and maturin, along with the AWS SDK for Rust. Uncover how Rust can optimize Lambda functions, including the development of Lambda extensions, all without requiring a complete rewrite of your existing code base. • Rustはそのパフォーマンスとセキュリティで知られており、 AWS Lambdaアプリケーションに重要な利点をもた らします。 • AWS SAMとcargo-lambdaを使用して、ローカルマシンからスムーズな開発プロセスを実現しながら、 Rust関 数をAWS Lambdaにデプロイする方法を学びます。 • PyO3やmaturinなどのツールを使用して、 AWS SDK for Rustを組み合わせることで、 RustをPython Lambda関数に簡単に統合できます。 • Rustを使用してLambda関数を最適化する方法を探り、既存のコードを完全に書き直すことなく Lambda拡張 の開発を含めて活用します。
  3. アーキテクチャ(改善後) • View S3 Buckets Lambdaのboto3 (AWS SDK for Python)をRustコードに置き換える

    a. S3 APIの呼び出しを非同期IO/並列化して高速化する • Analytics SQSへのデータ送信部分を Rust実装のExtensionに置き換える • AuthorizerをRustで完全に書き換える
  4. ほんとに? • Explore how to integrate Rust into Python Lambda

    functions effortlessly using tools like PyO3 and maturin • レイテンシは最大で4分の1、コストは最大3分の1 くらいになった
  5. ビルド&デプロイしてみよう なんかたくさん入ってる • .devcontainer/ Dev Containerの設定 ※ qemu上だとRustのビルドが重い • pyproject.toml

    Poetry(Python パッケージマネージャ) の設定 & Poet (タスクランナー) • Cargo.toml Rustのビルド定義 • samconfig.toml / template.yml SAMの設定&インフラテンプレート やること • maturin で *.whl ファイル (Rustからビルドしたのバイナリ形式の拡張モジュール) • wheel形式(*.whl)はビルド済みのバイナリ形式の拡張モジュール • Apple Silicon Mac 上だとクロスビルドできない (?) • x86_64 Linux 向けのwheelファイルをビルドするために同環境向けの Pythonが必要 • 結局 GitHub Actionsでビルド • PythonのLambda関数にwheelファイルをパッケージ • Cargo LambdaでRustのLambda関数 / Extensionのパッケージ • sam-cli を使ってデプロイ • READMEとpyproject.tomlにだいたい書いてある
  6. ベンチマーク AWS Lambda 向けのベンチマークツールを使う • コールドスタート時の所要時間 SAR-measure-cold-start • メモリ割り当て量 vs

    所要時間/コスト aws-lambda-power-tuning どちらもStep Functionsを使ってLambda関数を起動、ログを分析する OSSツール
  7. コールドスタート時の所要時間 SAR-measure-cold-start functioName memorySize # of coldStarts min p25 median

    p75 p95 max stddev s3-admin-List BucketsRust- Skixu32S4m7 7 128.00 90.00 1,025.26 1,231.88 1,258.11 1,291.68 1,354.99 1,490.81 64.70 s3-admin-List BucketsPytho n-FBl1ffItnuYj 128.00 88.00 3,692.48 4,052.09 4,128.15 4,183.66 4,254.29 4,311.06 116.21
  8. まとめ • (クロスビルドできないところ以外は)たしかに effortlessだった • コストもパフォーマンスも改善していた • このサンプル以外のケースで同じ結果が得られるか ? •

    効きそうなケース • CPUバウンドなワークロード • 大量の非同期I/O (tokioが速い) • 効果なさそう • レイテンシが大きいI/Oバウンド • 既にネイティブコード化されている (numpyとか)