CDKのプロジェクトを作成する場合、先にckdコマンドをグローバルでインストールします。 その後、プロジェクト用ディレクトリを作成して、その中でプロジェクト作成用コマンドを実行します。

コマンドのインストール

参考

上記を参考に最新のcdkツールをインストールする場合、以下を実行してインストールします。

npm install -g aws-cdk

プロジェクト作成

参考

プロジェクト用の空のディレクトリを作成します。 その中に移動してcdk init コマンドを実行します。 cdk init は、空のディレクトリで実行する必要があります。

npx cdk init app --language=[言語]
注意

例 TypeScriptのプロジェクト作成

$ npx cdk init app --language=typescript
Applying project template app for typescript
# Welcome to your CDK TypeScript project

This is a blank project for CDK development with TypeScript.

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

## Useful commands

* `npm run build`   compile typescript to js
* `npm run watch`   watch for changes and compile
* `npm run test`    perform the jest unit tests
* `cdk deploy`      deploy this stack to your default AWS account/region
* `cdk diff`        compare deployed stack with current state
* `cdk synth`       emits the synthesized CloudFormation template

Initializing a new git repository...
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Executing npm install...
✅ All done!

プロジェクトの中身

cdk init app で作成した場合、空のディレクトリは以下のようになります。

$ ls -al
total 212
drwxrwxr-x   7 vagrant vagrant   4096 Jul  1 16:15 .
drwxrwxr-x   3 vagrant vagrant   4096 Jul  1 16:14 ..
drwxrwxr-x   2 vagrant vagrant   4096 Jul  1 16:15 bin
-rw-rw-r--   1 vagrant vagrant   2278 Jul  1 16:15 cdk.json
drwxrwxr-x   8 vagrant vagrant   4096 Jul  1 16:15 .git
-rw-rw-r--   1 vagrant vagrant     93 Jul  1 16:15 .gitignore
-rw-rw-r--   1 vagrant vagrant    157 Jul  1 16:15 jest.config.js
drwxrwxr-x   2 vagrant vagrant   4096 Jul  1 16:15 lib
drwxrwxr-x 217 vagrant vagrant  12288 Jul  1 16:15 node_modules
-rw-rw-r--   1 vagrant vagrant     65 Jul  1 16:15 .npmignore
-rw-rw-r--   1 vagrant vagrant    530 Jul  1 16:15 package.json
-rw-rw-r--   1 vagrant vagrant 149179 Jul  1 16:15 package-lock.json
-rw-rw-r--   1 vagrant vagrant    536 Jul  1 16:15 README.md
drwxrwxr-x   2 vagrant vagrant   4096 Jul  1 16:15 test
-rw-rw-r--   1 vagrant vagrant    663 Jul  1 16:15 tsconfig.json