Lazy-load aws_cdk submodules to speed up CDK synth
Project description
cdk-lazy-import
Speed up AWS CDK synth in Python by lazy-loading aws_cdk.aws_* service modules.
The problem
The AWS CDK for Python uses jsii to bridge between JavaScript and Python. When you
import aws_cdk.aws_s3, jsii eagerly loads the module and all of its dependencies.
A typical CDK app imports a handful of service modules, but each one pulls in shared
machinery that touches all ~300 service packages. This means cdk synth spends several
seconds just on imports before it even starts synthesizing your stack.
How it works
cdk-lazy-import installs a meta path finder that intercepts import aws_cdk.aws_*
statements. Instead of loading the real module immediately, it returns a lightweight stub.
The stub defers all work until your code actually accesses an attribute (like aws_s3.Bucket),
at which point the real module is loaded transparently.
Non-service submodules (aws_cdk.cx_api, aws_cdk.pipelines, aws_cdk._jsii, etc.) are
not intercepted and load normally. Deeper imports like aws_cdk.aws_s3.notifications are
also handled normally once their parent resolves.
The result is byte-identical CloudFormation output with ~30-40% faster synth times in limited testing (~5.8s vs ~9.0s).
Installation
pip install cdk-lazy-import
That's it. The package includes a .pth file that activates the lazy-loading hook
automatically at interpreter startup. No code changes, no configuration, no manual imports.
If aws-cdk-lib is not installed in the environment, the hook is a silent no-op.
Uninstallation
pip uninstall cdk-lazy-import
Removing the package removes the .pth file and restores default import behavior.
API
For most users, installing the package is sufficient. If you need manual control:
import cdk_lazy_import
# The .pth file calls this automatically, but you can call it yourself
# if you need to install the hook at a specific point. It's a no-op if
# the hook is already installed or aws_cdk is not available.
cdk_lazy_import.install()
Caveats
- Only
aws_cdk.aws_*submodules are lazified. Other submodules load normally. - The hook must be installed before any
aws_cdk.aws_*imports. The.pthfile ensures this happens at interpreter startup, but if something imports CDK modules before.pthfiles are processed (unusual), those modules won't be lazified. - Performance gains depend on how many service modules your app uses. Apps that import nearly every service module will see less benefit.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cdk_lazy_import-1.0.0.tar.gz.
File metadata
- Download URL: cdk_lazy_import-1.0.0.tar.gz
- Upload date:
- Size: 53.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee21929c779c1f10735c3997b4a04716fdbcc6b7d0c0618306b1f55268dda2ea
|
|
| MD5 |
60eab6033e7ffc43626ab000f953bcd4
|
|
| BLAKE2b-256 |
6b4daf2f256227efa5db514d70718955a89735503b5013ae05ed838a416a01dd
|
Provenance
The following attestation bundles were made for cdk_lazy_import-1.0.0.tar.gz:
Publisher:
publish.yml on schlarpc/cdk-lazy-import
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cdk_lazy_import-1.0.0.tar.gz -
Subject digest:
ee21929c779c1f10735c3997b4a04716fdbcc6b7d0c0618306b1f55268dda2ea - Sigstore transparency entry: 1239492897
- Sigstore integration time:
-
Permalink:
schlarpc/cdk-lazy-import@79fa0402e20fec19ab516516d7fec90038301d13 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/schlarpc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@79fa0402e20fec19ab516516d7fec90038301d13 -
Trigger Event:
release
-
Statement type:
File details
Details for the file cdk_lazy_import-1.0.0-py3-none-any.whl.
File metadata
- Download URL: cdk_lazy_import-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6416e72ecc4addda60c37f733644bd30dc2c6c2a120ea3ed5029e6a3ffc8d6f
|
|
| MD5 |
6c58f0cc5d6d39b767018931f5adc406
|
|
| BLAKE2b-256 |
63e3f420755695f712d03b85d3ad1511d756d73bbcb4964145704e5d4b26ee63
|
Provenance
The following attestation bundles were made for cdk_lazy_import-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on schlarpc/cdk-lazy-import
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cdk_lazy_import-1.0.0-py3-none-any.whl -
Subject digest:
c6416e72ecc4addda60c37f733644bd30dc2c6c2a120ea3ed5029e6a3ffc8d6f - Sigstore transparency entry: 1239492900
- Sigstore integration time:
-
Permalink:
schlarpc/cdk-lazy-import@79fa0402e20fec19ab516516d7fec90038301d13 -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/schlarpc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@79fa0402e20fec19ab516516d7fec90038301d13 -
Trigger Event:
release
-
Statement type: