Package: cdk-organizer
CDK Organizer Python LibraryThis library contains the core features to handle CDK projects including:LoggingConfiguration ResolverStack Groups LoaderNaming UtilsFull documentation:https://cdk-organizer.github.io/Installationpipinstallcdk-organizer[terraform,aws]Extrasterraform: Includecdktfas a dependency.aws: Includeaws-cdk-libas a dependency.CDK Start ScriptThe content of theapp.pyfile should be as follows:AWS CDKimportaws_cdkascdkfromcdk_organizer.miscellaneous.loggingimportsetup_loggingfromcdk_organizer.stack_groupimportStackGroupLoaderapp=cdk.App()logger=setup_logging(__name__,app.node.try_get_context("loglevel")or"INFO")loader=StackGroupLoader(app)loader.synth()app.synth()CDK for Terraformfromcdk_organizer.miscellaneous.loggingimportsetup_loggingfromcdk_organizer.stack_groupimportStackGroupLoaderfromcdktfimportAppapp=App()logger=setup_logging(__name__,app.node.try_get_context("loglevel")or"INFO")loader=StackGroupLoader(app)loader.synth()app.synth()Context VariablesThe following context variables are required to the CDK Organizer to work properly:envregionThe variables can be set in thecdk.jsonfile:{..."context":{..."env":"dev","region":"us-east-1"...}...}Or passed as arguments to the CDK CLI:cdksynth--contextenv=dev--contextregion=us-east-1In thecdktfCLI the context variables cannot be passed as arguments, so they need to be set in thecdk.jsonfile.https://github.com/hashicorp/terraform-cdk/issues/2019Theenvvariable can also be set as an environment variableCDK_ENV.Project StructureTo apply the pattern purposed in this library for CDK projects, the following structure is required:.
+-- cdk.json
+-- app.pyStack Structurethe stack class needs to inherit from classcdk_organizer.aws.stack.Stackfor AWS CDK andcdk_organizer.terraform.stack.Stackfor Terraform CDK.AWS CDKfromconstructsimportConstructfromcdk_organizer.aws.stackimportStackfromcdk_organizer.aws.stack_groupimportStackGroupclassMyStack(Stack):def__init__(self,scope:Construct,id:str,stack_group:StackGroup,**kwargs)->None:super().__init__(scope,id,stack_group,**kwargs)Terraform CDKfromconstructsimportConstructfromcdk_organizer.terraform.stackimportStackfromcdk_organizer.terraform.stack_groupimportStackGroupclassMyStack(Stack):def__init__(self,scope:Construct,id:str,stack_group:StackGroup,**kwargs)->None:super().__init__(scope,id,stack_group,**kwargs)Using S3 BackendTo use S3 terraform backend, the following resources are required:S3 BucketDynamoDB TableAdd the following object to the environment configuration file:s3_backend:bucket:"<bucket-name>"region:"<aws-region>"dynamodb_table:"<dynamodb-table-name>"Stack Group StructureCreate astacksfolder in the root of the project and structure it as follows:.
+-- cdk.json
+-- app.py
+-- stacks/
| +-- <groupName>/
|   +-- stacks.pyThe stack groups files follow this pattern:AWS CDKimportaws_cdkascdkfromdataclassesimportdataclassfromcdk_organizer.aws.stack_groupimportStackGroup@dataclassclassMyStackGroupConfig:domain:stripv6:bool=TrueclassMyStackGroup(StackGroup[MyStackGroupConfig]):def_load_stacks(self)->None:MyStack(self.app,self.get_stack_name('my-stack'),stack_group=self,parameters=MyStackParameters(domain=self.data.domain,ipv6=self.data.ipv6))Terraformimportcdktffromdataclassesimportdataclassfromcdk_organizer.terraform.stack_groupimportStackGroup@dataclassclassMyStackGroupConfig:domain:stripv6:bool=TrueclassMyStackGroup(StackGroup[MyStackGroupConfig]):def_load_stacks(self)->None:MyStack(self.app,self.get_stack_name('my-stack'),stack_group=self,parameters=MyStackParameters(domain=self.data.domain,ipv6=self.data.ipv6))Using Stack Attributes from Other Stack GroupsIn some cases, you may want to use the attributes of another stack group. For example, refer the DNS Hosted Zone created by a shared stack group.To resolve the group use theself.resolve_groupfunction in the stack group class, like the example below:importaws_cdkascdkfromdataclassesimportdataclassfromcdk_organizer.aws.stack_groupimportStackGroupfromstacks.dnsimportDNSStackGroup@dataclassclassMyStackGroupConfig:domain:stripv6:bool=TrueclassMyStackGroup(StackGroup[MyStackGroupConfig]):def_load_stacks(self)->None:MyStack(self.app,self.get_stack_name('my-stack'),stack_group=self,parameters=MyStackParameters(domain=self.data.domain,zone=self.resolve_group(DNSStackGroup).zoneipv6=self.data.ipv6))The functionget_stack_namegenerates the stack name based on following pattern.Pattern:{module_path}-{name}-{region}-{env}Consider the following example:module_path:myproject.myapp.wwwname:sparegion:us-east-1env:devStack name will be:myproject-myapp-www-spa-us-east-1-dev.Config StructureCreate aconfigfolder in the root of the project and structure it as follows:.
+-- cdk.json
+-- app.py
+-- config/
| +-- <env>/
|   +-- config.yaml
|   +-- <region>/
|     +-- config.yaml
|     +-- <groupName>/
|         +-- config.yamlThe first two levels are reserved to the environment name and the region name, the next levels needs to match the stack group structure.Example:.
+-- cdk.json
+-- app.py
+-- config/
| +-- config.yaml
| +-- dev/
|   +-- config.yaml
|   +-- us-east-1/
|     +-- config.yaml
|     +-- app1/
|       +-- config.yaml
+-- stacks/
| +-- app1/
|   +-- stacks.py
+-- templates/
|   +-- stacks/ExamplesAWS CDKCDK for Terraform