Using uv with the Python AWS CDK

How to initialise a Python AWS CDK app along with the uv package and project manager.

  1. Purpose
  2. Assumptions
  3. Background
  4. Procedure
  5. Conclusion

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

  • You have installed the AWS CDK
  • You have installed uv

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

  1. 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.
  2. Initialise the CDK app using Python:
    cdk init app --language python
    
  3. 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.
  4. Activate the virtual environment:
    source .venv/bin/activate
    # On Windows, run `.\venv\Scripts\activate` instead
    
  5. Add the contents of the CDK requirements.txt file to the pyproject.toml dependencies:
    uv add -r requirements.txt
    
  6. Add the contents of the CDK requirements-dev.txt file to the pyproject.toml dev dependencies:
    uv add --dev -r requirements-dev.txt
    
  7. Remove the requirements files to minimise any future confusion:
    rm -f requirements.txt requirements-dev.txt
    
  8. 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.


© 2022. All rights reserved.

Powered by Hydejack v9.2.1