Skip to main content

CDK Resources

Motivation

Architecture

Regular aws-cdk has a stack-based architecture where resources are defined in each stack and then resources are shared between stacks (import from stacks). This project is proposing a resource-based architecture as this would enable a more natural organization of resources based on AWS services.

Typical CDK Structure

└── custom_construct/
|    └── constructs1.py
|    └── ...
|    └── constructs1.py
| 
└── stacks/
|    └── stack1.py
|    └── stack2.py
|    ...
|    └── stackN.py

└── app.py
└── cdk.json

CDK Resources Approach

└── custom_construct/
|    └── constructs1.py
|    └── ...
|    └── constructs1.py
| 
└── resources/
|    └── apigateway.py
|    └── ec2.py
|    └── ecs.py
|    └── eks.py
|    └── elasticsearch.py
|    └── ...
|    └── vpc.py
|
└── stacks/
|    └── stack1.py
|    └── stack2.py
|    ...
|    └── stackN.py

└── app.py
└── cdk.json

Environment Parameters

One of the most broadly used approach to parameterize a cdk stack based on environments also knows as stages (dev, ..., prod) is to pass them as configurations in thecdk.json file.

In a big multi stack project, this approach become a issue as cdk.json starts growing is difficult to manage

Parameterization based on context

{
  "app": "python3 app.py",
  "context": {
    "configurations": {
      "stack1": {
        "dev": {
          "aurora_cluster_instances": 1,
          ...
          "ecs_service_desired_container_count": 1
        },
        "prod": {
          "aurora_cluster_instances": 3,
          ...
          "ecs_service_desired_container_count": 5
        }
      },
      ...
    }
  }
}

CDK Resources Parameterization

class PostgreSqlRdsDatabase(Resource[aws_rds.DatabaseCluster]):
    construct_class = aws_rds.DatabaseCluster
    construct_props = dict(
        default=dict(
            ...
            instances=1,
            ...
        ),
        prod=dict(
            instances=2,
        )
    )

Installation

To install use pip

$ pip install cdk-resources

Components

Resource

Resources are the most important component as it contains mostly all the logic of the project. A resource is a natural representation of an AWS element, and in terms of cdk is the equivalent of a Construct Manager. Components must inherit from cdk_resources.Resource.

There are two types of resources: resource managed by the stack and imported resources.

Resource Attributes:

  • construct_class (Required): The aws_cdk.construct class this resource represent.

  • construct_props: Required only if it is a managed resource. The cdk construct class properties.

  • construct_lookup_method: Method of the aws_cdk.construct construct to be used to import the object.

  • construct_lookup_props: Required if the object is an imported resource. Kwargs used by construct_lookup_method to lookup for the object.

Resource Methods:

  • get(): Class method of the resource that returns the aws_cdk.construct. Either by lookup or because was previously created.

  • post_create(): Extra configurations to apply to the construct after construct was init.

Resource Examples:

As it can seen in the example below for the PostgreSqlRdsDatabase construct_class is aws_rds.DatabaseCluster, desired configurations for all the environments are being specified in the construct_props attr. And other resources are imported.

from aws_cdk import aws_rds, core, aws_ec2

from cdk_resources import Resource

from resources.ec2 import PostgreSqlRdsDatabaseSg
from resources.vpc import (
    DefaultVpc,
    DefaultPrivateDbASubnet,
    DefaultPrivateDbBSubnet,
    DefaultPrivateDbCSubnet,
)

class PostgreSqlRdsDatabase(Resource[aws_rds.DatabaseCluster]):
    construct_class = aws_rds.DatabaseCluster
    construct_props = dict(
        default=dict(
            engine=aws_rds.DatabaseClusterEngine.aurora_postgres(
                version=aws_rds.AuroraPostgresEngineVersion.VER_13_4
            ),
            backup=aws_rds.BackupProps(retention=core.Duration.days(3)),
            deletion_protection=True,
            instance_props=lambda: aws_rds.InstanceProps(
                instance_type=aws_ec2.InstanceType.of(
                    aws_ec2.InstanceClass.BURSTABLE3,
                    aws_ec2.InstanceSize.MEDIUM,
                ),
                security_groups=[PostgreSqlRdsDatabaseSg.get()],
                vpc=DefaultVpc.get(),
                vpc_subnets=aws_ec2.SubnetSelection(
                    subnets=[
                        DefaultPrivateDbASubnet.get(),
                        DefaultPrivateDbBSubnet.get(),
                        DefaultPrivateDbCSubnet.get(),
                    ]
                ),
                parameter_group=PostgreSqlParameterGroup().construct,
            ),
            instances=1,
            port=5432,
            removal_policy=core.RemovalPolicy.RETAIN,
            storage_encrypted=True,
        ),
        prod=dict(
            backup=aws_rds.BackupProps(retention=core.Duration.days(30)),
            instances=2,
            vpc_subnets=lambda: aws_ec2.SubnetSelection(
                subnets=[
                    DefaultPrivateDbASubnet.get(),
                    DefaultPrivateDbCSubnet.get(),
                    DefaultPrivateDbCSubnet.get()
                ]
            ),
        ),
    )

Stacks

A stack is the natural representation of a CFN Stack. All stacks must inherit from cdk_resources.ResourceStack.

Resource Attributes:

  • EXISTING_RESOURCES (list): The list of existing resources that must be inited in aws_cdk.scope. These are resources that are used by the Stack resources.

  • RESOURCES (list): The resources own for this stack.

Resource Examples:

As it can be seen in the example below for the SampleStack. The stack creates a DynamoTable, Security Group, RDS Aurora Parameter Group, and RDS Cluster.

Also, some resources must be imported. Those are specified in EXISTING_RESOURCE list as the VPC resources.

from cdk_resources import ResourceStack

from resources.dynamodb import DynamoTable
from resources.ec2 import PostgreSqlRdsDatabaseSg
from resources.rds import PostgreSqlRdsDatabase, PostgreSqlParameterGroup
from resources.sns import SnsTopic
from resources.vpc import (
    DefaultVpc,
    DefaultPrivateDbASubnet,
    DefaultPrivateDbBSubnet,
    DefaultPrivateDbCSubnet,
)


class SampleStack(ResourceStack):
    EXISTING_RESOURCES = [
        ("vpc", DefaultVpc),
        ("subnet_db_a", DefaultPrivateDbASubnet),
        ("subnet_db_b", DefaultPrivateDbBSubnet),
        ("subnet_db_c", DefaultPrivateDbCSubnet),
    ]
    RESOURCES = [
        # DynamoDB
        ("dynamodb", DynamoTable),
        # RDS
        ("postgresql-sg", PostgreSqlRdsDatabaseSg),
        ("postgresql-parameter-group", PostgreSqlParameterGroup),
        ("postgresqlDb", PostgreSqlRdsDatabase),
        # SNS
        ("sns-topic", SnsTopic),
    ]

Parameterization

to do

Examples

Here are some availables examples.

Release files for cdk-resources 0.2.9

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

Source distribution (sdist)

Source distribution for cdk-resources 0.2.9
File Size Uploaded
cdk_resources-0.2.9.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cdk-resources 0.2.9
File Interpreter ABI Platform
cdk_resources-0.2.9-py3-none-any.whl Python 3 none any Details

Total release size: 19.0 kB

Release files / cdk_resources-0.2.9.tar.gz

Download URL cdk_resources-0.2.9.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c6e6b73e624514f4fd47cfef0dea9a4dd0e59f671eac2eb607214999e9d95956
BLAKE2b-256 checksum
How to use checksums
74f51944ad2c0cbe9e0494d6c53793a5c63f888ad2e58862bfc73afe28c94dc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.1

Release files / cdk_resources-0.2.9-py3-none-any.whl

Download URL cdk_resources-0.2.9-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de999896966f6659a5f2d0c30f3ce8b9812a0f0a5cc5d33fd64cf92985c6c12b
BLAKE2b-256 checksum
How to use checksums
a86842994226466788dbff15166cf2af750e56ec3070c91918935bb0b35647a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.1

Release history Release notifications | RSS feed

2.0.9

1 release file

2.0.8

1 release file

2.0.7

1 release file

2.0.6

1 release file

2.0.5

1 release file

2.0.4

1 release file

2.0.3

1 release file

2.0.2

1 release file

2.0.1

1 release file

2.0.0

1 release file

1.0.0

1 release file

This release

0.2.9 This release

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

1 release file

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