Sub topic 3
Installing the CLI and creating a project in TypeScript or Python
This chapter assumes you have Node.js installed (an active LTS release), a working AWS CLI with credentials configured, and basic knowledge of TypeScript or Python. You do not need to understand constructs yet. The goal is a working project that synthesizes a template.
What you are installing
The CDK has two separate pieces, and confusing them causes many early errors.
- The CDK CLI is the
cdkcommand. It runs your app, then previews and deploys the result. It is published on npm asaws-cdk, even if you write Python. - The construct library is
aws-cdk-lib. It holds the classes you import in your code, such ass3.Bucket. It is installed per project, through npm for TypeScript or pip for Python.
The CLI is a Node.js program, so Node is required for both languages. The CLI and the library have separate version numbers. In CDK v2 the CLI must be at least as new as the library your project uses. If it is older, you will see an error about the cloud assembly schema version.
Install the CLI
npm install -g aws-cdk
cdk --versionA global install gives you one cdk command everywhere. If your team wants a pinned version per project, install it as a dev dependency and run it with npx cdk instead. A project created by cdk init already lists aws-cdk as a dev dependency, so npx cdk synth works inside it even without a global install.
The CLI finds credentials through the standard AWS chain: environment variables, the shared credentials file, or SSO. Use AWS_PROFILE=name or the --profile name flag to choose a profile. Check which identity you are using before you deploy anything:
aws sts get-caller-identityCreate a project
cdk init generates a project from a template. It must run in an empty directory, and the directory name becomes the project name.
mkdir my-app && cd my-app
cdk init app --language typescriptFor Python, change the flag and prepare a virtual environment before installing dependencies:
mkdir my-app && cd my-app
cdk init app --language python
source .venv/bin/activate
pip install -r requirements.txtOn Windows, activate with .venv\Scripts\activate.bat instead. For Python, cdk init creates the .venv directory itself when it can. If it did not, create one with python3 -m venv .venv first. The TypeScript template installs npm packages for you.
Run cdk init --list to see other templates and supported languages. The app template is the right start for this tutorial. It also runs git init and makes a first commit unless you pass --generate-only.
What the template generates
For a project named my-app, the layout is roughly:
my-app/
cdk.json
bin/my-app.ts # entry point: creates the App
lib/my-app-stack.ts # your first Stack
test/my-app.test.ts # Jest test
package.json
tsconfig.jsonThe Python version has the same pieces under different names:
my-app/
cdk.json
app.py # entry point: creates the App
my_app/my_app_stack.py # your first Stack
tests/unit/test_my_app_stack.py
requirements.txt
requirements-dev.txtThe two languages have the same structure. A file creates an App, and the App instantiates one or more Stack classes. The Python names use snake_case, but the model is identical.
cdk.json: how the CLI runs your code
The CLI does not know how your program is written. cdk.json tells it which command to run to execute your app.
{
"app": "npx ts-node --prefer-ts-exts bin/my-app.ts"
}In a Python project the value is python3 app.py. When you run cdk synth, the CLI runs this command, and your program writes its output into the cdk.out directory. That output is the cloud assembly from the previous chapter. The file also holds a context section with feature flags and cached lookups. Do not delete it, and commit it to source control.
Two consequences follow from this design:
- If the command fails, the CLI fails. A TypeScript compile error or a Python import error appears as a
cdk syntherror. - You can change the command. A common change in TypeScript projects is to compile ahead of time and run plain JavaScript, which starts faster than
ts-node.
Verify the setup
Before writing any resources, confirm that the toolchain works end to end:
cdk ls
cdk synthcdk ls prints the stack IDs in your app, for example MyAppStack. cdk synth prints the CloudFormation template. The generated stack is empty, so the template may contain only a CDKMetadata resource, which the CLI adds. Nothing has been created in your account. Neither command deploys anything.
If you want the full flow now, the spine's complete sequence also bootstraps one environment and deploys. Bootstrapping is covered in its own chapter, so treat it as a preview here:
cdk bootstrap aws://123456789012/us-east-1
cdk diff
cdk deployReplace the account ID and region with your own.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `cdk init cannot be run in a non-empty directory` | The directory has files in it | Use a new empty directory |
| Python: `ModuleNotFoundError: aws_cdk` | Virtual environment is not active, or dependencies were not installed | Activate `.venv`, run `pip install -r requirements.txt` |
| Cloud assembly schema version mismatch | CLI is older than the library | Run `npm install -g aws-cdk@latest` |
| `Unable to resolve AWS account to use` | No credentials found | Set `AWS_PROFILE` or run `aws configure` |
| `cdk: command not found` | Global npm bin directory is not on `PATH` | Fix `PATH`, or use `npx cdk` |
Exercise
You run cdk init app --language python and then cdk synth, and it fails with an import error for aws_cdk. What is the most likely cause, and which file tells the CLI which command to run when it synthesizes?
Show answer
The Python virtual environment is probably not active, or pip install -r requirements.txt has not been run, so aws-cdk-lib is missing. The file that tells the CLI how to run the app is cdk.json, through its app field (for Python, python3 app.py).
Exercise
Why is the CLI installed with npm even when your infrastructure code is written in Python?
Show answer
The CLI is a Node.js program published as the npm package aws-cdk. Only the construct library (aws-cdk-lib) is installed with pip for Python projects.