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
280
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
Error Handling in Rust Applications
taiki45
0
89
Efficient Platform for Security and Compliance
taiki45
3
750
SPIFFE Meetup Tokyo #2 LT: Envoy SDS
taiki45
0
590
builderscon Tokyo 2019: Intro Service Mesh
taiki45
6
3k
NoOps Meetup Tokyo #7: 入門サービスメッシュ
taiki45
4
1.7k
CloudNative Days Tokyo 2019: Understanding Envoy
taiki45
3
3.3k
Cloud Native Meetup Tokyo #8 ServiceMesh Day Recap
taiki45
2
300
EnvoyCon 2018: Building and operating service mesh at mid-size company
taiki45
4
4.3k
Cookpad Tech Kitchen #20 クックパッドでのサービスメッシュについて
taiki45
1
2.2k
Other Decks in Programming
See All in Programming
大規模マルチテナントを解決するYugabyteDBという選択肢
nnaka2992
1
250
CSC307 Lecture 05
javiergs
PRO
0
210
DDDを志して3年経ったら「DDDの皮を被ったクリーンアーキテクチャ」になった話【デブサミ2024夏】
texmeijin
1
620
Introduction to GitOps
hwchiu
0
110
Ruby メモリ管理 プログラミング
megmogmog1965
0
130
【Go言語】golangci-lintの使い方
tomo1227
0
270
[After Kotlin Fest 2024 LT Night @ Sansan] もっともっとKotlinを好きになる!K2 Compiler Pluginで遊んでみよう!
kitakkun
2
260
Android開発者のための Kotlin Multiplatform入門
ntaro
0
190
CSC307 Lecture 10
javiergs
PRO
0
310
feature環境をGitHub ActionsとCloudFormationでいい感じに管理する
nealle
2
310
SDCon2024: Enabling DevOps and Team Topologies thru architecture: architecting for fast flow
cer
PRO
0
780
Clean Architecture by TypeScript & NestJS
ryounasso
0
150
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
321
23k
Automating Front-end Workflow
addyosmani
1362
200k
Code Review Best Practice
trishagee
58
16k
10 Git Anti Patterns You Should be Aware of
lemiorhan
652
58k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
662
120k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
The World Runs on Bad Software
bkeepers
PRO
63
11k
Agile that works and the tools we love
rasmusluckow
325
20k
Large-scale JavaScript Application Architecture
addyosmani
506
110k
How to Ace a Technical Interview
jacobian
274
23k
BBQ
matthewcrist
82
9k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
325
21k
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