base¶
Base classes for AWS CDK constructs and stacks with integrated parameter management.
This module provides base classes that integrate AWS CDK constructs and stacks with
the parameter system defined in cdkit.params. The integration eliminates
repetitive parameter definition code by leveraging Python dataclasses to create
strongly-typed, validated configuration objects.
- class cdkit.base.BaseConstruct(*args: Any, **kwargs)[source]¶
Base class for AWS CDK constructs with integrated parameter management.
Example, define a custom construct with typed parameters:
import dataclasses from cdkit.api import BaseConstruct, ConstructParams, REQ @dataclasses.dataclass class BucketParams(ConstructParams): bucket_name: str = dataclasses.field(default=REQ) versioned: bool = dataclasses.field(default=False) class S3Bucket(BaseConstruct): def __init__( self, scope: Construct, params: BucketParams, ): super().__init__(scope=scope, params=params) self.params = params # this is for type hint # Access parameters via self.params bucket = s3.Bucket( self, "Bucket", bucket_name=self.params.bucket_name, versioned=self.params.versioned, )
- class cdkit.base.BaseStack(*args: Any, **kwargs)[source]¶
Base class for AWS CDK stacks with integrated parameter management.
This class extends the standard cdk.Stack class with parameter management capabilities, enabling a more structured and type-safe approach to stack configuration. It serves as the foundation for all CDK stacks in the project, providing consistency in initialization and access to stack parameters.
- Parameters:
scope – The CDK construct scope (typically a Stack or another Construct)
params – Parameter object containing all construct configuration, must be an instance of
ConstructParamsor a subclass
Example: create a subclass of BaseStack and pass your custom stack parameters:
import dataclasses import aws_cdk as cdk from constructs import Construct from cdkit.api import BaseStack, StackParams, REQ @dataclasses.dataclass class MyStackParams(StackParams): project_name: str = dataclasses.field(default=REQ) env_name: str = dataclasses.field(default=REQ) class MyStack(BaseStack): def __init__( self, scope: Construct, params: MyStackParams, ): super().__init__(scope=scope, params=params) self.params = params # this is for type hint cdk.Tags.of(self).add("tech:project_name", self.params.project_name) cdk.Tags.of(self).add("tech:env_name", self.params.env_name)