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
サーバーレスなメール送信システムをAWS CDKで構築し数千人規模の環境で運用した話 / aws dev day cdk mail
Search
k.goto
November 09, 2022
Programming
3
4.7k
サーバーレスなメール送信システムをAWS CDKで構築し数千人規模の環境で運用した話 / aws dev day cdk mail
AWS Dev Day 2022 Japan登壇資料です。
k.goto
November 09, 2022
Tweet
Share
More Decks by k.goto
See All by k.goto
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
4
630
S3バケットを高速で削除・空にするツール「cls3」 / s3-deletion-tool-cls3
gotok365
4
450
AWS CDKで コンテナイメージスキャンを行う 〜ECRとその他の方法〜 / cdk-container-image-scan
gotok365
2
960
スタートアップでこそCDKが活きた〜生産性を向上できた5つの理由〜 / startup-cdk-productivity
gotok365
9
3.4k
AWS CDKで"使う"GoFデザインパターン 〜実際どうなの?〜 / GoF design patterns used with AWS CDK
gotok365
3
1k
AWS CDKで学ぶGoFデザインパターン〜IaCにもコード設計〜 / aws dev day cdk gof design patterns
gotok365
11
4.2k
CloudWatch複合アラームでELBの5XXをいい感じに検知しようとしたらうまくいかなかった話 / cloudwatch alarm elb 5xx
gotok365
0
2.5k
AWS CDKとZodを活用したバリデーションパターン集 / validation patterns with cdk and zod
gotok365
6
2.9k
App Runnerで AppでないものをRunする / run non apps with app runner
gotok365
5
2k
Other Decks in Programming
See All in Programming
Exploring Type-Informed Lint Rules in Rust based TypeScript Linters
unvalley
3
650
TypeScriptでもLLMアプリケーション開発 / LLM Application In Typescript
rkaga
5
1.3k
Namespace, What and Why
tagomoris
4
760
TypeScriptのパフォーマンス改善
yajihum
14
5.2k
The grand strategy of Ruby Parser
yui_knk
5
340
[RubyKaigi 2024] Ruby Mixology 101: adding shots of PHP, Elixir, and more
palkan
0
140
TSKaigi 2024 - 新サービス Progate Path の演習で TypeScript を採用して見えた教材観点からの利点と課題
makotoshimazu
1
230
Implementing Design Systems in Swift
seyfoyun
2
530
GitLab CI/CD で C#/WPFアプリケーションのテストとインストーラーのビルド・デプロイを自動化する
hacarus
0
620
JavaScript Closure
asoluka
0
2k
“Seeing Like a Programmer”—Resiliency, Limits, and Moral Hazards in Software Engineering (LambdaConf 2024)
chriskrycho
0
440
仕様と実装で学ぶOpenTelemetry
drumato
2
1.2k
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
649
58k
Facilitating Awesome Meetings
lara
43
5.6k
KATA
mclloyd
16
12k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
Creatively Recalculating Your Daily Design Routine
revolveconf
211
11k
The MySQL Ecosystem @ GitHub 2015
samlambert
244
12k
Code Reviewing Like a Champion
maltzj
516
39k
Pencils Down: Stop Designing & Start Developing
hursman
117
11k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
275
13k
Docker and Python
trallard
35
2.7k
Transcript
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. サーバーレスなメール送信システムを AWS CDKで構築し数千⼈規模の環境で運⽤した話 後藤 健太 2 0 2 2 . 1 1 . 0 8 - 1 0 A W S D E V D A Y J A P A N C - 5 - 1
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⾃⼰紹介 •後藤 健太 § テックリード § 365歩のテック(テックブログ) § ラーメンライター § Twitter: @365_step_tech
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アジェンダ •システム要件 (メール送信システム) •アーキテクチャ •運⽤してみた結果
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. システム要件 (メール送信システム)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. システム要件(メール送信システム) •添付ファイルあり •メールはキューイングし、送信失敗時にリトライもしたい •AWSで構築されたバックエンドからアクセスしたい § APIとしての公開でなくてもいい •いい感じにスケールしてほしい サーバーレスでいってみよう︕
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. アーキテクチャ
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ Amazon Simple Notification Service (Amazon SNS) Amazon Simple Queue Service (Amazon SQS) Amazon DynamoDB AWS Cloud Development Kit (AWS CDK) Amazon Simple Storage Service (Amazon S3) Amazon Simple Email Service (Amazon SES) AWS Lambda Amazon CloudWatch Alarm AWS Chatbot Amazon Simple Queue Service (Amazon SQS) Backend Service End Users send messages for mails upload attached files for mails get attached files for mails batch response for failures lock for deduplication send mails send to DLQ trigger alarm notice
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ • ①AWS Cloud Development Kit (AWS CDK)(TypeScript) • ②Amazon Simple Queue Service (Amazon SQS) • ③Amazon Simple Storage Service (Amazon S3) • ④Amazon DynamoDB • ⑤AWS Lambda(TypeScript) • ⑥Amazon Simple Email Service (Amazon SES) • ⑦監視リソース § Amazon SQS(Dead Letter Queue) § Amazon CloudWatch Alarm § Amazon Simple Notification Service (Amazon SNS) § AWS Chatbot
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •① AWS CDK § 外部パラメータ⽤コンフィグ(TS)ファイル – TypeScriptの型保証 – Zodでバリデーション § ※このネタだけでブログ記事書きました – 「CDKの外部パラメータに型だけでなく制約もかける」 Ø https://go-to-k.hatenablog.com/entry/cdk-parameter-type-validation ①
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •② Amazon SQS § 標準キュー – LambdaトリガーだとFIFOキューでも重複起動の可能性がある Ø メール送信後何らかのエラーでLambdaが終了した際、SQSにメッセージは残り再度Lambdaが⾛る – 標準キュー+DynamoDBでメール送信の重複排除をすることにした § 部分バッチ応答 – バッチ処理のうち失敗したメッセージだけをリトライできる ② CDK例(SQS) 部分バッチ応答 のLambda例 → →部分バッチ応答
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •③ Amazon S3 § 添付ファイル⽤ – SQSに画像をバイナリで乗せる案もあったが・・・ § SQSのペイロード上限 (256KB)超過を考慮して採⽤ – ※Java⽤のAmazon SQS 拡張クライアントライブラリの話はしません § 添付ファイルの流れ – メールシステム呼び出し側で先にS3に画像をアップロード – 画像のパスをメッセージに含めてSQSに渡す – Lambdaが受け取ったパスをもとにS3に取りにいく – S3上のファイルはライフサイクルで定期削除 ③ CDK例(S3)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •④ Amazon DynamoDB § メール送信の重複排除⽤ § 条件付き書き込み(ConditionExpression)でロックを表現 – 送信メールごとに⼀意になるキーを定義 (メールID+時刻 など) – 条件付き書き込みが成功した=ロックが確保できたらメールを送信する Ø ロック確保後にメール送信に失敗したらcatch節でロック解放 – 条件付き書き込みに失敗した=ロックが確保できなかったら 既にメールが送信されたものと⾒なしてメールを送信しない § TTL(TimeToLive) – 重複排除⽤のデータは送信後しばらくしたら消えても良い – 指定した期間後にデータが⾃動削除されコスト削減 ④ CDK例(DynamoDB)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •⑤ AWS Lambda § NodejsFunction (aws-cdk-lib/aws-lambda-nodejs) – esbuild – LambdaとCDKで1つのpackage.jsonを共有できる Ø それぞれで必要な依存関係だけに絞ってバンドルしてくれる – ファイル名のルールによりパスを指定せず⾃動でソースコードファイルを読んでくれる Ø スタックファイル︓mystack.ts Ø Lambdaコードファイル︓mystack.mailer.ts ⑤ CDK例(Lambda)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •⑥ Amazon SES § 構築はCDKのスコープ外とした – CloudFormationでは作りづらかった(SPFやDMARCなどの設定もある) Ø EmailIdentityなどの設定はあります – なので構築済みの前提とします – スクリプトなどでも作れます § サンドボックス外への移動(=制限解除)を忘れずに – APIスロットルレートなどの緩和がされます – デフォルト(サンドボックス状態) Ø 200 メッセージ/24h, 1 メッセージ/1s ⑥
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アーキテクチャ •⑦ 監視リソース § Amazon SQS(Dead Letter Queue) § Amazon CloudWatch Alarm § Amazon SNS § AWS Chatbot ⑦ Constructクラス Construct Stack
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 運⽤してみた結果
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •半年間ほど運⽤してみた § 利⽤者数︓8000⼈強 – マイクロサービスとして⼀部のAPIから使⽤される •結局どうだったの︖
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •累計送信数 § 約30万件 •Error件数 § 約9000件 – ※バウンスは対象外 エラー率︓3% (つまり100件に3件はエラー)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 えっ・・・ ダメじゃん・・・︖
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 いえ、違うんです︕︕
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •エラーの内容を⾒てみると・・・ § ほぼ全てAmazon SESのスロットリングエラー – SESレート設定(クォータ)︓秒間20件、24時間5万件 – ⼀度に複数送るユースケースも多いのでエラーは想定済み・必然的 ※SQS部分バッチ応答はエラー発⽣時catch内でthrowせずにreturn(正常終了)するため、 Lambdaのエラーメトリクスにはカウントされない(そのためログから観測しました) § 「ほぼ全て」というのは︖ – 1件のみLambdaの原因不明のタイムアウト(timeout: 15秒) – 残りは全てスロットリングエラー 発⽣したのは必然的なエラーのみ(1件以外)
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •起きたエラーは再実⾏により、全てメール送信はできていた § SQSの部分バッチ応答によるリトライ § エラーのみを再実⾏し、全て試⾏回数内で送信成功 – maxReceiveCount: 5 実質、送信失敗数は 【0件】︕
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •運⽤の⼿間 § ほとんど何もしていない – バウンス対策はSESのサプレッションリストが便利 – バウンス率の監視はすべき (CloudWatchで可能) •パフォーマンス § 平均duration (Lambda) : 約300ms (メモリ 512MB) – 1 Lambda実⾏での平均メール送信数は約2件 (設定バッチ数とユースケース次第) § 最⼤ピーク時のメール送信数 : 約3000件/5分 – ※CloudWatchメトリクスの63⽇以前のデータは5分単位の集計になるので実際はもっと狭い期間なはず
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 運⽤してみた結果 •結局どうだったの︖ § 送信失敗なし § サーバーレス・マネージドサービスにより運⽤の⼿間もほぼなし § ⾼スケーラビリティ・⾼コストパフォーマンス – SQS + Lambdaによる⽔平スケーリング – 従量課⾦ •課題 § Amazon SESのレート制限の緩和申請してみる︖ § SQS->Lambdaのバッチ数(同時起動数)を減らしてみる︖ § Lambdaのタイムアウト調査・監視
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. まとめ •メール送信システムを構築 § フルサーバーレス § AWS CDK •数千⼈規模の環境で半年間運⽤ § 送信失敗なし § 運⽤の⼿間ほぼなし § ⾼スケーラビリティ § ⾼コストパフォーマンス
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 参考 •ブログ記事(365歩のテック) § 「サーバーレスでスケールするメール送信システムをAWS CDKで構築する」 – https://go-to-k.hatenablog.com/entry/mail-queues § 「CDKの外部パラメータに型だけでなく制約もかける」 – https://go-to-k.hatenablog.com/entry/cdk-parameter-type-validation •GitHub § go-to-k/mail-queues – https://github.com/go-to-k/mail-queues
© 2022, Amazon Web Services, Inc. or its affiliates. All
rights reserved. Thank you! © 2022, Amazon Web Services, Inc. or its affiliates. All rights reserved. 後藤 健太 Twitter: @365_step_tech