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
1
250
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
SPIFFE Meetup Tokyo #2 LT: Envoy SDS
taiki45
0
570
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.2k
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
Building and operating service mesh at mid-size company
taiki45
2
2.2k
Observability, Service Mesh and Microservices
taiki45
25
6.2k
Other Decks in Programming
See All in Programming
高品質と高スピードを両立させるテストアプローチ/Test Approach that Improves Quality and Agility Together
goyoki
12
8.4k
CSC307 Lecture 04
javiergs
PRO
0
220
Converting AST
yui_knk
4
170
並行処理を学びGuzzleと仲良くなる
shimabox
2
420
Get started with Compose Multiplatform!
ogi2ogi
0
1.2k
プログラミングスクールのつながりがキャリアにつながるのかもしれない話 / Connections at programming schools may lead to a career
aiandrox
0
290
あらゆるアプリをCompose Multiplatformで書きたい! -ネイティブアプリの「あの機能」を私たちはどう作るか-
subroh0508
1
940
Mastering Developer Experience: A Roadmap for Success 【開発生産性Conference 2024】
findyinc
0
190
フレームワークを10年以上開発する中で培ってきた単体テストのプラクティス / JJUG CCC 2024 Spring
kiwasaki
1
130
Fluent UI Blazor 最新Update
tomokusaba
1
160
Using Next.js as a full-stack framework / Next.jsをフルスタックフレームワークとして使ってみた
mongolyy
PRO
1
170
Tap it! Shake it! Fling it! Sheep it! The Gesture Animations Dance!
kwakeuijin
0
140
Featured
See All Featured
Optimizing for Happiness
mojombo
372
69k
Visualization
eitanlees
137
14k
4 Signs Your Business is Dying
shpigford
177
21k
Facilitating Awesome Meetings
lara
44
5.8k
Producing Creativity
orderedlist
PRO
339
39k
Happy Clients
brianwarren
94
6.5k
Robots, Beer and Maslow
schacon
PRO
155
8k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
661
120k
Stop Working from a Prison Cell
hatefulcrawdad
266
20k
Designing the Hi-DPI Web
ddemaree
276
33k
Atom: Resistance is Futile
akmur
260
25k
Principles of Awesome APIs and How to Build Them.
keavy
122
16k
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