Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
RustでAWS Lambda functionをいい感じに書く
Search
taiki45
May 14, 2024
Programming
2
570
RustでAWS Lambda functionをいい感じに書く
Talk at UV Study : Rust LT会#49
https://uniquevision.connpass.com/event/316789/
taiki45
May 14, 2024
Tweet
Share
More Decks by taiki45
See All by taiki45
Mocking in Rust Applications
taiki45
2
530
Error Handling in Rust Applications
taiki45
3
500
Efficient Platform for Security and Compliance
taiki45
4
1.3k
SPIFFE Meetup Tokyo #2 LT: Envoy SDS
taiki45
0
700
builderscon Tokyo 2019: Intro Service Mesh
taiki45
6
3.3k
NoOps Meetup Tokyo #7: 入門サービスメッシュ
taiki45
4
1.8k
CloudNative Days Tokyo 2019: Understanding Envoy
taiki45
3
3.4k
Cloud Native Meetup Tokyo #8 ServiceMesh Day Recap
taiki45
2
350
EnvoyCon 2018: Building and operating service mesh at mid-size company
taiki45
4
4.4k
Other Decks in Programming
See All in Programming
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
150
Djangoアプリケーション 運用のリアル 〜問題発生から可視化、最適化への道〜 #pyconshizu
kashewnuts
1
250
Writing documentation can be fun with plugin system
okuramasafumi
0
120
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
47
17k
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
190
Conform を推す - Advocating for Conform
mizoguchicoji
3
690
第3回 Snowflake 中部ユーザ会- dbt × Snowflake ハンズオン
hoto17296
4
370
Amazon ECS とマイクロサービスから考えるシステム構成
hiyanger
2
560
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
37
14k
Honoのおもしろいミドルウェアをみてみよう
yusukebe
1
210
Grafana Loki によるサーバログのコスト削減
mot_techtalk
1
130
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
470
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
328
21k
Thoughts on Productivity
jonyablonski
69
4.5k
Visualization
eitanlees
146
15k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.6k
Typedesign – Prime Four
hannesfritz
40
2.5k
It's Worth the Effort
3n
184
28k
Embracing the Ebb and Flow
colly
84
4.6k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
What's in a price? How to price your products and services
michaelherold
244
12k
Six Lessons from altMBA
skipperchong
27
3.6k
Why Our Code Smells
bkeepers
PRO
336
57k
Side Projects
sachag
452
42k
Transcript
© 2024 Finatext Ltd. RustでAWS Lambda functionを いい感じに書く 1 Finatext
@taiki45
© 2024 Finatext Ltd. • Taiki Ono • ソフトウェアエンジニア ◦
Platform engineering and developer productivity • 登壇系だと2018-2019にサービスメッシュ関連 ◦ Envoy proxy • 2022 8月~ Finatext 2 taiki45
© 2024 Finatext Ltd. 3
© 2024 Finatext Ltd. • AWSのコンピューティング方面のサーバーレスなやつ • FaaS的な 4 AWS
Lambda
© 2024 Finatext Ltd. • 手元での動作確認を手軽にできるようにしたい • テストを手軽に書けるようにしたい • 既存の便利ライブラリの恩恵を受けたい
• あわよくばLambda以外でも動かしたい 5 Lambda functionを実装する時の課題
© 2024 Finatext Ltd. • github.com/awslabs/aws-lambda-rust-runtime のREADMEはそっ閉じする • HTTP requests受ける系
◦ axumを使う ◦ towerの上に載せれるweb frameworkならなんでもOK • その他: EventBridge、SQSなどのイベント駆動 ◦ lambda_runtime::service_fn直じゃなく抽象レイヤー1枚入れる ◦ Handlerみたいな構造体にいろいろ持たせると便利 6 いい感じに実装する
© 2024 Finatext Ltd. • tower::Serviceとは ◦ Composable Service •
lambda_http::runはServiceを受け取る ◦ lambda_runtime::runも同様 • axum::RouterとかはServiceを実装している ◦ Composableだしlambda_http::runにも渡せる 7 HTTP受け取る系: tower::Service Service ::poll_ready ::call Service ::poll_ready ::call Service ::poll_ready ::call Service ::poll_ready ::call Service ::poll_ready ::call Request Response
© 2024 Finatext Ltd. • tower::httpの各種ミドルウェア ◦ ServiceBuilder<Stack<TimeoutLayer, Stack<SetResponseHeaderLayer<HeaderValue>, Stack<TraceLayer<SharedClassifier<ServerErrorsAsFailures>,
impl Fn(&Request<Body>) -> Span>, Stack<PropagateRequestIdLayer, Stack<SetRequestIdLayer<OrguReqeustIdMaker>, Stack<SetSensitiveRequestHeadersLayer, Identity>>>>>>> ◦ NormalizePath<Router> ▪ ミドルウェアスタックの型はRouterで吸収テク ▪ apply_middleware(Router) -> Router • tower::ServiceExt::oneshotでテスト実行できる • lambda_http::runに渡さず別の実行関数に渡せばLambdaの外でも動かせる ◦ axumの場合axum::serve • axumみたいなライブラリのルーティング機能を使える ◦ shared stateみたいなのも自分で実装しなくて便利 ◦ extractorみたいなのも便利 ◦ (これ以外はlambda_http::service_fnを使ってもできるはできるはず) 8 towerの上に乗るメリット
© 2024 Finatext Ltd. • lambda_runtime::service_fn直じゃなく抽象レイヤー1枚入れる ◦ Handlerみたいな構造体にいろいろ持たせると便利 • 手元で一発実行するだけのoneshotコマンドをclapとかで作ると動作確認に便利
• イベントソースからイベント受け取るようなHTTPサーバーを作ってその中で処理を動かす と、手元の動作確認やインテグレーションテスト用に便利 ◦ Lambda外で動かす道筋にもなる 9 その他: EventBridge、SQSなどのイベント駆動
© 2024 Finatext Ltd. 10 その他: Lambdaで動かす時
© 2024 Finatext Ltd. 11 その他: 手元でOneshotに動かす時
© 2024 Finatext Ltd. 12 その他: HTTP serverの裏で動かす時
© 2024 Finatext Ltd. 13 その他: Handler
© 2024 Finatext Ltd. 14 なぜそのような仕組みが動くのだろう?🤔 →→→
© 2024 Finatext Ltd. 15 完全理解: Rust with AWS Lambda
© 2024 Finatext Ltd. 16 AWS Lambda https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html
© 2024 Finatext Ltd. Container Process 17 AWS Lambda and
Rust Lambda service Lambda runtime event loop Runtime<S> ::run_with_incoming trait Service<Request> ::call service_fn(|event: LambdaEvent<T> ...) 色々 /runtime/invocation/next /runtime/invocation/ <AwsRequestId>/response polling 色々 Runtime event loopはmultiplexingしない😳
© 2024 Finatext Ltd. Container Process 18 AWS Lambda and
Rust Lambda service Lambda SDK event loop Runtime<S> ::run_with_incoming trait Service<Request> ::call service_fn(|event: LambdaEvent<T> ...) 色々 /runtime/invocation/next /runtime/invocation/ <AwsRequestId>/response polling 色々
© 2024 Finatext Ltd. 19 AWS Lambda and Rust Lambda
SDK event loop Runtime<S> ::run_with_incoming trait Service<Request> ::call service_fn(|event: LambdaEvent<T> ...) Runtime::new(handler: F) wrap_handler(handler, ... CatchPanicService::new(handler) RuntimeApiResponseService::new(safe_service) RuntimeApiClientService::new(response_service, ... RuntimeApiResponseService::call内部でLambdaInvocation (Lambda serviceからのレスポンスで実際のevent payloadが入っている)のbody をdeserializeしてevent struct Tに変換している
© 2024 Finatext Ltd. 20 AWS Lambda and Rust: HTTP
lambda_http::run(handler: Service<...>) Service<Request>::call Adapter::call(..., req: LambdaEvent<LambdaRequest>) LambdaRequest -> Request (http::Request<Body>) lambda_runtime::run(Adapter::from(handler)) Adapterに変換を任せてあとは lambda_runtime::runの上で動かす
© 2024 Finatext Ltd. • AWS Lambda functionを実装する時のデザインパターンについて紹介しました • AWS
Lambdaとaws-lambda-rust-runtimeの仕組みについてはブログ記事として投稿しま す! ◦ aws-lambda-goも扱っています • このデザインパターンを使って開発したorguというGitHub org-wide workflowsを実現する ソフトウェアをOSSとしてリリース予定なのでお楽しみに ◦ 7/9にあるPlatform Engineering Kaigiでもトーク内で扱う予定です! • 「実用Rustアプリケーション開発」というZenn本を最近書きました! ◦ https://zenn.dev/taiki45/books/pragmatic-rust-application-development • これからもおもしろいと思う情報発信をしていくのでXのフォローをよければお願いします! ◦ https://twitter.com/taiki45 Finatextはとてもいい会社かつ絶賛採用中なので興味のある方はぜひ見てみてください! https://speakerdeck.com/finatext/finatext-are-hiring-engineers 21 おわりに
© 2024 Finatext Ltd. 22