Skip to main content

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 .pth file ensures this happens at interpreter startup, but if something imports CDK modules before .pth files 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk_lazy_import-1.0.0.tar.gz (53.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk_lazy_import-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

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

Hashes for cdk_lazy_import-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ee21929c779c1f10735c3997b4a04716fdbcc6b7d0c0618306b1f55268dda2ea
MD5 60eab6033e7ffc43626ab000f953bcd4
BLAKE2b-256 6b4daf2f256227efa5db514d70718955a89735503b5013ae05ed838a416a01dd

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdk_lazy_import-1.0.0.tar.gz:

Publisher: publish.yml on schlarpc/cdk-lazy-import

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cdk_lazy_import-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk_lazy_import-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6416e72ecc4addda60c37f733644bd30dc2c6c2a120ea3ed5029e6a3ffc8d6f
MD5 6c58f0cc5d6d39b767018931f5adc406
BLAKE2b-256 63e3f420755695f712d03b85d3ad1511d756d73bbcb4964145704e5d4b26ee63

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page