$30 off During Our Annual Pro Sale. View Details »

AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns

k.goto
June 21, 2023

AWS CDKで学ぶGoFデザインパターン 〜IaCにもコード設計〜 / aws dev day cdk gof design patterns

AWS Dev Day 2023 Tokyo登壇資料です。

k.goto

June 21, 2023
Tweet

More Decks by k.goto

Other Decks in Programming

Transcript

  1. © 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

    View Slide

  2. © 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歩のテック)

    View Slide

  3. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    アジェンダ
    • GoFデザインパターンとは
    • 本セッションの⽬的と注意
    • GoFデザインパターン × AWS CDK実例集
    • まとめ

    View Slide

  4. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターンとは

    View Slide

  5. © 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) の
    内部実装にも⼀部⽤いられている
    ※『オブジェクト指向における再利⽤のためのデザインパターン』 (ソフトバンクパブリッシング)
    著: エーリヒ・ガンマ、ラルフ・ジョンソン、リチャード・ヘルム、ジョン・ブリシディース
    監訳: 本位⽥真⼀, 吉⽥和樹

    View Slide

  6. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    本セッションの⽬的と注意

    View Slide

  7. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    本セッションの⽬的
    • AWS CDKを通してGoFデザインパターンを学ぶ
    • AWS CDKコードを効率的にする
    ※もちろんCDKコードの効率性に繋がる点もたくさんありますが、あくまで
    本セッションではそれらを推奨・⽬的とするものではありません。
    AWSユーザに馴染み深いAWS CDKを通して
    GoFデザインパターンを学ぼう︕

    View Slide

  8. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    注意
    • 本来のGoFデザインパターンと表現が多少異なる場合があります。
    § CDKコードにうまく適⽤するため
    • CDKコードを書く上で、通常と⽐べて冗⻑になる場合があります。
    § GoFデザインパターンを適⽤・説明しやすくするため

    View Slide

  9. © 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実例集

    View Slide

  10. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集










    View Slide

  11. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン

    View Slide

  12. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    • ⽊構造を伴う再起的なデータ構造を利⽤した⼿法
    § 容器と中⾝を同⼀視することで再起的な構造の⾛査が可能に

    View Slide

  13. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【CDKでのシチュエーション】
    § 特定のリソース群に対して⼀元的にパラメータ設定などの操作を適⽤したい
    【使⽤例】
    § AWS CDKの特徴であるリソースのツリー構造を利⽤して、
    特定のコンストラクト内の全リソース、またはスタック内のすべてのリソースに
    対して⼀括で「RemovalPolicy」パラメータを指定する

    View Slide

  14. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【CDKクラス図】

    View Slide

  15. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【CDKクラス図 簡略版】

    View Slide

  16. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【⽐較】

    View Slide

  17. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【コード例】

    View Slide

  18. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ①Compositeパターン
    【コード例】
    →ResourceならRemovalPolicy適⽤
    →Constructなら、さらにその⼦供達に対して同メソッドを再起呼び出し
    ※applyRemovalPolicy: Resourceに定義されたRemovalPolicy適⽤メソッド
    ※RetainConstruct:
    Retainにしたいリソースを集めた⾃作Construct

    View Slide

  19. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン









    View Slide

  20. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン

    View Slide

  21. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン
    • 複数のクラス群を組み合わせて⾏う複雑な⼿順を、⼀つの窓⼝を
    設けることで、それを呼び出すだけで簡単に⾏えるようにする⼿法

    View Slide

  22. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン
    【CDKでのシチュエーション】
    § 複雑なリソース構築をうまくまとめてシンプルに呼び出したい
    【使⽤例】
    § Constructを作成してその中に⼀連のリソース定義をまとめて定義し、
    Stackからはそれを呼び出すだけでシンプルなリソース構築を⾏う

    View Slide

  23. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン
    【CDKクラス図】

    View Slide

  24. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン
    【⽐較】

    View Slide

  25. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ②Facadeパターン
    【コード例】
    Facade≒Constructみたいなもの


    ② ③


    View Slide

  26. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン








    View Slide

  27. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン

    View Slide

  28. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    • 既存のクラスに⼿を加えられない時に、そのクラスを変更すること
    なく機能拡張をするための⼿法
    § ライブラリの拡張など後⽅互換性を損ないたくないケースで有効

    View Slide

  29. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    • 継承でなく委譲(コンポジションを利⽤した技法)する形も可
    § AdapterがAdapteeをプロパティとして持ち、Adapterのメソッド内部でAdapteeの
    メソッドを呼び出す
    • 本セッション例ではCDKとの相性の理由で継承を使⽤しているが、
    本来は継承より委譲の⽅が望ましい
    § “クラス継承よりもオブジェクトコンポジションを多⽤すること”
    – GoF本 第1章『概論』より

    View Slide

  30. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    【CDKでのシチュエーション】
    § CDK提供のリソース定義(BucketやQueue等)に、新たなメソッドを追加したい
    【使⽤例】
    § あるリソースに複数のリソースポリシーを⼀度の呼び出しで⼀気に付けたいが、
    単⼀ポリシーを付けるメソッド(addToResourcePolicy)しか⽤意されていない
    § そこで、それらのリソースクラスを拡張する新たなクラスを作り、そのクラスに
    ⾃作のメソッド(addManyToResourcePolicy)を定義する

    View Slide

  31. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    【CDKクラス図】

    View Slide

  32. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    【⽐較】

    View Slide

  33. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    【コード例】
    ※Bucketをextendsしているので、⾃⾝(this)が
    addToResourcePolicyメソッドを持っている →それをループで回すことで⼀気にポリシーをアタッチするように⾒せる




    View Slide

  34. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ③Adapterパターン
    【コード例】

    View Slide

  35. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン







    View Slide

  36. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン

    View Slide

  37. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    • 基となるオブジェクトに動的に飾りつけを重ねていき、柔軟に機能の
    拡張を⾏うための⼿法
    § オブジェクトをラップすることで、飾りつけ(機能追加)が可能に

    View Slide

  38. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【CDKでのシチュエーション】
    § リソースを作成するときに、あらかじめ特定の設定が適⽤されたものが
    作られるようにしたい
    【使⽤例】
    § リソースを作成する際にいちいちapplyRemovalPolicyメソッドを呼び出さずに
    RemovalPolicyが適⽤できるようなラップクラスを定義する

    View Slide

  39. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【CDKクラス図】

    View Slide

  40. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【CDKクラス図 簡略版】

    View Slide

  41. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【⽐較】

    View Slide

  42. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【コード例】
    ※若⼲無理やりパターンを実現をしています
    constructor内で、サブクラスのgetOwnRemovalPolicyでRETAIN|DESTROYを呼び、
    それを⾃⾝のapplyRemovalPolicyに渡している。
    これによってnewと同時にRemovalPolicyの適⽤ができるようになる。

    ② ③

    ② ③

    View Slide

  43. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ④Decoratorパターン
    【コード例】

    View Slide

  44. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン
    ④Decoratorパターン






    View Slide

  45. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン

    View Slide

  46. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン
    • あるクラスのコンストラクタが、何度呼び出されても⼀つしか
    インスタンスが作成されないことを保証する⼿法
    § ⼀度作られたインスタンスを使い回す
    ※今はあまり推奨されていないパターン

    View Slide

  47. © 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

    View Slide

  48. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン
    【CDKクラス図】

    View Slide

  49. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン
    【⽐較】

    View Slide

  50. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン
    【コード例】
    constructorがprivateなのでクラス内部からしか呼び出せない


    View Slide

  51. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑤Singletonパターン
    【コード例】

    View Slide

  52. © 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コード】

    View Slide

  53. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン
    ④Decoratorパターン
    ⑤Singletonパターン





    View Slide

  54. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン

    View Slide

  55. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    • クライアントと独⽴してアルゴリズムを定義し、かつクライアントが
    意識せず動的に振る舞いを変更可能にする⼿法
    § 戦略(アルゴリズム)/型(クラス)ごとの分岐処理いらず

    View Slide

  56. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    【CDKでのシチュエーション】
    【使⽤例】
    § CDKで提供されるValidation機能の内部実装において使⽤されている

    View Slide

  57. © 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メソッドが発⽕しバリデーションが⾛る

    View Slide

  58. © 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()というメソッドを
    実装すれば良い

    View Slide

  59. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    【CDKクラス図】

    View Slide

  60. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    【CDKクラス図 簡略版】

    View Slide

  61. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    【⽐較】

    View Slide

  62. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑥Strategyパターン
    【コード例】


    addValidationにより登録されたバリデーション処理
    (validationメソッド)がcdk synth時に発⽕する

    View Slide

  63. © 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()の中⾝がどんな処理かも知らずに実⾏できる。
    = ポリモーフィズム

    View Slide

  64. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン
    ④Decoratorパターン
    ⑤Singletonパターン
    ⑥Strategyパターン




    View Slide

  65. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン

    View Slide

  66. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    • 処理の⼤枠の⼿順を組んでおき、各ステップの具体的な内容は
    サブクラスで実装する⼿法

    View Slide

  67. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    【CDKでのシチュエーション】
    § Validation機能(⑥Strategyパターン参照)において、各環境(dev | prod)ごとで
    バリデーションの内容を変えたい
    【使⽤例】
    § IValidationを実装するValidatorを抽象クラスとし、
    そこでバリデーションロジックの⼤枠を組む
    § それを継承するdev | prod⽤サブクラスを作成し、上記⼤枠の中の
    各ステップでのロジックをそれぞれのサブクラスごとで組む

    View Slide

  68. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    【CDKクラス図】

    View Slide

  69. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    【⽐較】

    View Slide

  70. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    【コード例】






    View Slide

  71. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑦Template Methodパターン
    【コード例】

    View Slide

  72. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン
    ④Decoratorパターン
    ⑤Singletonパターン
    ⑥Strategyパターン
    ⑦Template Methodパターン



    View Slide

  73. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン

    View Slide

  74. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    • 操作の中でオブジェクト⽣成部分は抽象的に定義しておき、実際に
    どのクラスをインスタンス化するかはサブクラスが決める⼿法

    View Slide

  75. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    【CDKでのシチュエーション】
    § あるリソースを⽣成する際に、あらかじめ特定の設定が適⽤されたものが
    作られるようにしたい
    – ④Decoratorパターン章とほぼ同じシチュエーション
    – Decoratorでは「どのリソースも」、こちらは「あるリソースを」
    【使⽤例】
    § あるリソースの⽣成処理だけ抽象的に定義しておき、それによって⽣成された
    オブジェクトに対して特定の設定(RemovalPolicy)を適⽤する処理を定義する
    § 抽象定義した⽣成部分は、サブクラスで実装する

    View Slide

  76. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    【CDKクラス図】

    View Slide

  77. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    【⽐較】

    View Slide

  78. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    【コード例】






    何が作られるかはサブクラス次第だが、作られるものはResource型である。
    Resource型はapplyRemovalPolicyを持つので、作られるものが何だろうと実⾏できる

    View Slide

  79. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑧Factory Methodパターン
    【コード例】

    View Slide

  80. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    GoFデザインパターン × AWS CDK実例集
    ①Compositeパターン
    ②Facadeパターン
    ③Adapterパターン
    ④Decoratorパターン
    ⑤Singletonパターン
    ⑥Strategyパターン
    ⑦Template Methodパターン
    ⑧Factory Methodパターン


    View Slide

  81. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン

    View Slide

  82. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    • 特定の組み合わせのオブジェクト群を⽣成するためのインターフェー
    ス(受け⼝)を提供する⼿法

    View Slide

  83. © 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などもつけたい

    View Slide

  84. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    【CDKクラス図】

    View Slide

  85. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    【CDKクラス図 簡略版】

    View Slide

  86. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    【⽐較】

    View Slide

  87. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    【コード例】

    ② ③



    View Slide

  88. © 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もほぼ同様コード

    View Slide

  89. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑨Abstract Factoryパターン
    【コード例】
    これを呼んでconfigを作り、Stack呼び出しで渡す
    これだけで、dev | prdで違うセットのリソースが作れ、
    条件分岐の数も最⼩(余計な分岐が出てこない)
    さらにパラメータも違うものが作れる(以下参照)

    View Slide

  90. © 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パターン

    View Slide

  91. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン

    View Slide

  92. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    • データ構造から操作を分離することで、既存のオブジェクトに対する
    新しい操作をデータ構造に変更を加えずに追加できる⼿法

    View Slide

  93. © 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

    View Slide

  94. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    【CDKクラス図】

    View Slide

  95. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    【CDKクラス図 簡略版】

    View Slide

  96. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    【⽐較】

    View Slide

  97. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    【コード例】


    データ構造と操作を分離することが
    Visitorパターンの本質
    データ構造: ResourceやConstruct
    構造に対する操作: Aspect(visit)

    View Slide

  98. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    ⑩Visitorパターン
    【コード例】
    ② ③


    ※これはただのAspectsの使い⽅ (Visitorパターンにあまり関係ない)
    例︓Bucketを定義したスタック(③)に対してAspectを当てる

    View Slide

  99. © 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コード】


    View Slide

  100. © 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を取得
    してループ

    View Slide

  101. © 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パターン
    ② ①

    View Slide

  102. © 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パターン

    View Slide

  103. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    まとめ

    View Slide

  104. © 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デザインパターンを学ぼう︕

    View Slide

  105. © 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
    参考︓ GitHub(参考コード・クラス図)
    • 本セッションで挙げたCDKコード実例・クラス図など全てGitHubにて
    公開しています。
    – https://github.com/go-to-k/cdk-gof-design-pattern

    View Slide

  106. © 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

    View Slide

  107. © 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

    View Slide