Read Secrets with Terraform Variables
Secrets appear in your synthesized CDKTN code when you read them directly from environment variables or from files with normal system access. This introduces risk, especially if you are checking the synthesized configuration into a version control system. To mitigate this, use theTerraformVariable construct to read secrets. Terraform uses the values in TerraformVariable directly at execution time, so CDKTN does not write them to the synthesized cdktf.json file.
The following example uses a Terraform variable to read the sensitive admin password instead of reading it directly from an environment variable.
TF_VAR_NAME. For example, set TF_VAR_adminPassword='<your password>' in the execution environment.
If you use HCP Terraform with remote execution, you can store your secrets in HCP Terraform. Refer to the HCP Terraform documentation about workspace variables for more details.
We recommend using TerraformVariable only at the Stack level. Nesting them in custom constructs makes the interface unclear because some information may be passed into the constructor of the construct, while other data may be passed through a TerraformVariable. Nesting also changes the logical ID and makes it difficult to understand which TerraformVariable instances you must configure for the CDKTN program to run.
Providers
A provider is a Terraform plugin that allows users to manage an external API. Provider plugins act as a translation layer that allows Terraform to communicate with many different cloud providers, databases, and services. Use pre-built providers when possible. It can take several minutes to generate the code bindings for providers with very large schemas, so we offer several popular providers as pre-built packages. Pre-built providers are a performance optimization that reduces the time it takes to synthesize and run your application. You can also use pre-built providers as a peer dependency if you use open-source custom constructs. Refer to the CDKTN Provider GitHub repositories for a complete list of pre-built providers.Application Architecture
We recommend the following best practices when structuring your CDKTN application.Separate Business Units with Stacks
A stack represents a collection of infrastructure that CDKTN synthesizes as a dedicated Terraform configuration. Stacks allow you to separate the state management for multiple environments within an application. We recommend creating separate stacks for the following use cases:- Deployment stages (development / staging / production)
- Business purposes (e-commerce / blog / data warehouse)
- Regions (eu-west-1 / us-east-1) if they deploy similar infrastructure, e.g. for high availability
- Software components (network / db / compute)
- Deployment cycles (databases and domain names / applications)
Create Extensible Constructs
Constructs let you abstract common behavior into reusable classes. If you have no reason to limit the extensibility of a construct, you should default to making it as easy as possible to overwrite custom behavior, while still providing good standard defaults. In some cases, you can use interfaces from the generated provider bindings to allow users to customize the configuration. In very complex constructs, we recommend using methods to encapsulate behavior. For example, you can create a method that derives default values for the configuration. This lets users extend the base class and overwrite the behavior in a central location. The following example exposes configuration options for the S3 bucket resource within the construct. It also creates a second construct class that overwrites the default naming behavior.Use Projen to Distribute Constructs
If your code is hosted on Github and you want to distribute it as a CDKTN construct, you can use projen to create a repository with all required tooling set up for you. You can runnpx projen new cdktn-construct in a new folder, and the created project will be ready to use. Projen has built-in options to publish constructs to all registries.
If you want to deploy your CDKTN construct as a Terraform module, we recommend projen-cdktf-hybrid-construct.