Upgrade to Pro — share decks privately, control downloads, hide ads and more …

cdk-snapshot-diff-drift

 cdk-snapshot-diff-drift

AWS CDKには、リソースやテンプレートの「差分」を確認するさまざまな手段があります。それぞれの操作が何を比較しているかを正しく理解することによって、意図しない変更を見つけられるようになります。このセッションでは、CDKとCloudFormationの世界のつながりを意識しながら、さまざまな差分検出のしくみを解説し、CDKの運用を安全にする方法を紹介します。
- AWS CDK Conference 2025 の登壇資料です

Avatar for Kenji Kono

Kenji Kono

July 12, 2025
Tweet

More Decks by Kenji Kono

Other Decks in Programming

Transcript

  1. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. ご注⽂の差分はこちらですか︖ ~ AWS CDKのいろいろな差分検出と安全なデプロイ ~ ⾼野 賢司 A W S C D K C O N F E R E N C E J A P A N 2 0 2 5 シニア ソリューション アーキテクト アマゾン ウェブ サービス ジャパン合同会社 2025/7/12
  2. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 • AWS の開発者体験とツールのエキスパート • Infrastructure as Code (IaC) の ⽇本における技術⽀援をリード • AI 駆動開発の話もよくしています • 撮影班も兼務 📸 @konokenj こ う の け ん じ ⾼野 賢司 シニア ソリューション アーキテクト @名古屋 アマゾン ウェブ サービス ジャパン合同会社 本⽇ 2回⽬
  3. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 IaC ツールは状態の差分を⾃動的に埋めてくれる ソースコード IaC ツール クラウドリソース リソースは この状態であるべき (宣⾔的) ⽬的の状態に 収束 IaC コードの変更がリソースにどのような影響を及ぼすか理解したり、 IaC ツールが管理していないリソースの変更に対処したりするには IaC の世界での 差分 を正しく理解することが⼤切 Under the hood
  4. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 AWS CDK における 4 つの差分検出 $ npm run test $ cdk diff --no-change-set $ cdk diff $ cdk drift スナップショットテスト * テストフレームワークが⽣成した .snap ファイルの差分を Git で⽐較 CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算 CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 (default) CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 ↑ ⾒た⽬は同じ ↓
  5. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 🤔 なにがどう違うの︖
  6. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 AWS CDK における 4 つの差分検出 $ npm run test $ cdk diff --no-change-set $ cdk diff $ cdk drift スナップショットテスト * テストフレームワークが⽣成した .snap ファイルの差分を Git で⽐較 CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算 CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 (default) CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 ↑ ⾒た⽬は同じ ↓ プログラミングと Git の世界
  7. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. スナップショットテストのしくみ
  8. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 スナップショットテストのしくみ ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません テストフレームワークが Snapshot ファイルを⾃動的に保存 * 中⾝は CloudFormation テンプレート (JSON) Git リポジトリ Queue を追加 [+] test/__snapshots__/hoge.test.ts.snap new Queue(stack, 'Queue', { visibilityTimeout: Duration.seconds(600), }); hoge.ts test('Snapshots are consistent', () => { const app = new cdk.App(); const stack = new HogeStack(app, 'Dev-Hoge', {}); const template = Template.fromStack(stack); expect(template).toMatchSnapshot(); }); hoge.test.ts $ npm run test 600 1. ユーザーが CDK の実装コードとテストコードを書いて、テストを実⾏ main
  9. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 スナップショットテストのしくみ ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません スナップショットが 現在のものと異なるため テストが失敗する Git リポジトリ Queue を追加 [+] test/__snapshots__/hoge.test.ts.snap new Queue(stack, 'Queue', { visibilityTimeout: Duration.seconds(600), }); hoge.ts test('Snapshots are consistent', () => { const app = new cdk.App(); const stack = new HogeStack(app, 'Dev-Hoge', {}); const template = Template.fromStack(stack); expect(template).toMatchSnapshot(); }); hoge.test.ts $ npm run test 600 2. ユーザーが CDK の実装コードを変更して、テストを実⾏ 変更なし 100 main
  10. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 スナップショットテストのしくみ ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません スナップショットファイルが 新しい CloudFormation テンプレートの内容で更新される Git リポジトリ Queue を追加 [+] test/__snapshots__/hoge.test.ts.snap new Queue(stack, 'Queue', { visibilityTimeout: Duration.seconds(600), }); hoge.ts test('Snapshots are consistent', () => { const app = new cdk.App(); const stack = new HogeStack(app, 'Dev-Hoge', {}); const template = Template.fromStack(stack); expect(template).toMatchSnapshot(); }); hoge.test.ts $ npm run test -- -u 3. ユーザーが意図した変更であれば、スナップショットを更新 変更なし 100 可視性タイムアウトを変更 [~] test/__snapshots__/hoge.test.ts.snap 100 main feat 600 追加
  11. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 スナップショットテストのしくみ ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません Git リポジトリ Queue を追加 [+] test/__snapshots__/hoge.test.ts.snap 4. プルリクエストを送り、スナップショットの差分をレビューする 可視性タイムアウトを変更 [~] test/__snapshots__/hoge.test.ts.snap 100 main feat 600 レビュアー スナップショットテストの意義 • 合成済みの完全な CloudFormation テンプレートの 変更を追跡する • Git の差分でリソースの変更をレビューする • aws-cdk-lib を含む外部ライブラリの変更による 差分に気付く • リファクタリングを可能にする この差分は OK!!
  12. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 AWS CDK における 4 つの差分検出 $ npm run test $ cdk diff --no-change-set $ cdk diff $ cdk drift スナップショットテスト * テストフレームワークが⽣成した .snap ファイルの差分を Git で⽐較 CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算 CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 (default) CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 ↑ ⾒た⽬は同じ ↓ AWS CloudFormation の世界
  13. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. AWS CloudFormation による 状態管理 〜 リソース作成&更新
  14. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 AWS CloudFormation Queue1 VisibilityTimeout: 600 1. ユーザーが CloudFormation テンプレートをデプロイ https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません $ aws cloudformation deploy
  15. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 AWS CloudFormation Stack Copy of Template Queue1 VisibilityTimeout: 600 2. CloudFormation がスタックを作成して、テンプレートを保存 https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません New 新しいテンプレートの コピーを保存
  16. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 3. CloudFormation が現在の状態を確認して、変更を計画する (変更セット) https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません New 変更セット 現在の状態によると Queue1 はないので 作成しよう Current
  17. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 4. CloudFormation がリソースを操作する https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません New AWS API Call Current 変更セット CreateQueue
  18. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 5. CloudFormation がリソースの状態 (State) を記録する https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません 覚えた Point CloudFormation Stack には 最後にデプロイしたときの リソースの状態が記録されている Current Current
  19. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 $ cdk diff --no-change-set AWS CloudFormation Stack Copy of Template State of Resources https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません 100 100 600 CDK Toolkit 差分あるで!! 「diff」の名前の通り diff コマンドに近い動作 ※ 速いが検出精度が低い Current CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算
  20. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 $ cdk diff AWS CloudFormation Stack Copy of Template State of Resources https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません 100 100 600 ここ変えるで!! CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 「変更セット」の名前の通り 変更計画書のようなもの • $ cdk diff --no-change-set よりも⾼精度 • !Ref や !GetAtt での参照や SSM Parameter Store, Secrets Manager の動的参照も 解決した上で差分を計算する ※ AWS CDK v2.119.0 以降のデフォルト New Current 変更セット
  21. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 🤔 CloudFormation の外で リソースを変更したらどうなる︖
  22. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. AWS CloudFormation による 状態管理 〜 ドリフト検出
  23. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 / ドリフト検出 AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 ユーザーが CloudFormation の外でリソースを直接変更する ※ 強く⾮推奨 https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません 100 ⼿で変えたろ 600 Current Current
  24. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 CloudFormation による状態管理 / ドリフト検出 AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 DelaySeconds: 200 ドリフトがある状態でデプロイすると、動作は未定義 ※ 強く⾮推奨 https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません ? 600 Current New 100 VisibilityTimeout は ... 600 に戻る︖ 100 のまま︖ 更新に失敗する︖ 動作は予測できない
  25. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 $ cdk drift AWS CloudFormation Stack Copy of Template State of Resources Queue1 VisibilityTimeout: 600 Queue1 VisibilityTimeout: 600 https://docs.aws.amazon.com/prescriptive-guidance/latest/getting-started-terraform/states-and-backends.html ※ 現在の動作および⼀部のドキュメントに基づく記述であり、 AWS サービスの仕様を⽰すものではありません 100 CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 100 600 思ってたんと 違う!! CloudFormation が 知っている状態との⽐較 Current Current
  26. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 ドリフトの解消⽅法はこちらをご覧ください https://speakerdeck.com/wandora58/aws-cdk- kai-fa-wocheng-gong-nidao- kutoraburusiyuteingugaido AWS CDK 開発を成功に導く トラブルシューティングガイド
  27. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. まとめ
  28. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 AWS CDK における 4 つの差分検出 $ npm run test $ cdk diff --no-change-set $ cdk diff $ cdk drift スナップショットテスト * テストフレームワークが⽣成した .snap ファイルの差分を Git で⽐較 CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算 CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 (default) CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 ↑ ⾒た⽬は同じ ↓
  29. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 AWS CDK における 4 つの差分検出 $ npm run test $ cdk diff --no-change-set $ cdk diff $ cdk drift スナップショットテスト * テストフレームワークが⽣成した .snap ファイルの差分を Git で⽐較 CloudFormation テンプレートの新旧⽐較 * CDK が現在のテンプレートをダウンロードして⽂字列ベースで差分を計算 CloudFormation 変更セット * CloudFormation がサービスの機能としてリソースの差分を計算 (default) CloudFormation ドリフト検出 * CloudFormation が記録しているリソースの状態と現実の状態の差分を計算 ソースコード スナップ ショット v2 v2 v1 スナップ ショット CloudFormation テンプレート AWS CloudFormation Stack Copy of Template State of Resources リソース 1 2 3 4 1 2 3 4
  30. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 ご注⽂の差分は︖ 右図の順序がそのまま差分の使⽤頻度に対応する ソースコード スナップ ショット v2 v2 v1 スナップ ショット CloudFormation テンプレート AWS CloudFormation Stack Copy of Template State of Resources リソース 1 3 4 すべての変更 • テンプレートの差分をレビューしたい • ライブラリ更新の⾃動化や リファクタリングを安全に⾏いたい 1 スナップ ショット テスト 多くの変更 • リソースの更新動作を確認したい (中断の有無、置換) • パラメータを反映した結果を確認したい 3 $ cdk diff 必要に応じて • リソースを⼿で触ってしまった • ⻑い間デプロイしていないので不安 • 定期的なコンプライアンスチェック 4 $ cdk drift
  31. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 • すべてを Git で管理する (Everything as Code) • そもそもドリフトさせない • CDK / CloudFormation 以外の⽅法(⼿作業、CLI 等)でリソースを変更しない • ドリフトがある場合は、解消してからデプロイする • CloudFormation テンプレートで明⽰的に指定されたプロパティのみ ドリフト検出できる • すべてのプロパティを明⽰的に指定するのは保守性を著しく損なうため⾮推奨 AWS CDK を安全にデプロイするために
  32. © 2025, Amazon Web Services, Inc. or its affiliates. All

    rights reserved. Amazon Confidential and Trademark. < #cdkconf2025 Thank you! © 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved. Amazon Confidential and Trademark. Kenji Kono @konokenj https://x.com/konokenj