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

壊して学ぶAWS CDK: そのcdk deployで消えるもの、残るもの

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

壊して学ぶAWS CDK: そのcdk deployで消えるもの、残るもの

AWS CDKの変更で「何が消え、何が残るのか」を、実際に壊しながら確かめます。RemovalPolicy、Construct ID変更、CDK Refactor、cdk diffとcdk driftの違いを具体例で整理。デプロイ成功だけでは分からない、リソース・データ・アプリへの影響を安全に見極める方法を紹介します。

登壇資料 @CDK Conference 2026
https://jawsug-cdk.connpass.com/event/393595/

Avatar for Kazuki Adachi

Kazuki Adachi

July 18, 2026

More Decks by Kazuki Adachi

Other Decks in Technology

Transcript

  1. 自己紹介 SPEAKER Kazuki Adachi @k_adachi_01 AWS Community Builder AWS All

    Certifications Engineer JAWS-UG 配信部 好きなAWSサービス KiroとCDK 最近ハマっているもの コーヒーとトレラン 2
  2. “To invent you have to experiment, and if you know

    in advance that it’s going to work, it’s not an experiment.” 「発明するには実験しなければならない。 あらかじめ成功すると分かっているなら、 それは実験ではない」 Jeff Bezos Amazon 2015 Letter to Shareholders https://ir.aboutamazon.com/files/doc_financials/annual/2015-Letter-to-Shareholders.PDF 7
  3. lib/s3-stack.ts ts const bucket = new s3.Bucket(this, "Uploads" removalPolicy: RemovalPolicy.DESTROY,

    autoDeleteObjects: true, }); データとか知らん、消えてしまえ 13
  4. CDKコードからBucketを削除 - const bucket = new s3.Bucket( - this, "Uploads",

    { - removalPolicy: - RemovalPolicy.DESTROY, - autoDeleteObjects: true, - }); サンプル出力 Resources [-] AWS::S3::Bucket  Uploads... 16
  5. OrderStack export class OrderStack extends Stack { constructor( scope: Construct,

    id: string, props?: StackProps, ) { super(scope, id, props); const table = new Table(this, "Table", { partitionKey: { name: "id", type: AttributeType.STRING, }, removalPolicy: RemovalPolicy.RETAIN, }); } } DynamoDB 変更前: 3件 24
  6. Construct IDを変更する - const table = new Table(this, "Table", {

    + const table = new Table(this, "Orders", { 25
  7. cdk diffのmethod method 動作 auto Change Setを試す 失敗したらtemplate比較 change-set Change

    Setを使って差分を確認 template template同士を比較 https://docs.aws.amazon.com/cdk/v2/guide/ref-cli-cmd-diff.html 30
  8. これはConstruct ID const table = new Table( this, "Orders", {

    /* ... */ }, ); これは変数名ではない Construct ID 34
  9. Construct IDを変えた結果 Construct ID Table → 旧Logical ID → 旧DynamoDB

    Table Construct ID Orders → 新Logical ID → 新DynamoDB Table CloudFormationには、別のリソースに見える 36
  10. Logical IDを付け替える 旧Logical ID 新Logical ID → 同じ物理 DynamoDB Table

    Logical IDは変わる 物理リソースは変えない 45
  11. IN_SYNCは何を意味するか Stackのdrift status 意味 IN_SYNC 対応リソースの現在の構成がtemplateと一致 DRIFTED 1つ以上のリソースに差分がある NOT_CHECKED driftをまだ確認していない

    UNKNOWN driftの確認に失敗した IN_SYNCでも、アプリが正常に動くとは限らない リソース単位ではMODIFIED / DELETEDなどのstatusも表示される 62
  12. 消えていない 確認例 $ aws s3api head-object  --bucket <bucket>  --key sample.txt

    ContentLength: 128 確認例 $ aws kms describe-key  --key-id <key-id> KeyState: Enabled 72
  13. IN_SYNCでも、アプリは壊れる 確認 結果 cdk diff kms:Decrypt削除を表示 cdk drift IN_SYNC cdk

    deploy UPDATE_COMPLETE アプリ AccessDenied リソースもデータも残っている でも、アプリからは使えない 73
  14. 見ているものが違う コマンド 比較しているもの cdk diff 新しいtemplate ↔ デプロイ済みtemplate cdk drift

    デプロイ済みtemplate ↔ 現在のリソース AI どちらも、アプリが正常に動くことまでは確認していない AIに見えるのは、コードとコマンド出力だけ 74