bootstrap まで終わっている前提

app

環境が整ったのでやっとhello worldです。 https://docs.aws.amazon.com/cdk/v2/guide/hello_world.html

言語は Python を想定。あらかじめ3.10をインストール済み。

PS C:\Users\User\hello-cdk> cdk init app --language python
Applying project template app for python

# Welcome to your CDK Python project!

This is a blank project for CDK development with Python.

The `cdk.json` file tells the CDK Toolkit how to execute your app.

This project is set up like a standard Python project.  The initialization
process also creates a virtualenv within this project, stored under the `.venv`
directory.  To create the virtualenv it assumes that there is a `python3`
(or `python` for Windows) executable in your path with access to the `venv`
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.

To manually create a virtualenv on MacOS and Linux:

```
$ python -m venv .venv
```

After the init process completes and the virtualenv is created, you can use the following
step to activate your virtualenv.

```
$ source .venv/bin/activate
```

If you are a Windows platform, you would activate the virtualenv like this:

```
% .venv\Scripts\activate.bat
```

Once the virtualenv is activated, you can install the required dependencies.

```
$ pip install -r requirements.txt
```

At this point you can now synthesize the CloudFormation template for this code.

```
$ cdk synth
```

To add additional dependencies, for example other CDK libraries, just add
them to your `setup.py` file and rerun the `pip install -r requirements.txt`
command.

## Useful commands

 * `cdk ls`          list all stacks in the app
 * `cdk synth`       emits the synthesized CloudFormation template
 * `cdk deploy`      deploy this stack to your default AWS account/region
 * `cdk diff`        compare deployed stack with current state
 * `cdk docs`        open CDK documentation

Enjoy!

Initializing a new git repository...
Please run 'python -m venv .venv'!
Executing Creating virtualenv...
 All done!

PS C:\Users\User\hello-cdk>

ライブラリ導入

init で作成された仮想環境を有効にし、必要なライブラリを導入する。

PS C:\Users\User\hello-cdk> .\.venv\Scripts\activate
(.venv) PS C:\Users\User\hello-cdk> python -m pip install -r requirements.txt
Collecting aws-cdk-lib==2.64.0
  Using cached aws_cdk_lib-2.64.0-py3-none-any.whl (26.7 MB)
Collecting constructs<11.0.0,>=10.0.0
  Using cached constructs-10.1.246-py3-none-any.whl (57 kB)
Collecting aws-cdk.asset-node-proxy-agent-v5<3.0.0,>=2.0.42
  Using cached aws_cdk.asset_node_proxy_agent_v5-2.0.52-py3-none-any.whl (1.3 MB)
Collecting publication>=0.0.3
  Using cached publication-0.0.3-py2.py3-none-any.whl (7.7 kB)
Collecting jsii<2.0.0,>=1.74.0
  Using cached jsii-1.74.0-py3-none-any.whl (571 kB)
Collecting typeguard~=2.13.3
  Using cached typeguard-2.13.3-py3-none-any.whl (17 kB)
Collecting aws-cdk.asset-kubectl-v20<3.0.0,>=2.1.1
  Using cached aws_cdk.asset_kubectl_v20-2.1.1-py3-none-any.whl (25.4 MB)
Collecting aws-cdk.asset-awscli-v1<3.0.0,>=2.2.52
  Using cached aws_cdk.asset_awscli_v1-2.2.63-py3-none-any.whl (13.4 MB)
Collecting attrs<23.0,>=21.2
  Using cached attrs-22.2.0-py3-none-any.whl (60 kB)
Collecting cattrs<22.3,>=1.8
  Using cached cattrs-22.2.0-py3-none-any.whl (35 kB)
Collecting typing-extensions<5.0,>=3.7
  Using cached typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Collecting python-dateutil
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting exceptiongroup
  Using cached exceptiongroup-1.1.0-py3-none-any.whl (14 kB)
Collecting six>=1.5
  Using cached six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: publication, typing-extensions, typeguard, six, exceptiongroup, attrs, python-dateutil, cattrs, jsii, constructs, aws-cdk.asset-node-proxy-agent-v5, aws-cdk.asset-kubectl-v20, aws-cdk.asset-awscli-v1, aws-cdk-lib
