まずこれに沿って https://aws.amazon.com/jp/getting-started/guides/setup-cdk/module-one/

Python On Windows で行きます

node.js のインストール

Pythonで開発するにしても前提でnode.jsが必要。まじか。

node.jsのバージョン管理には、nvm-windows、nvs、voltaなどがあるらしい1が、nvs が一番導入が楽そう(on Windows 11)なのでこれにする。

https://github.com/jasongin/nvs に従って、

PS C:\> winget install jasongin.nvs
'msstore' ソースでは、使用する前に次の契約を表示する必要があります。
Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction
ソースが正常に機能するには、現在のマシンの 2 文字の地理的リージョンをバックエンド サービスに送信する必要があります (例: "US")

すべてのソース契約条件に同意しますか?
[Y] はい  [N] いいえ: y
見つかりました NVS [jasongin.nvs] バージョン 1.6.2
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
ダウンロード中 https://github.com/jasongin/nvs/releases/download/v1.6.2/nvs-1.6.2.msi
  ██████████████████████████████   928 KB /  928 KB
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました

https://aws.amazon.com/jp/getting-started/guides/setup-cdk/module-one/ によると新しめのLTSならよさそうなので、LTSを入れる

AWS CDK は、特定のバージョンの Node.js を使用します (バージョン 13.0.0~13.6.0 を除く10.13.0 以上)。アクティブな長期サポート (LTS) のバージョンをお勧めします。

