Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
AWS CDKで学ぶGoFデザインパターン〜IaCにもコード設計〜 / aws dev da...
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
k.goto
June 21, 2023
Programming
9.6k
18
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns
AWS Dev Day 2023 Tokyo登壇資料です。
k.goto
June 21, 2023
More Decks by k.goto
See All by k.goto
Level Up Your CDK DX: 5 Tools I’ve Been Building
gotok365
2
210
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
3k
IaC コードを資産へ:AWS CDK 社内ライブラリと横断展開 / aws-summit-japan-2026
gotok365
10
4.9k
AWS CDK の目玉新機能「Mixins」とは / cdk-mixins
gotok365
2
780
AWS CDKの仕組み / how-aws-cdk-works
gotok365
18
6k
AWS CDK 実践的アプローチ N選 / aws-cdk-practical-approaches
gotok365
9
2.9k
TypeScript製IaCツールのAWS CDKが様々な言語で実装できる理由 ~他言語変換の仕組み~ / cdk-language-transformation
gotok365
10
1.7k
とあるEdTechベンチャーのシステム構成こだわりN選 / edtech-system
gotok365
7
1.1k
CodePipelineのアクション統合から学ぶAWS CDKの抽象化技術 / codepipeline-actions-cdk-abstraction
gotok365
5
570
Other Decks in Programming
See All in Programming
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.4k
R8 の設定見直しでアプリサイズ削減
andpad
0
190
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
130
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
160
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
120
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
340
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
450
Agents on Rails - Rails at Scale 2026
irinanazarova
0
350
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
320
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
150
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
400
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
570
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
300
Six Lessons from altMBA
skipperchong
29
4.5k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
SEO for Brand Visibility & Recognition
aleyda
0
4.8k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Prompt Engineering for Job Search
mfonobong
0
490
Designing for humans not robots
tammielis
254
26k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.4k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
610
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
350
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Transcript
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 後藤 健太 (k.goto) 2 0 2 3 . 0 6 . 2 2 - 2 3 A W S D E V D A Y 2 0 2 3 T O K Y O B - 1
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⾃⼰紹介 • 後藤 健太 (k.goto) § テックリード § AWS Community Builder (DevTools) § 365歩のテック (技術ブログ) § ⾃作AWSツールのOSS開発 § AWS CDK コントリビューター § Twitter: @365_step_tech – k.goto(365歩のテック)
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. アジェンダ • GoFデザインパターンとは • 本セッションの⽬的と注意 • GoFデザインパターン × AWS CDK実例集 • まとめ
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. GoFデザインパターンとは
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターンとは • オブジェクト指向におけるコード設計のデザインパターン § 書籍『オブジェクト指向における再利⽤のためのデザインパターン』 (※) – 通称『GoF本』 – GoF(Gang of Four) = この共著者の4⼈ – 全23パターン • 古くからあるが、AWS Cloud Development Kit (AWS CDK) の 内部実装にも⼀部⽤いられている ※『オブジェクト指向における再利⽤のためのデザインパターン』 (ソフトバンクパブリッシング) 著: エーリヒ・ガンマ、ラルフ・ジョンソン、リチャード・ヘルム、ジョン・ブリシディース 監訳: 本位⽥真⼀, 吉⽥和樹
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 本セッションの⽬的と注意
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 本セッションの⽬的 • AWS CDKを通してGoFデザインパターンを学ぶ • AWS CDKコードを効率的にする ※もちろんCDKコードの効率性に繋がる点もたくさんありますが、あくまで 本セッションではそれらを推奨・⽬的とするものではありません。 AWSユーザに馴染み深いAWS CDKを通して GoFデザインパターンを学ぼう︕
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 注意 • 本来のGoFデザインパターンと表現が多少異なる場合があります。 § CDKコードにうまく適⽤するため • CDKコードを書く上で、通常と⽐べて冗⻑になる場合があります。 § GoFデザインパターンを適⽤・説明しやすくするため
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. GoFデザインパターン × AWS CDK実例集
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ①Compositeパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン • ⽊構造を伴う再起的なデータ構造を利⽤した⼿法 § 容器と中⾝を同⼀視することで再起的な構造の⾛査が可能に
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【CDKでのシチュエーション】 § 特定のリソース群に対して⼀元的にパラメータ設定などの操作を適⽤したい 【使⽤例】 § AWS CDKの特徴であるリソースのツリー構造を利⽤して、 特定のコンストラクト内の全リソース、またはスタック内のすべてのリソースに 対して⼀括で「RemovalPolicy」パラメータを指定する
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ①Compositeパターン 【コード例】 →ResourceならRemovalPolicy適⽤ →Constructなら、さらにその⼦供達に対して同メソッドを再起呼び出し ※applyRemovalPolicy: Resourceに定義されたRemovalPolicy適⽤メソッド ※RetainConstruct: Retainにしたいリソースを集めた⾃作Construct
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ②Facadeパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ②Facadeパターン • 複数のクラス群を組み合わせて⾏う複雑な⼿順を、⼀つの窓⼝を 設けることで、それを呼び出すだけで簡単に⾏えるようにする⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ②Facadeパターン 【CDKでのシチュエーション】 § 複雑なリソース構築をうまくまとめてシンプルに呼び出したい 【使⽤例】 § Constructを作成してその中に⼀連のリソース定義をまとめて定義し、 Stackからはそれを呼び出すだけでシンプルなリソース構築を⾏う
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ②Facadeパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ②Facadeパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ②Facadeパターン 【コード例】 Facade≒Constructみたいなもの ① ① ② ③ ② ③
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ③Adapterパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン • 既存のクラスに⼿を加えられない時に、そのクラスを変更すること なく機能拡張をするための⼿法 § ライブラリの拡張など後⽅互換性を損ないたくないケースで有効
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン • 継承でなく委譲(コンポジションを利⽤した技法)する形も可 § AdapterがAdapteeをプロパティとして持ち、Adapterのメソッド内部でAdapteeの メソッドを呼び出す • 本セッション例ではCDKとの相性の理由で継承を使⽤しているが、 本来は継承より委譲の⽅が望ましい § “クラス継承よりもオブジェクトコンポジションを多⽤すること” – GoF本 第1章『概論』より
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン 【CDKでのシチュエーション】 § CDK提供のリソース定義(BucketやQueue等)に、新たなメソッドを追加したい 【使⽤例】 § あるリソースに複数のリソースポリシーを⼀度の呼び出しで⼀気に付けたいが、 単⼀ポリシーを付けるメソッド(addToResourcePolicy)しか⽤意されていない § そこで、それらのリソースクラスを拡張する新たなクラスを作り、そのクラスに ⾃作のメソッド(addManyToResourcePolicy)を定義する
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン 【コード例】 ※Bucketをextendsしているので、⾃⾝(this)が addToResourcePolicyメソッドを持っている →それをループで回すことで⼀気にポリシーをアタッチするように⾒せる ① ② ① ②
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ③Adapterパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ④Decoratorパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン • 基となるオブジェクトに動的に飾りつけを重ねていき、柔軟に機能の 拡張を⾏うための⼿法 § オブジェクトをラップすることで、飾りつけ(機能追加)が可能に
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【CDKでのシチュエーション】 § リソースを作成するときに、あらかじめ特定の設定が適⽤されたものが 作られるようにしたい 【使⽤例】 § リソースを作成する際にいちいちapplyRemovalPolicyメソッドを呼び出さずに RemovalPolicyが適⽤できるようなラップクラスを定義する
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【コード例】 ※若⼲無理やりパターンを実現をしています constructor内で、サブクラスのgetOwnRemovalPolicyでRETAIN|DESTROYを呼び、 それを⾃⾝のapplyRemovalPolicyに渡している。 これによってnewと同時にRemovalPolicyの適⽤ができるようになる。 ① ② ③ ① ② ③
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ④Decoratorパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤ ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑤Singletonパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン • あるクラスのコンストラクタが、何度呼び出されても⼀つしか インスタンスが作成されないことを保証する⼿法 § ⼀度作られたインスタンスを使い回す ※今はあまり推奨されていないパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン 【CDKでのシチュエーション】 § あるスタック内で何度呼び出されても作られるリソースは⼀つであって欲しい – Custom ResourceのハンドラーとしてLambda関数を使う場合など 【使⽤例】 § CDK提供のSingletonFunctionクラスを使う ※CDK提供のSingletonFunctionは、挙動はいわゆるSingletonパターンと同じ ですが内部の実装がCDK⾊強いため、今回は⾃前のSingleton実装を出します。 ※参考: 『AWS CDK コンストラクトライブラリ開発に関する5つのTips』 https://tmokmss.hatenablog.com/entry/20220517/1652755027
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン 【コード例】 constructorがprivateなのでクラス内部からしか呼び出せない ① ①
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑤Singletonパターン https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/aws-lambda/lib/singleton-lambda.ts 【参考: SingletonFunctionコード】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥ ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑥Strategyパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン • クライアントと独⽴してアルゴリズムを定義し、かつクライアントが 意識せず動的に振る舞いを変更可能にする⼿法 § 戦略(アルゴリズム)/型(クラス)ごとの分岐処理いらず
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【CDKでのシチュエーション】 【使⽤例】 § CDKで提供されるValidation機能の内部実装において使⽤されている
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【addValidation / IValidation】 ※https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.Node.html#addwbrvalidationvalidation addValidationによりバリデーションルール(IValidation)を登録し、 cdk synth時にvalidationメソッドが発⽕しバリデーションが⾛る
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【addValidation / IValidation】 ※https://docs.aws.amazon.com/cdk/api/v2/docs/constructs.IValidation.html validate()というメソッドを 実装すれば良い
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン 【コード例】 ① ① addValidationにより登録されたバリデーション処理 (validationメソッド)がcdk synth時に発⽕する
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑥Strategyパターン https://github.com/aws/constructs/blob/10.x/src/construct.ts 【validateコード】 IValidationというinterfaceであればvalidate()を持っているので _validationsの各要素が何の実クラスなのか気にしなくて良い。 validate()の中⾝がどんな処理かも知らずに実⾏できる。 = ポリモーフィズム
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦ ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑦Template Methodパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン • 処理の⼤枠の⼿順を組んでおき、各ステップの具体的な内容は サブクラスで実装する⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン 【CDKでのシチュエーション】 § Validation機能(⑥Strategyパターン参照)において、各環境(dev | prod)ごとで バリデーションの内容を変えたい 【使⽤例】 § IValidationを実装するValidatorを抽象クラスとし、 そこでバリデーションロジックの⼤枠を組む § それを継承するdev | prod⽤サブクラスを作成し、上記⼤枠の中の 各ステップでのロジックをそれぞれのサブクラスごとで組む
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン 【コード例】 ① ① ② ② ③ ③
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑦Template Methodパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧ ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑧Factory Methodパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン • 操作の中でオブジェクト⽣成部分は抽象的に定義しておき、実際に どのクラスをインスタンス化するかはサブクラスが決める⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン 【CDKでのシチュエーション】 § あるリソースを⽣成する際に、あらかじめ特定の設定が適⽤されたものが 作られるようにしたい – ④Decoratorパターン章とほぼ同じシチュエーション – Decoratorでは「どのリソースも」、こちらは「あるリソースを」 【使⽤例】 § あるリソースの⽣成処理だけ抽象的に定義しておき、それによって⽣成された オブジェクトに対して特定の設定(RemovalPolicy)を適⽤する処理を定義する § 抽象定義した⽣成部分は、サブクラスで実装する
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン 【コード例】 ① ① ② ② ③ ③ 何が作られるかはサブクラス次第だが、作られるものはResource型である。 Resource型はapplyRemovalPolicyを持つので、作られるものが何だろうと実⾏できる
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑧Factory Methodパターン 【コード例】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨ ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑨Abstract Factoryパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン • 特定の組み合わせのオブジェクト群を⽣成するためのインターフェー ス(受け⼝)を提供する⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【CDKでのシチュエーション】 § 環境ごとでコンポーネントの組み合わせの違う、環境特有のセットを作りたい 【使⽤例】 § 細かいコンポーネント(App群, Batch群, WAF等)を各Constructで定義する § Dev | Prodでそれぞれ使⽤するコンポーネント(Construct)の組み合わせを さらにConstructとして定義し、環境ごとに個別で構築できるようにする – 例︓Devはアプリだけでいいが、Prodは管理⽤バッチやWAFなどもつけたい
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【コード例】 ① ② ③ ③ ① ②
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【コード例】 ④ ⑤ ⑥ ⑥ ④ ⑤ AppModule, BatchModule, WafModuleはそれぞれConstruct。 例︓AppModuleにはLambdaがn個、BatchにはLambdaが1個、WAFにはWAFやIPSetなど・・・ ※ProductBもほぼ同様コード
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑨Abstract Factoryパターン 【コード例】 これを呼んでconfigを作り、Stack呼び出しで渡す これだけで、dev | prdで違うセットのリソースが作れ、 条件分岐の数も最⼩(余計な分岐が出てこない) さらにパラメータも違うものが作れる(以下参照)
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨Abstract Factoryパターン ⑩
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. ⑩Visitorパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン • データ構造から操作を分離することで、既存のオブジェクトに対する 新しい操作をデータ構造に変更を加えずに追加できる⼿法
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【CDKでのシチュエーション】 【使⽤例】 § “Aspects employ the visitor pattern.”(※) § スコープ内の全ての要素に操作の適⽤・検証する機能 – コンプライアンス・セキュリティのガードレールなどのために使われることが多い ※https://docs.aws.amazon.com/cdk/v2/guide/aspects.html
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【CDKクラス図】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【CDKクラス図 簡略版】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【⽐較】
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【コード例】 ① ① データ構造と操作を分離することが Visitorパターンの本質 データ構造: ResourceやConstruct 構造に対する操作: Aspect(visit)
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【コード例】 ② ③ ② ③ ※これはただのAspectsの使い⽅ (Visitorパターンにあまり関係ない) 例︓Bucketを定義したスタック(③)に対してAspectを当てる
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/core/lib/aspect.ts 【参考: Aspectsコード】 ④ ④
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. ⑩Visitorパターン 【参考: synthesizeコード】 https://github.com/aws/aws-cdk/blob/a2c633f1e698249496f11338312ab42bd7b1e4f0/packages/aws-cdk-lib/core/lib/private/synthesis.ts データ構造に対して⾛査し 分離して定義した操作を当てる =Visitorパターンの本質 再起呼び出し (データ構造の⾛査) visit(操作) Aspectsを取得 してループ
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. • ※補⾜︓よく使われるVisitorパターンのセオリー(以下例)とは細かい実装⽅法が異なる § ①構造⾃体(CDKで⾔うconstruct) がacceptというメソッドを持つ § ②visitor側がデータ構造の各サブクラスごとの型の引数のメソッドを全て⽤意 § ③acceptメソッドの引数でvisitorを渡し、その中でvisitor.visit(this)呼び出し – ⾃分⾃⾝を渡すことで(動的に)メソッド/型の選択を⾏うダブルディスパッチ § ④ObjectStructure(構造保持オブジェクト)により構造を⾛査 (これはデータ構造の種類が限られている前提であり、CDKの様に構造の種類が膨⼤・かつ可変なケースでは現実的に難しい。 そのためCDK(Aspects)ではダブルディスパッチでなく抽象を受け取るvisitメソッドでinstanceofでの分岐が必要になる) ⑩Visitorパターン ② ① ③
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. GoFデザインパターン × AWS CDK実例集 ①Compositeパターン ②Facadeパターン ③Adapterパターン ④Decoratorパターン ⑤Singletonパターン ⑥Strategyパターン ⑦Template Methodパターン ⑧Factory Methodパターン ⑨Abstract Factoryパターン ⑩Visitorパターン
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. まとめ
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. まとめ • GoFデザインパターン × AWS CDKの実例紹介 § 10パターン / 全23パターン • 本セッションの⽬的 § AWS CDKを通してGoFデザインパターンを学ぶ § AWS CDKコードを効率的にする AWSユーザに馴染み深いAWS CDKを通して GoFデザインパターンを学ぼう︕
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 参考︓ GitHub(参考コード・クラス図) • 本セッションで挙げたCDKコード実例・クラス図など全てGitHubにて 公開しています。 – https://github.com/go-to-k/cdk-gof-design-pattern
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. 宣伝︓⾃作AWSツールOSS • delstack: AWS CloudFormationスタック強制削除ツール – https://go-to-k.hatenablog.com/entry/delstack • cls3: S3バケット⾼速削除・空にするツール(バージョニング対応) – https://go-to-k.hatenablog.com/entry/cls3 • lamver: Lambdaランタイム/バージョン検索ツール(リージョン横断) – https://go-to-k.hatenablog.com/entry/lamver
© 2023, Amazon Web Services, Inc. or its affiliates. All
rights reserved. Thank you! © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved. 後藤 健太 Twitter: @365_step_tech