Successfully installed attrs-22.2.0 aws-cdk-lib-2.64.0 aws-cdk.asset-awscli-v1-2.2.63 aws-cdk.asset-kubectl-v20-2.1.1 aws-cdk.asset-node-proxy-agent-v5-2.0.52 cattrs-22.2.0 constructs-10.1.246 exceptiongroup-1.1.0 jsii-1.74.0 publication-0.0.3 python-dateutil-2.8.2 six-1.16.0 typeguard-2.13.3 typing-extensions-4.4.0

[notice] A new release of pip available: 22.3.1 -> 23.0
[notice] To update, run: python.exe -m pip install --upgrade pip
(.venv) PS C:\Users\User\hello-cdk>

スタックの確認

(.venv) PS C:\Users\User\hello-cdk> cdk ls
HelloCdkStack

(.venv) PS C:\Users\User\hello-cdk>

コードの編集

init 時のサンプルアプリそのまま、コメントを外しただけ。 SQSが作られるはず。

from aws_cdk import (
    Duration,
    Stack,
    aws_sqs as sqs,
)
from constructs import Construct

class HelloCdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # The code that defines your stack goes here

        # example resource
        queue = sqs.Queue(
            self, "HelloCdkQueue",
            visibility_timeout=Duration.seconds(300),
        )

テンプレートの合成

cloud formation の定義(YAML)が生成される。

--no-version-reporting オプションをつけるとバージョン情報送信をオプトアウトする。1

(.venv) PS C:\Users\User\hello-cdk> cdk --no-version-reporting synth
Resources:
  HelloCdkQueueB56C77B9:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 300
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: HelloCdkStack/HelloCdkQueue/Resource
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
  CheckBootstrapVersion:
    Assertions:
      - Assert:
          Fn::Not:
            - Fn::Contains:
                - - "1"
                  - "2"
                  - "3"
                  - "4"
                  - "5"
                - Ref: BootstrapVersion
        AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.


(.venv) PS C:\Users\User\hello-cdk>

デプロイ

(.venv) PS C:\Users\User\hello-cdk> cdk deploy

  Synthesis time: 26.61s

HelloCdkStack: building assets...

[0%] start: Building 226459a13a982bc81cafd4b30fc0f8bfc0d1adfdc2ab749dc21835f5128b7bc3:current_account-ap-northeast-1
[100%] success: Built 226459a13a982bc81cafd4b30fc0f8bfc0d1adfdc2ab749dc21835f5128b7bc3:current_account-ap-northeast-1

HelloCdkStack: assets built

HelloCdkStack: deploying... [1/1]
[0%] start: Publishing 226459a13a982bc81cafd4b30fc0f8bfc0d1adfdc2ab749dc21835f5128b7bc3:current_account-ap-northeast-1
[100%] success: Published 226459a13a982bc81cafd4b30fc0f8bfc0d1adfdc2ab749dc21835f5128b7bc3:current_account-ap-northeast-1
HelloCdkStack: creating CloudFormation changeset...
HelloCdkStack | 0/3 | 14:38:11 | REVIEW_IN_PROGRESS   | AWS::CloudFormation::Stack | HelloCdkStack User Initiated
HelloCdkStack | 0/3 | 14:38:16 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | HelloCdkStack User Initiated
HelloCdkStack | 0/3 | 14:38:20 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 0/3 | 14:38:21 | CREATE_IN_PROGRESS   | AWS::SQS::Queue    | HelloCdkQueue (HelloCdkQueueB56C77B9)
HelloCdkStack | 0/3 | 14:38:22 | CREATE_IN_PROGRESS   | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata) Resource creation Initiated
HelloCdkStack | 1/3 | 14:38:22 | CREATE_COMPLETE      | AWS::CDK::Metadata | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 1/3 | 14:38:23 | CREATE_IN_PROGRESS   | AWS::SQS::Queue    | HelloCdkQueue (HelloCdkQueueB56C77B9) Resource creation Initiated
1/3 Currently in progress: HelloCdkStack, HelloCdkQueueB56C77B9
HelloCdkStack | 2/3 | 14:39:35 | CREATE_COMPLETE      | AWS::SQS::Queue    | HelloCdkQueue (HelloCdkQueueB56C77B9)
HelloCdkStack | 3/3 | 14:39:36 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | HelloCdkStack

   HelloCdkStack

  Deployment time: 89.21s

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxxxx:stack/HelloCdkStack/xxxxxxxxxx-a905-11ed-aed6-0acf0779561b

  Total time: 115.82s


(.venv) PS C:\Users\User\hello-cdk>

確認

確かにできている