PS C:\> nvs add lts
Downloading [#########################################################################################################################] 100%
Extracting  [#########################################################################################################################] 100%
Added at: $env:LOCALAPPDATA\nvs\node\18.14.0\x64\node.exe
To use this version now: nvs use node/18.14.0/x64
PS C:\>
PS C:\> nvs use lts
PATH += $env:LOCALAPPDATA\nvs\node\18.14.0\x64
PS C:\> node -v
v18.14.0
PS C:\>

うおー、簡単すぎる。

cdk 導入

PS C:\Users\User> npm install -g aws-cdk

added 1 package in 3s
npm notice
npm notice New minor version of npm available! 9.3.1 -> 9.4.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v9.4.2
npm notice Run npm install -g npm@9.4.2 to update!
npm notice
PS C:\Users\User> cdk
Usage: cdk -a <cdk-app> COMMAND

Commands:
  cdk list [STACKS..]             Lists all stacks in the app      [aliases: ls]
  cdk synthesize [STACKS..]       Synthesizes and prints the CloudFormation
                                  template for this stack       [aliases: synth]
  cdk bootstrap [ENVIRONMENTS..]  Deploys the CDK toolkit stack into an AWS
                                  environment
  cdk deploy [STACKS..]           Deploys the stack(s) named STACKS into your
                                  AWS account
  cdk import [STACK]              Import existing resource(s) into the given
                                  STACK
  cdk watch [STACKS..]            Shortcut for 'deploy --watch'
  cdk destroy [STACKS..]          Destroy the stack(s) named STACKS
  cdk diff [STACKS..]             Compares the specified stack with the deployed
                                  stack or a local template file, and returns
                                  with status 1 if any difference is found
  cdk metadata [STACK]            Returns all metadata associated with this
                                  stack
  cdk acknowledge [ID]            Acknowledge a notice so that it does not show
                                  up anymore                      [aliases: ack]
  cdk notices                     Returns a list of relevant notices
  cdk init [TEMPLATE]             Create a new, empty CDK project from a
                                  template.
  cdk context                     Manage cached context values
  cdk docs                        Opens the reference documentation in a browser
                                                                  [aliases: doc]
  cdk doctor                      Check your set-up for potential problems

Options:
  -a, --app                REQUIRED WHEN RUNNING APP: command-line for executing
                           your app or a cloud assembly directory (e.g. "node
                           bin/my-app.js"). Can also be specified in cdk.json or
                           ~/.cdk.json                                  [string]
      --build              Command-line for a pre-synth build           [string]
  -c, --context            Add contextual string parameter (KEY=VALUE)   [array]
  -p, --plugin             Name or path of a node package that extend the CDK
                           features. Can be specified multiple times     [array]
      --trace              Print trace for stack warnings              [boolean]
      --strict             Do not construct stacks with warnings       [boolean]
      --lookups            Perform context lookups (synthesis fails if this is
                           disabled and context lookups need to be performed)
                                                       [boolean] [default: true]
      --ignore-errors      Ignores synthesis errors, which will likely produce
                           an invalid output          [boolean] [default: false]
  -j, --json               Use JSON output instead of YAML when templates are
                           printed to STDOUT          [boolean] [default: false]
  -v, --verbose            Show debug logs (specify multiple times to increase
                           verbosity)                   [count] [default: false]
      --debug              Enable emission of additional debugging information,
                           such as creation stack traces of tokens
                                                      [boolean] [default: false]
      --profile            Use the indicated AWS profile as the default
                           environment                                  [string]
      --proxy              Use the indicated proxy. Will read from HTTPS_PROXY
                           environment variable if not specified        [string]
      --ca-bundle-path     Path to CA certificate to use when validating HTTPS
                           requests. Will read from AWS_CA_BUNDLE environment
                           variable if not specified                    [string]
  -i, --ec2creds           Force trying to fetch EC2 instance credentials.
                           Default: guess EC2 instance status          [boolean]
      --version-reporting  Include the "AWS::CDK::Metadata" resource in
                           synthesized templates (enabled by default)  [boolean]
      --path-metadata      Include "aws:cdk:path" CloudFormation metadata for
                           each resource (enabled by default)
                                                       [boolean] [default: true]
      --asset-metadata     Include "aws:asset:*" CloudFormation metadata for
                           resources that uses assets (enabled by default)
                                                       [boolean] [default: true]
  -r, --role-arn           ARN of Role to use when invoking CloudFormation
                                                                        [string]
      --staging            Copy assets to the output directory (use --no-staging
                           to disable the copy of assets which allows local
                           debugging via the SAM CLI to reference the original
                           source files)               [boolean] [default: true]
  -o, --output             Emits the synthesized cloud assembly into a directory
                           (default: cdk.out)                           [string]
      --notices            Show relevant notices                       [boolean]
      --no-color           Removes colors and other style from console output
                                                      [boolean] [default: false]
      --ci                 Force CI detection. If CI=true then logs will be sent
                           to stdout instead of stderr[boolean] [default: false]
      --version            Show version number                         [boolean]
  -h, --help               Show help                                   [boolean]

If your app has a single stack, there is no need to specify the stack name

If one of cdk.json or ~/.cdk.json exists, options specified there will be used
as defaults. Settings in cdk.json take precedence.

PS C:\Users\User>

入りました。

aws

aws とのやりとりは実際には aws-cli が司る。

https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/getting-started-install.html

msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

aws-cli-1.png aws-cli-2.png aws-cli-3.png aws-cli-4.png aws-cli-5.png

PS C:\Users\User> aws configure
AWS Access Key ID [None]: xxxxxxxxxxxxxx
AWS Secret Access Key [None]: ***********************************
Default region name [None]: ap-northeast-1
Default output format [None]:
PS C:\Users\User>

bootstrap

bootstrapでAWSアカウントに対してCDK用のIAM設定とS3バケットが作成される。 本当は用途によって適切なテンプレート設計をして、それをbootstrapに与える必要があるがとりあえずデフォルトで。

PS C:\Users\User> cdk bootstrap aws://xxxxxxxxxxxxxx/ap-northeast-1
   Bootstrapping environment aws://xxxxxxxxxxxxxx/ap-northeast-1...
Trusted accounts for deployment: (none)
Trusted accounts for lookup: (none)
Using default execution policy of 'arn:aws:iam::aws:policy/AdministratorAccess'. Pass '--cloudformation-execution-policies' to customize.
CDKToolkit: creating CloudFormation changeset...
CDKToolkit |  0/12 | 11:25:32 | REVIEW_IN_PROGRESS   | AWS::CloudFormation::Stack | CDKToolkit User Initiated
CDKToolkit |  0/12 | 11:25:37 | CREATE_IN_PROGRESS   | AWS::CloudFormation::Stack | CDKToolkit User Initiated
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | LookupRole
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | FilePublishingRole
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | CloudFormationExecutionRole
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::S3::Bucket         | StagingBucket
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | ImagePublishingRole
CDKToolkit |  0/12 | 11:25:42 | CREATE_IN_PROGRESS   | AWS::SSM::Parameter     | CdkBootstrapVersion
CDKToolkit |  0/12 | 11:25:43 | CREATE_IN_PROGRESS   | AWS::ECR::Repository    | ContainerAssetsRepository
CDKToolkit |  0/12 | 11:25:43 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | FilePublishingRole Resource creation Initiated
CDKToolkit |  0/12 | 11:25:43 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | LookupRole Resource creation Initiated
CDKToolkit |  0/12 | 11:25:43 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | CloudFormationExecutionRole Resource creation Initiated
CDKToolkit |  0/12 | 11:25:43 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | ImagePublishingRole Resource creation Initiated
CDKToolkit |  0/12 | 11:25:44 | CREATE_IN_PROGRESS   | AWS::SSM::Parameter     | CdkBootstrapVersion Resource creation Initiated
CDKToolkit |  0/12 | 11:25:44 | CREATE_IN_PROGRESS   | AWS::ECR::Repository    | ContainerAssetsRepository Resource creation Initiated
CDKToolkit |  1/12 | 11:25:45 | CREATE_COMPLETE      | AWS::ECR::Repository    | ContainerAssetsRepository
CDKToolkit |  2/12 | 11:25:45 | CREATE_COMPLETE      | AWS::SSM::Parameter     | CdkBootstrapVersion
CDKToolkit |  2/12 | 11:25:45 | CREATE_IN_PROGRESS   | AWS::S3::Bucket         | StagingBucket Resource creation Initiated
CDKToolkit |  3/12 | 11:26:07 | CREATE_COMPLETE      | AWS::S3::Bucket         | StagingBucket
CDKToolkit |  3/12 | 11:26:09 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy   | StagingBucketPolicy
CDKToolkit |  3/12 | 11:26:10 | CREATE_IN_PROGRESS   | AWS::S3::BucketPolicy   | StagingBucketPolicy Resource creation Initiated
CDKToolkit |  4/12 | 11:26:10 | CREATE_COMPLETE      | AWS::S3::BucketPolicy   | StagingBucketPolicy
CDKToolkit |  5/12 | 11:26:26 | CREATE_COMPLETE      | AWS::IAM::Role          | FilePublishingRole
CDKToolkit |  6/12 | 11:26:26 | CREATE_COMPLETE      | AWS::IAM::Role          | CloudFormationExecutionRole
CDKToolkit |  7/12 | 11:26:26 | CREATE_COMPLETE      | AWS::IAM::Role          | ImagePublishingRole
CDKToolkit |  8/12 | 11:26:27 | CREATE_COMPLETE      | AWS::IAM::Role          | LookupRole
CDKToolkit |  8/12 | 11:26:28 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy
CDKToolkit |  8/12 | 11:26:29 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy
CDKToolkit |  8/12 | 11:26:29 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | DeploymentActionRole
CDKToolkit |  8/12 | 11:26:29 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy Resource creation Initiated
CDKToolkit |  8/12 | 11:26:30 | CREATE_IN_PROGRESS   | AWS::IAM::Role          | DeploymentActionRole Resource creation Initiated
CDKToolkit |  8/12 | 11:26:30 | CREATE_IN_PROGRESS   | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy Resource creation Initiated
 8/12 Currently in progress: CDKToolkit, FilePublishingRoleDefaultPolicy, ImagePublishingRoleDefaultPolicy, DeploymentActionRole
CDKToolkit |  9/12 | 11:27:12 | CREATE_COMPLETE      | AWS::IAM::Policy        | FilePublishingRoleDefaultPolicy
CDKToolkit | 10/12 | 11:27:12 | CREATE_COMPLETE      | AWS::IAM::Policy        | ImagePublishingRoleDefaultPolicy
CDKToolkit | 11/12 | 11:27:13 | CREATE_COMPLETE      | AWS::IAM::Role          | DeploymentActionRole
CDKToolkit | 12/12 | 11:27:16 | CREATE_COMPLETE      | AWS::CloudFormation::Stack | CDKToolkit
   Environment aws://xxxxxxxxxxxxxx/ap-northeast-1 bootstrapped.

PS C:\Users\User>

以上で開発環境構築は終わり。