Skip to main content

aws-cdk-config

Provides typed input parsing for AWS CDK (or really anything else that needs it) allowing for Python built-ins as well as casting to any other class types as well as define validation

CDK provides a method for passing arguments via Parameters, but this mechanism has certain limitations:

  • Typing is limited to the same types as CloudFormation and does not support python types that you may want to use in code
  • CDK Parameters are not concrete values, but rather a later resolvable value, and cannot be treated as such
  • Parameters can't be configured in a flat file, but have to be passed as commandline arguments. Often developers use context instead, but this removes things such as typing

Benefits of using CdkConfig:

  • Support for using yaml/json files as a config or passing a config as a dict from any source in code
  • Input values are concrete values
  • Inputs can be typed as anything that can accept one a yaml or json value (unpacked if a sequence or object) as it's initializer's arguments
  • Inputs can have a callable, including lambda, as a validator
  • Guaranteed immutability of inputs after parsing

See the full code documentation here

Example:

Config file inputs.yaml

development:
    GroupName: Foo
    GroupMembers:
        - bar
        - baz

CDK code:

#!/user/bin/env python3
from typing import List

from aws_cdk_config import CdkConfig
from aws_cdk import (
    Stack,
    aws_iam as iam,
)
from boto3 import client
from constructs import Construct


def group_exists(name: str) -> bool:
    """
    Provides validation to ensure the iam group doesn't already exist
    so we can fail fast if it does.
    """
    iam_client = client("iam")
    try:
        iam_client.get_group(GroupName=name)
    except iam_client.exceptions.NoSuchEntityException:
        return True
    return False


config = CdkConfig(values_file=inputs.yaml, namespace="development")
config.add_input(
    name="GroupName",
    type=str,
    description="The name of the group to create",
    validator=group_exists
)
config.add_input(
    name="GroupUsers",
    type=List[str],
    description="A list of users to add to the group",
    validator=lambda x: x != "root"  # Use a lambda as the callable just to keep it simple
)
config.parse()

class InputDemo(Stack):

    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        group = iam.Group(self, config.GroupName.value)

        for username in config.GroupUsers.value:
            user = iam.User.from_user_name(self, username, username)
            group.add_user(user)

Typing

Inputs can be any python type, not just primitives. This means that any types that aren't both YAML, JSON, and python builtins (str, list, int, etc) will be cast by passing the config values to the type. The classes accept arguments in the following ways:

  • If the type isn't a python builtin sequence, and yaml/json value is an array, the class must accept the value being passed "unpacked" as arguments, eg: Foo(*myinput)
  • If the type isn't a python hashable and the yaml/json value is an object, the calss must accept the value being "unpacked" being passed as arguments, eg: Bar(**myinput)

Example:

from aws_cdk_config import CdkConfig

class Foo:
    def __init__(self, arg1, arg2):  # Could also be signed as *args
        pass


config = CdkConfig()
config.add_argument(
    name="test",
    type=Foo,
    value=["arg_value_1", "arg_value_2"]
)
config.parse()

# Returns True
isinstance(config.test, Foo)
from aws_cdk_config import CdkConfig

class Bar:
    def __init__(self, *, arg1, arg2):  # Could also be signed as **kwargs
        pass


config = CdkConfig()
config.add_argument(
    name="test",
    type=Bar,
    value={
        "arg1": "arg_value_1",
        "arg2": "arg_value_2"
    }
)
config.parse()

# Returns True
isinstance(config.test, Bar)

See the examples directory for more examples.

Metadata

Release files for aws-cdk-config 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-cdk-config 0.1.2
File Size Uploaded
aws_cdk_config-0.1.2.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-cdk-config 0.1.2
File Interpreter ABI Platform
aws_cdk_config-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 14.3 kB

Release files / aws_cdk_config-0.1.2.tar.gz

Download URL aws_cdk_config-0.1.2.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
75f4bd5312395d04cfccfbe237b6fc396067366ae909b09dac5054a9e3374526
BLAKE2b-256 checksum
How to use checksums
ddb2f2a86e602ea51d572d2acc222013c4f97fe72f823ed2e069bb797f0c1d97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1016-azure

Release files / aws_cdk_config-0.1.2-py3-none-any.whl

Download URL aws_cdk_config-0.1.2-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d09d88c5f54e8d3442ddcf430fc42256b5f48449c23c26231126ab84a63b4a9f
BLAKE2b-256 checksum
How to use checksums
e0907733660d056f1eb2d85dbe0e7dd7db1db2a2ccdbb0a3a7a457c2c5652e02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.0 Linux/6.2.0-1016-azure

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page