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
93
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
MIERUNE BBQにおけるユーザー中心設計()
mierune
PRO
1
110
CSC307 Lecture 14
javiergs
PRO
0
220
みんなのオブザーバビリティプラットフォームを作ってるんだがパフォーマンスがやばい #mackerelio #srenext
ne_sachirou
0
380
ドメイン駆動設計の実践
masuda220
PRO
19
5.2k
Microservices rules (July 2024) : what good looks like
cer
PRO
0
1.6k
はしめてのプログラミングとロボット制御
watawatavoltage
0
290
日付と正規化
megmogmog1965
0
140
企業向け生成AIアプリの 開発から得られた知見
takaakikakei
0
310
CSC307 Lecture 12
javiergs
PRO
0
220
유연한 Composable 설계
l2hyunwoo
0
380
12年前の『型システム入門』翻訳の思い出話
mame
11
1.2k
Exploring the Gradually Lost Technical Skills in the Cloud Native Era
hwchiu
2
3.9k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
48
13k
Why Our Code Smells
bkeepers
PRO
332
56k
Facilitating Awesome Meetings
lara
46
5.8k
Done Done
chrislema
179
15k
5 minutes of I Can Smell Your CMS
philhawksworth
200
19k
Into the Great Unknown - MozCon
thekraken
20
1.3k
What the flash - Photography Introduction
edds
65
11k
From Idea to $5000 a Month in 5 Months
shpigford
377
46k
Adopting Sorbet at Scale
ufuk
71
8.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
248
20k
A designer walks into a library…
pauljervisheath
201
24k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
34
1.9k
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