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
220
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
560
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.2k
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
Distributed Scheduling with Spring Boot: the challenges and pitfalls of implementing a background job
rponte
7
1.4k
Why Spring Matters to Jakarta EE - And Vice Versa
ivargrimstad
0
1.2k
詳解 "Fixing For Loops in Go 1.22" 自作linterをgolangci-lintへコントリビュートした話
qualiarts
8
750
spaCy meets LLMs: Using Generative AI for Structured Data
inesmontani
PRO
1
310
Spring Bootと行レベルセキュリティではじめるマルチテナントアーキテクチャ / Multi-tenant architecture using Row-Level Security
jaguar_imo
1
210
OpenAPI 3.1 and Spring-Boot 3 What's New?
bnasslahsen
0
170
TypeScriptを活用したi18n対応
minako__ph
8
1.5k
Ruby と Rails の小ネタ集
daisukeshinoku
2
140
コンテナと文字列の中間インタフェースspanとstring_view
faithandbrave
1
110
[KR] 2024 DroidKnights: Compose 성능 최적화를 위한 Stability 마스터하기
skydoves
0
600
Platform Engineering with Spring Boot
thombergs
0
410
ABEMA 画像配信基盤における Cloudflare Workers の活用
peaceiris
6
380
Featured
See All Featured
The Brand Is Dead. Long Live the Brand.
mthomps
51
33k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Infographics Made Easy
chrislema
238
18k
What’s in a name? Adding method to the madness
productmarketing
PRO
18
2.8k
The Cost Of JavaScript in 2023
addyosmani
24
4.1k
Happy Clients
brianwarren
92
6.5k
Bash Introduction
62gerente
606
210k
Atom: Resistance is Futile
akmur
260
25k
Facilitating Awesome Meetings
lara
44
5.7k
Art, The Web, and Tiny UX
lynnandtonic
290
19k
The Mythical Team-Month
searls
217
42k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
660
120k
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