Package: cdk-resources
CDK ResourcesMotivationArchitectureRegularaws-cdkhas astack-basedarchitecture where resources are defined in each stack and then resources are
shared between stacks (import from stacks). This project is proposing aresource-basedarchitecture 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.jsonCDK 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.jsonEnvironment ParametersOne 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.jsonfile.In a big multi stack project, this approach become a issue ascdk.jsonstarts
growing is difficult to manageParameterization 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 ParameterizationclassPostgreSqlRdsDatabase(Resource[aws_rds.DatabaseCluster]):construct_class=aws_rds.DatabaseClusterconstruct_props=dict(default=dict(...instances=1,...),prod=dict(instances=2,))InstallationTo install use pip$ pip install cdk-resourcesComponentsResourceResources 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 aConstruct Manager. Components must
inherit fromcdk_resources.Resource.There are two types of resources:resource managed by the stackandimported
resources.Resource Attributes:construct_class(Required): Theaws_cdk.constructclass this resource
represent.construct_props: Required only if it is a managed resource. The cdk
construct class properties.construct_lookup_method: Method of theaws_cdk.constructconstruct to
be used to import the object.construct_lookup_props: Required if the object is an imported resource.
Kwargs used byconstruct_lookup_methodto lookup for the object.Resource Methods:get(): Class method of the resource that returns theaws_cdk.construct.
Either by lookup or because was previously created.post_create(): Extra configurations to apply to the constructafterconstruct was init.Resource Examples:As it can seen in the example below for thePostgreSqlRdsDatabaseconstruct_classisaws_rds.DatabaseCluster, desired configurations for all
the environments are being specified in theconstruct_propsattr. And other
resources are imported.fromaws_cdkimportaws_rds,core,aws_ec2fromcdk_resourcesimportResourcefromresources.ec2importPostgreSqlRdsDatabaseSgfromresources.vpcimport(DefaultVpc,DefaultPrivateDbASubnet,DefaultPrivateDbBSubnet,DefaultPrivateDbCSubnet,)classPostgreSqlRdsDatabase(Resource[aws_rds.DatabaseCluster]):construct_class=aws_rds.DatabaseClusterconstruct_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()]),),)StacksA stack is the natural representation of aCFN Stack. All stacks must inherit
fromcdk_resources.ResourceStack.Resource Attributes:EXISTING_RESOURCES(list): The list of existing resources that must be
inited inaws_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 theSampleStack. The stack
creates a DynamoTable, Security Group, RDS Aurora Parameter Group, and
RDS Cluster.Also, some resources must be imported. Those are specified inEXISTING_RESOURCElist as the VPC resources.fromcdk_resourcesimportResourceStackfromresources.dynamodbimportDynamoTablefromresources.ec2importPostgreSqlRdsDatabaseSgfromresources.rdsimportPostgreSqlRdsDatabase,PostgreSqlParameterGroupfromresources.snsimportSnsTopicfromresources.vpcimport(DefaultVpc,DefaultPrivateDbASubnet,DefaultPrivateDbBSubnet,DefaultPrivateDbCSubnet,)classSampleStack(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),]Parameterizationto doExamplesHereare some
availables examples.