(.venv) PS C:\Users\User\hello-cdk> aws sqs list-queues --query="QueueUrls[?contains(@, 'HelloCdkQueue')]"
[
    "https://sqs.ap-northeast-1.amazonaws.com/xxxxxxxxxxxxxx/HelloCdkStack-HelloCdkQueueB56C77B9-XnsgaUJVz4XF"
]

(.venv) PS C:\Users\User\hello-cdk>

変更

先ほどのコードを下記に変更

from aws_cdk import (
    Duration,
    Stack,
    RemovalPolicy,   # ここを追記
    aws_sqs as sqs,
    aws_s3 as s3,    # ここを追記
)
from constructs import Construct

class HelloCdkStack(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # The code that defines your stack goes here

        # example resource
        queue = sqs.Queue(
            self, "HelloCdkQueue",
            visibility_timeout=Duration.seconds(300),
        )

        # ここを追記
        bucket = s3.Bucket(self, "MyFirstBucket",
            versioned=True,
            removal_policy=RemovalPolicy.DESTROY,
            auto_delete_objects=True)

で、差異を確認する。 IAMも自動的にいい感じにしてくれるみたい。

(.venv) PS C:\Users\User\hello-cdk> cdk diff
Stack HelloCdkStack
IAM Statement Changes
┌───┬────────────────────────────────────────┬────────┬────────────────────────────────────────┬────────────────────────────────────────┬───────────┐
    Resource                                Effect  Action                                  Principal                               Condition 
├───┼────────────────────────────────────────┼────────┼────────────────────────────────────────┼────────────────────────────────────────┼───────────┤
 +  ${Custom::S3AutoDeleteObjectsCustomRes  Allow   sts:AssumeRole                          Service:lambda.amazonaws.com                      
    ourceProvider/Role.Arn}                                                                                                                   
├───┼────────────────────────────────────────┼────────┼────────────────────────────────────────┼────────────────────────────────────────┼───────────┤
 +  ${MyFirstBucket.Arn}                    Allow   s3:DeleteObject*                        AWS:${Custom::S3AutoDeleteObjectsCusto            
    ${MyFirstBucket.Arn}/*                          s3:GetBucket*                           mResourceProvider/Role.Arn}                       
                                                    s3:List*                                                                                  
└───┴────────────────────────────────────────┴────────┴────────────────────────────────────────┴────────────────────────────────────────┴───────────┘
IAM Policy Changes
┌───┬───────────────────────────────────────────────────────────────────────┬───────────────────────────────────────────────────────────────────────┐
    Resource                                                               Managed Policy ARN                                                    
├───┼───────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────┤
 +  ${Custom::S3AutoDeleteObjectsCustomResourceProvider/Role}              {"Fn::Sub":"arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLam │
│   │                                                                       │ bdaBasicExecutionRole"}                                               
└───┴───────────────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

Resources
[+] AWS::S3::Bucket MyFirstBucket MyFirstBucketB8884501
[+] AWS::S3::BucketPolicy MyFirstBucket/Policy MyFirstBucketPolicy3243DEFD
[+] Custom::S3AutoDeleteObjects MyFirstBucket/AutoDeleteObjectsCustomResource MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E
[+] AWS::IAM::Role Custom::S3AutoDeleteObjectsCustomResourceProvider/Role CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092
[+] AWS::Lambda::Function Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F


(.venv) PS C:\Users\User\hello-cdk>

deploy

(.venv) PS C:\Users\User\hello-cdk> cdk deploy

  Synthesis time: 23.76s

HelloCdkStack: building assets...

[0%] start: Building 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[0%] start: Building a7d5c24b451838cfdb8782500924eba36d43b7a705dcfc0ffd257a64edd6831a:current_account-ap-northeast-1
[50%] success: Built 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[100%] success: Built a7d5c24b451838cfdb8782500924eba36d43b7a705dcfc0ffd257a64edd6831a:current_account-ap-northeast-1

HelloCdkStack: assets built

This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:

IAM Statement Changes
┌───┬────────────────────────────────────────┬────────┬────────────────────────────────────────┬────────────────────────────────────────┬───────────┐
    Resource                                Effect  Action                                  Principal                               Condition 
├───┼────────────────────────────────────────┼────────┼────────────────────────────────────────┼────────────────────────────────────────┼───────────┤
 +  ${Custom::S3AutoDeleteObjectsCustomRes  Allow   sts:AssumeRole                          Service:lambda.amazonaws.com                      
    ourceProvider/Role.Arn}                                                                                                                   
├───┼────────────────────────────────────────┼────────┼────────────────────────────────────────┼────────────────────────────────────────┼───────────┤
 +  ${MyFirstBucket.Arn}                    Allow   s3:DeleteObject*                        AWS:${Custom::S3AutoDeleteObjectsCusto            
    ${MyFirstBucket.Arn}/*                          s3:GetBucket*                           mResourceProvider/Role.Arn}                       
                                                    s3:List*                                                                                  
└───┴────────────────────────────────────────┴────────┴────────────────────────────────────────┴────────────────────────────────────────┴───────────┘
IAM Policy Changes
┌───┬───────────────────────────────────────────────────────────────────────┬───────────────────────────────────────────────────────────────────────┐
    Resource                                                               Managed Policy ARN                                                    
├───┼───────────────────────────────────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────┤
 +  ${Custom::S3AutoDeleteObjectsCustomResourceProvider/Role}              {"Fn::Sub":"arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLam │
│   │                                                                       │ bdaBasicExecutionRole"}                                               
└───┴───────────────────────────────────────────────────────────────────────┴───────────────────────────────────────────────────────────────────────┘
(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)

Do you wish to deploy these changes (y/n)? y
HelloCdkStack: deploying... [1/1]
[0%] start: Publishing 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[0%] start: Publishing a7d5c24b451838cfdb8782500924eba36d43b7a705dcfc0ffd257a64edd6831a:current_account-ap-northeast-1
[50%] success: Published a7d5c24b451838cfdb8782500924eba36d43b7a705dcfc0ffd257a64edd6831a:current_account-ap-northeast-1
[100%] success: Published 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
HelloCdkStack: creating CloudFormation changeset...
HelloCdkStack | 0/8 | 15:33:14 | UPDATE_IN_PROGRESS   | AWS::CloudFormation::Stack  | HelloCdkStack User Initiated
HelloCdkStack | 0/8 | 15:33:19 | CREATE_IN_PROGRESS   | AWS::S3::Bucket             | MyFirstBucket (MyFirstBucketB8884501)
HelloCdkStack | 0/8 | 15:33:19 | CREATE_IN_PROGRESS   | AWS::IAM::Role              | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
HelloCdkStack | 0/8 | 15:33:20 | UPDATE_IN_PROGRESS   | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 0/8 | 15:33:20 | CREATE_IN_PROGRESS   | AWS::IAM::Role              | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092) Resource creation Initiated
HelloCdkStack | 0/8 | 15:33:20 | CREATE_IN_PROGRESS   | AWS::S3::Bucket             | MyFirstBucket (MyFirstBucketB8884501) Resource creation Initiated
HelloCdkStack | 1/8 | 15:33:21 | UPDATE_COMPLETE      | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 2/8 | 15:33:42 | CREATE_COMPLETE      | AWS::S3::Bucket             | MyFirstBucket (MyFirstBucketB8884501)
HelloCdkStack | 3/8 | 15:33:57 | CREATE_COMPLETE      | AWS::IAM::Role              | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
HelloCdkStack | 3/8 | 15:33:59 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy       | MyFirstBucket/Policy (MyFirstBucketPolicy3243DEFD)
HelloCdkStack | 3/8 | 15:33:59 | CREATE_IN_PROGRESS   | AWS::Lambda::Function       | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
HelloCdkStack | 3/8 | 15:34:01 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy       | MyFirstBucket/Policy (MyFirstBucketPolicy3243DEFD) Resource creation Initiated
HelloCdkStack | 4/8 | 15:34:01 | CREATE_COMPLETE      | AWS::S3::BucketPolicy       | MyFirstBucket/Policy (MyFirstBucketPolicy3243DEFD)
HelloCdkStack | 4/8 | 15:34:01 | CREATE_IN_PROGRESS   | AWS::Lambda::Function       | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F) Resource creation Initiated
HelloCdkStack | 5/8 | 15:34:08 | CREATE_COMPLETE      | AWS::Lambda::Function       | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
HelloCdkStack | 5/8 | 15:34:10 | CREATE_IN_PROGRESS   | Custom::S3AutoDeleteObjects | MyFirstBucket/AutoDeleteObjectsCustomResource/Default (MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E)
HelloCdkStack | 5/8 | 15:34:17 | CREATE_IN_PROGRESS   | Custom::S3AutoDeleteObjects | MyFirstBucket/AutoDeleteObjectsCustomResource/Default (MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E) Resource creation Initiated
HelloCdkStack | 6/8 | 15:34:18 | CREATE_COMPLETE      | Custom::S3AutoDeleteObjects | MyFirstBucket/AutoDeleteObjectsCustomResource/Default (MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E)
HelloCdkStack | 7/8 | 15:34:19 | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack  | HelloCdkStack
HelloCdkStack | 8/8 | 15:34:20 | UPDATE_COMPLETE      | AWS::CloudFormation::Stack  | HelloCdkStack

   HelloCdkStack

  Deployment time: 79.82s

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxxxx:stack/HelloCdkStack/xxxxxxxxxx-a905-11ed-aed6-0acf0779561b

  Total time: 103.58s


(.venv) PS C:\Users\User\hello-cdk>

diffがなくなったことを確認

(.venv) PS C:\Users\User\hello-cdk> cdk diff
Stack HelloCdkStack
There were no differences

(.venv) PS C:\Users\User\hello-cdk>

できてる

(.venv) PS C:\Users\User\hello-cdk>  aws s3api list-buckets --query "Buckets[?contains(Name,'myfirstbucket')]"
[
    {
        "Name": "hellocdkstack-myfirstbucketb8884501-xrt0jv1xbbwj",
        "CreationDate": "2023-02-04T06:34:01+00:00"
    }
]

(.venv) PS C:\Users\User\hello-cdk>

リソース削除

SQSの部分をコメントして確認

(.venv) PS C:\Users\User\hello-cdk> cdk diff
Stack HelloCdkStack
Resources
[-] AWS::SQS::Queue HelloCdkQueueB56C77B9 destroy


(.venv) PS C:\Users\User\hello-cdk>

いざ削除

(.venv) PS C:\Users\User\hello-cdk> cdk deploy

  Synthesis time: 27s

HelloCdkStack: building assets...

[0%] start: Building 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[0%] start: Building 13f95fda4209bb39b3f0b951fa63d642bdc28c24e502de7a744c2e34c2587755:current_account-ap-northeast-1
[50%] success: Built 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[100%] success: Built 13f95fda4209bb39b3f0b951fa63d642bdc28c24e502de7a744c2e34c2587755:current_account-ap-northeast-1

HelloCdkStack: assets built

HelloCdkStack: deploying... [1/1]
[0%] start: Publishing 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[0%] start: Publishing 13f95fda4209bb39b3f0b951fa63d642bdc28c24e502de7a744c2e34c2587755:current_account-ap-northeast-1
[50%] success: Published 2332a8953f2d92ebffdc01cf20d5a2fb5bf2ef29764cda4186f01c55edee8c73:current_account-ap-northeast-1
[100%] success: Published 13f95fda4209bb39b3f0b951fa63d642bdc28c24e502de7a744c2e34c2587755:current_account-ap-northeast-1
HelloCdkStack: creating CloudFormation changeset...
HelloCdkStack | 0/4 | 17:14:13 | UPDATE_IN_PROGRESS   | AWS::CloudFormation::Stack  | HelloCdkStack User Initiated
HelloCdkStack | 0/4 | 17:14:18 | UPDATE_IN_PROGRESS   | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 1/4 | 17:14:19 | UPDATE_COMPLETE      | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack | 2/4 | 17:14:21 | UPDATE_COMPLETE_CLEA | AWS::CloudFormation::Stack  | HelloCdkStack
HelloCdkStack | 2/4 | 17:14:22 | DELETE_IN_PROGRESS   | AWS::SQS::Queue             | HelloCdkQueueB56C77B9
2/4 Currently in progress: HelloCdkStack, HelloCdkQueueB56C77B9
HelloCdkStack | 3/4 | 17:15:24 | DELETE_COMPLETE      | AWS::SQS::Queue             | HelloCdkQueueB56C77B9
HelloCdkStack | 4/4 | 17:15:24 | UPDATE_COMPLETE      | AWS::CloudFormation::Stack  | HelloCdkStack

   HelloCdkStack

  Deployment time: 79.01s

Stack ARN:
arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxxxx:stack/HelloCdkStack/xxxxxxxxxx-a905-11ed-aed6-0acf0779561b

  Total time: 106.01s


(.venv) PS C:\Users\User\hello-cdk> aws sqs list-queues --query="QueueUrls[?contains(@, 'Hello')]"
null

(.venv) PS C:\Users\User\hello-cdk>

削除できた

全部削除

(.venv) PS C:\Users\User\hello-cdk> cdk destroy
Are you sure you want to delete: HelloCdkStack (y/n)? y
HelloCdkStack: destroying... [1/1]
HelloCdkStack |   0 | 17:16:47 | DELETE_IN_PROGRESS   | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack |   0 | 17:16:47 | DELETE_IN_PROGRESS   | Custom::S3AutoDeleteObjects | MyFirstBucket/AutoDeleteObjectsCustomResource/Default (MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E)
HelloCdkStack |   1 | 17:16:48 | DELETE_COMPLETE      | AWS::CDK::Metadata          | CDKMetadata/Default (CDKMetadata)
HelloCdkStack |   2 | 17:16:55 | DELETE_COMPLETE      | Custom::S3AutoDeleteObjects | MyFirstBucket/AutoDeleteObjectsCustomResource/Default (MyFirstBucketAutoDeleteObjectsCustomResourceC52FCF6E)
HelloCdkStack |   2 | 17:16:56 | DELETE_IN_PROGRESS   | AWS::Lambda::Function       | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
HelloCdkStack |   2 | 17:16:56 | DELETE_IN_PROGRESS   | AWS::S3::BucketPolicy       | MyFirstBucket/Policy (MyFirstBucketPolicy3243DEFD)
HelloCdkStack |   3 | 17:16:57 | DELETE_COMPLETE      | AWS::S3::BucketPolicy       | MyFirstBucket/Policy (MyFirstBucketPolicy3243DEFD)
HelloCdkStack |   4 | 17:17:02 | DELETE_COMPLETE      | AWS::Lambda::Function       | Custom::S3AutoDeleteObjectsCustomResourceProvider/Handler (CustomS3AutoDeleteObjectsCustomResourceProviderHandler9D90184F)
HelloCdkStack |   4 | 17:17:03 | DELETE_IN_PROGRESS   | AWS::S3::Bucket             | MyFirstBucket (MyFirstBucketB8884501)
HelloCdkStack |   4 | 17:17:03 | DELETE_IN_PROGRESS   | AWS::IAM::Role              | Custom::S3AutoDeleteObjectsCustomResourceProvider/Role (CustomS3AutoDeleteObjectsCustomResourceProviderRole3B1BD092)
HelloCdkStack |   5 | 17:17:04 | DELETE_COMPLETE      | AWS::S3::Bucket             | MyFirstBucket (MyFirstBucketB8884501)

   HelloCdkStack: destroyed

(.venv) PS C:\Users\User\hello-cdk>

この状態でも Cloud Formationの Stackが残っている

(.venv) PS C:\Users\User\hello-cdk> aws cloudformation list-stacks --query "StackSummaries[?contains(StackName, 'CDK')]" --region ap-northeast-1
[
    {
        "StackId": "arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxxxx:stack/CDKToolkit/xxxxxxxxxx-a8ea-11ed-a5c9-0ea58eccf311",
        "StackName": "CDKToolkit",
        "TemplateDescription": "This stack includes resources needed to deploy AWS CDK apps into this environment",
        "CreationTime": "2023-02-04T02:25:32.068000+00:00",
        "LastUpdatedTime": "2023-02-04T02:25:37.962000+00:00",
        "StackStatus": "CREATE_COMPLETE",
        "DriftInformation": {
            "StackDriftStatus": "NOT_CHECKED"
        }
    }
]

(.venv) PS C:\Users\User\hello-cdk>

Stack 削除

(.venv) PS C:\Users\User\hello-cdk> aws cloudformation delete-stack --stack-name CDKToolkit
(.venv) PS C:\Users\User\hello-cdk> aws cloudformation list-stacks --query "StackSummaries[?contains(StackName, 'CDK')]" --region ap-northeast-1
[
    {
        "StackId": "arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxxxx:stack/CDKToolkit/xxxxxxxxxx-a8ea-11ed-a5c9-0ea58eccf311",
        "StackName": "CDKToolkit",
        "TemplateDescription": "This stack includes resources needed to deploy AWS CDK apps into this environment",
        "CreationTime": "2023-02-04T02:25:32.068000+00:00",
        "LastUpdatedTime": "2023-02-04T02:25:37.962000+00:00",
        "DeletionTime": "2023-02-04T08:48:47.296000+00:00",
        "StackStatus": "DELETE_COMPLETE",
        "DriftInformation": {
            "StackDriftStatus": "NOT_CHECKED"
        }
    }
]

(.venv) PS C:\Users\User\hello-cdk>

これで bootstrap で作成されたものはほぼ削除された。ただし S3 は残っている。