Stacks in CDKTN are different from the Terraform stacks concept announced at HashiConf 2023. Terraform stacks are a configuration layer that simplifies provisioning and managing resources at scale by controlling cross-configuration dependencies between Terraform modules. Refer to Terraform stacks, explained in the HashiCorp blog for additional information.
Scope
You can instantiate the same resource multiple times throughout your infrastructure. For example, you may want to create multiple S3 Buckets with different configurations. Instances that share the samestack parent element are considered to be part of the same scope. You must set a different name property for each instance to avoid naming conflicts.
Refer to the constructs documentation for more details and an example.
Single Stack
The following example generates a single Terraform configuration in the configured output folder. When you runcdktn synth, the synthesized Terraform configuration will be in the folder cdktf.out/stacks/a-single-stack
Multiple Stacks
You can specify multiple stacks in your application. For example, you may want a separate configuration for development, testing, and production environments. The following example synthesizes multiple Terraform configurations in the configured output folder.cdktn synth produces the following synthesized stacks.
cdktn deploy and cdktn destroy command or use a wild card glob (e.g., cdktn deploy '*-production').
Refer to Best Practices for more details about when to create multiple stacks and how to structure them.
Cross-Stack References
When you reference resources from one stack in another stack, you can do so by exposing the resource in the source stack and referencing it in the target stack.id value of vpc from the origin-stack instance of VpcStack and then referencing it in the target-stack instance of BackendStack.
Accessing a value from a different stack causes the value to be exported as TerraformOutput in the origin stack.
The value is then accessed through a TerraformRemoteState in the target stack.
Both are automatically added to the respective stacks to make the process seemless.
When you are using HCP Terraform, each stack must be its own workspace.
This means that you need to create a separate workspace for each stack and you need to set the permissions to allow access between the stacks.
Stack Dependencies
We add the stack dependencies in thecdktf.out/manifest.json file for each stack under dependencies.
By default a stack is dependant on another stack when the data used origins in that stack.
If you e.g. write this.allResources = Fn.concat([resourceFromStackA.items, resourceFromStackB.items]) in Stack C and use stackC.allResources in Stack D, Stack D will be dependant on Stack A and B, but not C since that is not the origin of the data.
To make the dependency explicit, runstackD.addDependency(stackC).
If you want to keep the result of the function attached to one stack and save its state, create a Terraform Local value and expose it. The following example creates a TerraformLocal.
--ignore-missing-stack-dependencies to the deploy and destroy commands.
Migration from <= 0.2
Until version 0.2, CDKTN only supported a single stack. For local state handling, CDKTN used a terraform.tfstate in the project root folder. With version >= 0.3, the local state file reflects the stack name it belongs to in its file name. When a terraform.tfstate file is still present in the project root folder, it has to be renamed to match the schema terraform.<stack-name>.tfstate manually.
Escape Hatch
For anything on the top-levelterraform block that is not natively implemented, use the stack escape hatch to define a configuration. For example, define remote backend using the addOverride method in TypeScript.
The following example synthesizes a Terraform configuration with the remote backend included in the terraform block.
remote backend configuration.