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で学ぶGoFデザインパターン〜IaCにもコード設計〜 / aws dev da...
Search
k.goto
June 21, 2023
Programming
13
6.3k
AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns
AWS Dev Day 2023 Tokyo登壇資料です。
k.goto
June 21, 2023
Tweet
Share
More Decks by k.goto
See All by k.goto
コミュニティ駆動 AWS CDK ライブラリ「Open Constructs Library」 / community-cdk-library
gotok365
2
120
AWS CDKにおける「再利用性」を考える / aws-cdk-reusability
gotok365
7
2.6k
OSS活動のススメ / oss-activities
gotok365
4
990
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
5
1.6k
S3バケットを高速で削除・空にするツール「cls3」 / s3-deletion-tool-cls3
gotok365
4
800
AWS CDKで コンテナイメージスキャンを行う 〜ECRとその他の方法〜 / cdk-container-image-scan
gotok365
2
1.4k
スタートアップでこそCDKが活きた〜生産性を向上できた5つの理由〜 / startup-cdk-productivity
gotok365
13
4k
AWS CDKで"使う"GoFデザインパターン 〜実際どうなの?〜 / GoF design patterns used with AWS CDK
gotok365
4
1.6k
CloudWatch複合アラームでELBの5XXをいい感じに検知しようとしたらうまくいかなかった話 / cloudwatch alarm elb 5xx
gotok365
0
3.4k
Other Decks in Programming
See All in Programming
ファインディの テックブログ爆誕までの軌跡
starfish719
2
1.1k
GAEログのコスト削減
mot_techtalk
0
120
ARA Ansible for the teams
kksat
0
150
PHPのバージョンアップ時にも役立ったAST
matsuo_atsushi
0
110
パスキーのすべて ── 導入・UX設計・実装の紹介 / 20250213 パスキー開発者の集い
kuralab
3
780
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
220
Writing documentation can be fun with plugin system
okuramasafumi
0
120
技術を根付かせる / How to make technology take root
kubode
1
250
データの整合性を保つ非同期処理アーキテクチャパターン / Async Architecture Patterns
mokuo
47
17k
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
140
XStateを用いた堅牢なReact Components設計~複雑なClient Stateをシンプルに~ @React Tokyo ミートアップ #2
kfurusho
1
900
Bedrock Agentsレスポンス解析によるAgentのOps
licux
3
840
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
10
1.3k
How to Ace a Technical Interview
jacobian
276
23k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
240
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
30
4.6k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
A Philosophy of Restraint
colly
203
16k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Faster Mobile Websites
deanohume
306
31k
Making the Leap to Tech Lead
cromwellryan
133
9.1k
We Have a Design System, Now What?
morganepeng
51
7.4k
Bootstrapping a Software Product
garrettdimon
PRO
306
110k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
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