Set Up the Python CDK
How do you set up the AWS Python CDK and get ready to deploy infrastructure?

The Python CDK is a fantastic way to define AWS infrastructure in beloved, beautiful Python. Because who wants to learn TypeScript anyway? This tutorial will show you how to set it up so you’re prepared to deploy stacks!
Step 0 — Prerequisites
If you’re looking to use the Python CDK for infrastructure, I’m assuming you already have Python installed. If not, you can get the latest version here:
Download it, install it, and move on to the first CDK-specific step.
Step 1 — Install Node.js
The Python CDK is an auto-generated wrapper around the TypeScript CDK. Therefore, you need the Typescript version installed to use it.
First of all, you need Node.js, a JavaScript runtime that lets you run JavaScript and TypeScript code outside of the browser. It’s built on the Chromium V8 Engine. You can get it via this link:
Or, assuming you’re on Windows and have Chocolatey installed, you can run this command to install it:
choco install nodejs-lts -y
This installation will make a command-line tool: npm available to you. You can check it worked by running:
npm --version
You’ll see an output like: 10.5.0.
Step 2 — Install the Typescript CDK
npm is short for the Node Package Manager, it’s the TypeScript + JavaScript ecosystems pip. You can use it to install the CDK with this command:
npm install -g aws-cdk
This will install a global version (-g for global) of the aws-cdk package and make it usable anywhere on your system. You can check it worked by running:
cdk --version
You should see output like this: 2.171.1 (build a95560c)
Step 3 — Set Up Your First Stack
With the tools installed, you can set up your first infrastructure stack. First, create a directory for your stack:
mkdir my_first_python_stack
Then, enter the directory and run the following command:
cdk init app --language python
This will populate your directory with a basic stack and all the necessary tools to run the CDK. It also comes with a README.md explaining the next steps for setting up your development environment. Ignore these steps, there’s a better way.
Step 4 — Use Poetry, Not Virtual Env
The README.md file tells you to use Virtual Env for environment management, but you shouldn’t. Poetry is a much better tool. If you don’t already have it, you can install it with:
pip install poetry
Initialise Poetry
In your CDK project’s root directory, you will see the following layout:
/.venv
You can safely delete the whole: /.venv directory, and source.bat (Windows only). These are here to help you set up a virtual environment, but we’re handling that with Poetry. Initialise a Poetry project with:
poetry init
Follow the CLI interface and create the project. This will give you a new file:
pyproject.toml
This is your project’s definition, which allows you to rebuild your project’s environment on other machines easily.
Prepare Your Environment
Set up a virtual environment for your project with:
poetry shell
The requirements.txt and requirements-dev.txt files, located in the standard directory structure, list all the Python packages your project needs to run the CDK. You can add these dependencies to your Poetry environment with:
poetry add $(cat requirements.txt)
and then the requirements-dev.txt file with:
poetry add --group dev $(cat requirements-dev.txt)
The --group dev flag puts these dependencies in a separate location so when you pass your pyproject.toml file to another machine, they won’t be installed unless you specify that you want to install them.
This step will create a new file: poetry.lock. This contains detailed information about the packages installed in your environment. Because of this file, you can safely delete the requirements files too:
rm requirements.txt, requirements-dev.txt
You’re finished!
That’s it! You have everything set up and you’re ready to start building infrastructure in Python.
Final Words
I hope you found this tutorial helpful. If you have any questions, post them in the comments. If you want to build a simple Lambda using CDK, read this:
The Simplest AWS Lambda in Python
Happy DevOps’ing!
Extras
These are the AWS docs on the Python CDK: