CDK Terrain (CDKTN)
repository·main·Indexed 42 days ago
https://github.com/open-constructs/cdk-terrainA community fork of CDKTF that allows developers to define and provision infrastructure using TypeScript, Python, Java, C#, and Go via OpenTofu or Terraform. It includes a CLI (cdktn-cli) for generating constructs, synthesizing configurations, and deploying stacks.
What's inside CDK Terrain
- cdktn is a framework designed for defining cloud infrastructure. It leverages Terraform providers and modules, allowing developers to define infrastructure resources using high-level programming languages instead of raw HCL.
What is CDK Terrain?
mainCDK Terrain (cdktn) is a community-driven continuation of the Cloud Development Kit for Terraform (CDKTF). It allows developers to define Terraform infrastructure using programming languages like TypeScript and Python, which are then synthesized into standard Terraform JSON configuration.Overview of @cdktn/provider-schema
mainThe@cdktn/provider-schemapackage provides the core methods and logic required to work with Terraform provider schemas. It is used to define, manipulate, or inspect the structure of Terraform providers within the CDK Terrain ecosystem.What is CDK Terrain (CDKTN)?
mainCDK Terrain (CDKTN) is a community fork of the Cloud Development Kit for Terraform (CDKTF). It allows you to define cloud infrastructure using familiar programming languages (TypeScript, Python, Java, C#, and Go) and provision it via OpenTofu or Terraform. This enables you to leverage your existing programming toolchain for testing and dependency management without needing to learn HashiCorp Configuration Language (HCL).Understand the documentation generation pipeline
mainThe documentation generation process follows a pipeline that operates on the MDAST (Markdown Abstract Syntax Tree) using
remark/unified. This approach ensures that code blocks are preserved without corruption during sanitization.Pipeline Stages
- Source:
packages/cdktn/.jsii(JSII assembly). - Generation:
jsii-docgencreates one large markdown document per language. - Parsing:
remark-parseconverts markdown into a full MDAST. - Splitting:
splitByHeading(tree, 2)divides the tree into a Map of topics based on H2 headings. - Sanitization:
sanitizeAst(subtree)walks text, HTML, and link nodes to clean them while skippingcodeandinlineCodenodes. - Serialization:
remark-stringifyconverts the AST back to markdown. - MDX Fixups:
postProcessForMdx()performs string-level fixes (autolinks, escaping braces, etc.). - Composition:
compose()wraps content with MDX frontmatter and writes the final.mdxfiles.
- Source:
Core components of CDK Terrain
mainCDK Terrain is composed of two primary packages:
cdktn-cli: A command-line interface used to initialize, import, and synthesize CDK Terrain applications.cdktn: A library used to define Terraform resources using programming constructs.
Compatibility with OpenTofu and Terraform
mainCDK Terrain is designed to be execution-engine agnostic. It synthesizes standard Terraform configuration, meaning users can run the generated output with either Terraform or OpenTofu without special configuration or feature gaps. This allows you to choose your execution engine based on operational or licensing preferences.How function availability is tracked and consumed
mainFunction availability for Terraform and OpenTofu is managed via
functions-matrix.json. This file records the stable releases in which every built-in function is available (tracking introductions, removals, and gaps per product).This matrix is consumed by two primary processes:
../scripts/generate.ts: Merges OpenTofu-only functions into theFnbindings.../scripts/generate-function-availability.ts: Emitspackages/cdktn/src/functions/function-availability.generated.ts. This file contains the version-constraint map used by theValidateFunctionVersionSupportmechanism to validate function versions at synthesis time.
Override Terraform functions in bindings
mainIf you need to skip exposing a function or export it with a modified signature (for example, to improve the UX of variadic parameters), you can use the
INTERNAL_METHODSconstant inscripts/generate.ts.Functions listed in
INTERNAL_METHODSwill be internalized by adding a_prefix and the@internaldocstring required by JSII. This allows you to then re-export them with a custom signature viapackages/cdktn/src/terraform-functions.ts.Understand the Provider Feature Availability Matrix
mainThe
features-matrix.jsonfile tracks the availability of various Terraform and OpenTofu CLI capabilities (such as provider-defined functions, ephemeral resources, write-only attributes, resource identity, list resources, actions, and state stores). It records which specific CLI releases first emitted these capabilities via theproviders schema -jsoncommand.This matrix is used to drive the
ValidateFeatureTargetSupportcheck during synthesis, ensuring that a project's declaredtargetVersionsare compatible with the features being used.Maintain Provider Feature Constraints and Schema Emission Boundaries
mainThe feature availability matrix is used to derive two critical, hand-maintained maps within the repository. Because there is currently no automated drift check, these must be updated manually whenever the matrix changes:
Usage Boundaries:
packages/cdktn/src/provider-feature-constraints.ts(specifically theproviderFeatureConstraintsmap). This defines when a feature is supported by the language syntax.- Note: There is an asymmetry for OpenTofu provider functions: language support began in version
1.7.0, but theproviders schema -jsoncommand only started emitting thefunctionskey in version1.8.0.
- Note: There is an asymmetry for OpenTofu provider functions: language support began in version
Schema Emission Boundaries:
packages/@cdktn/provider-schema/src/emission-check.ts(specifically theSCHEMA_EMISSION_BOUNDARIESmap). This defines when theproviders schema -jsoncommand starts including the specific feature key.
Understand the Stack architecture in CDK for Terraform
mainIn CDK for Terraform, a Stack represents a collection of resources that share a single Terraform state and are deployed as a single unit.
In complex applications, you can split infrastructure into multiple stacks (e.g.,
FrontendStack,PostsStack,PreviewStack) to manage lifecycles independently. For example, adevenvironment and aprodenvironment might each have their own instances of these stacks.