Using uv with the Python AWS CDK
How to initialise a Python AWS CDK app along with the uv package and project manager.
Purpose
The AWS CDK for Python includes a requirements.txt file with the official instructions explaining how to initialise a CDK app with pip
and a requirements.txt
file. If you are using the uv package and project manager, you may prefer to setup your environment with a pyproject.toml
and a specific version of Python. This article is a short adaption to the official AWS CDK Tutorial on how to do both!
Assumptions
Background
The following steps can be used in place of step 1.2 of the official AWS CDK Tutorial: Create your first AWS CDK app.
Procedure
- Ensure that you are in the directory that you wish to use for your AWS CDK app per step 1.1 of the official tutorial.
- Initialise the CDK app using Python:
cdk init app --language python
- Initialise a uv project within the same directory:
uv init --python 3.12
Arguments
- --python [VERSION]
- Use a specific Python version (3.12 in the above case). This argument is only required if you need to change from your default Python version.
- Activate the virtual environment:
source .venv/bin/activate # On Windows, run `.\venv\Scripts\activate` instead
- Add the contents of the CDK
requirements.txt
file to thepyproject.toml
dependencies:uv add -r requirements.txt
- Add the contents of the CDK
requirements-dev.txt
file to thepyproject.toml
dev dependencies:uv add --dev -r requirements-dev.txt
- Remove the requirements files to minimise any future confusion:
rm -f requirements.txt requirements-dev.txt
- Continue with the official AWS CDK Tutorial from step 2.
Conclusion
Once the above steps are complete, you should be able to use the uv CLI for project and package management in addition to the AWS CDK CLI commands from within the project directory.