Understanding CDKit’s Parameter System¶
The CDKit Parameter System addresses common challenges in AWS CDK development by providing a more structured and type-safe approach to manage construct and stack parameters. This documentation compares the traditional approach with CDKit’s enhanced parameter management system.
The Problem with Traditional CDK Parameter Management¶
Traditional CDK stack implementation requires manually defining all parameters in the constructor, managing defaults, and handling type safety yourself. This approach becomes unwieldy as applications grow more complex.
Let’s examine the traditional approach:
1# -*- coding: utf-8 -*-
2
3import aws_cdk as cdk
4from constructs import Construct
5
6
7class MyStack(cdk.Stack):
8 def __init__(
9 self,
10 scope: Construct,
11 id: str,
12 stack_name: str,
13 env: cdk.Environment,
14 your_custom_param_1: int,
15 your_custom_param_2: str = "my_default_value",
16 **kwargs, # more parameters for the default cdk.Stack.__init__
17 ):
18 super().__init__(
19 scope=scope,
20 id=id,
21 stack_name=stack_name,
22 env=env,
23 **kwargs,
24 )
25 self.your_custom_param_1 = your_custom_param_1
26 self.your_custom_param_2 = your_custom_param_2
27
28
29app = cdk.App()
30stack = MyStack(
31 scope=app,
32 id="...",
33 stack_name="my_stack_name",
34 env=cdk.Environment(
35 account="...",
36 region="...",
37 ),
38 your_custom_param_1=42,
39 your_custom_param_2="my_custom_value",
40)
In this traditional approach, several issues emerge:
Repetitive Parameter Definitions: Each subclass needs to redefine all parameters in the constructor signature.
Loss of Type Information: Using **kwargs loses type hints and IDE completion support.
Manual Validation Required: You must manually validate required parameters.
Poor Maintainability: Adding or changing parameters requires updates in multiple places.
Inconsistent Parameter Handling: Different developers may implement parameter validation differently.
CDKit’s Enhanced Parameter System¶
CDKit solves these problems with a structured parameter system built on Python dataclasses:
1# -*- coding: utf-8 -*-
2
3import dataclasses
4
5import aws_cdk as cdk
6from constructs import Construct
7
8import cdkit.api as cdkit
9from func_args.api import REQ
10
11
12@dataclasses.dataclass
13class MyStackParams(cdkit.StackParams):
14 your_custom_param_1: int = dataclasses.field(default=REQ)
15 your_custom_param_2: str = dataclasses.field(default="my_default_value")
16
17
18class MyStack(cdkit.BaseStack):
19 def __init__(
20 self,
21 scope: Construct,
22 params: MyStackParams,
23 ):
24 super().__init__(scope=scope, params=params)
25 self.params = params
26
27
28app = cdk.App()
29stack = MyStack(
30 scope=app,
31 params=MyStackParams(
32 id="...",
33 stack_name="my_stack_name",
34 env=cdk.Environment(
35 account="...",
36 region="...",
37 ),
38 your_custom_param_1=42,
39 your_custom_param_2="my_custom_value",
40 ),
41)
Benefits of CDKit’s Parameter System¶
- Type Safety and IDE Support
All parameters are strongly typed with proper annotations
Full IDE autocompletion for parameters
Type checking catches errors at development time
- Centralized Parameter Definition
Parameters are defined once in the dataclass
Changes to parameters happen in one place
Consistent parameter handling across the project
- Built-in Parameter Validation
Required parameters are clearly marked (REQ)
Optional parameters have explicit defaults (OPT)
Validation happens automatically when parameters are instantiated
4. Cleaner Stack Implementation¶
Stack implementation focuses on business logic, not parameter handling
Constructor is simplified to just scope and params
Self-documenting parameter organization
5. Easy Extension¶
New parameters can be added by extending the parameter dataclass
Parameter inheritance mirrors the stack inheritance hierarchy
Common parameters are inherited, specific ones are added
Summary¶
The CDKit parameter system transforms AWS CDK development by providing a more structured, type-safe, and maintainable approach to parameter management. By leveraging Python dataclasses, it eliminates repetitive code, improves type safety, and ensures consistent parameter handling across your CDK applications.
See also
See BaseConstruct, BaseStack, it only takes one customizable ConstructParams and StackParams to create a stack.
This approach significantly reduces boilerplate code while improving code quality and maintainability.