Skip to main content
CDK Terrain (CDKTN) stacks let you manage multiple Terraform configurations within a single application. Each stack synthesizes to its own Terraform configuration and state file, which lets you deploy and destroy stacks independently. In this tutorial, you deploy a CDKTN application (TypeScript-only) with two stacks. Each stack provisions a small AWS Lambda function and uses TerraformAsset to package the Lambda deployment artifact.

Prerequisites

This tutorial assumes you are already familiar with the basic CDKTN workflow. If you are new to CDKTN, start with the install tutorial.
  • Terraform v1.2+ (or OpenTofu)
  • CDKTN v0.15+
  • AWS account
  • AWS credentials configured for Terraform
  • Node.js + npm
Some of the infrastructure in this tutorial does not qualify for the AWS free tier. Destroy the infrastructure at the end of the guide to avoid unnecessary charges.
CDKTN works with both Terraform and OpenTofu. To run OpenTofu, set TERRAFORM_BINARY_NAME=tofu before using the CLI. Refer to Environment Variables for details.

Explore CDKTN application

Clone the sample repository and move into the CDKTN project directory.
Shell

View application stacks

Install dependencies.
Shell
Add the AWS and Random providers.
Shell
List the stacks in the application.
Shell

Deploy Hello World function

Deploy the lambda-hello-world stack.
Shell
Open the url output in your browser to confirm the API gateway returns a greeting.

Deploy Hello Name function

Deploy the lambda-hello-name stack.
Shell
Open the url output in your browser. Append ?name=Terry to the URL to confirm the API gateway responds with a personalized greeting.

Inspect synthesized stacks

CDKTN generates a cdktf.out directory when it synthesizes the Terraform configuration (during cdktn synth or cdktn deploy). Each stack has its own folder under cdktf.out/stacks.
cdktf.out/
Each stack also has its own state file (for example, terraform.lambda-hello-world.tfstate). Like any Terraform state file, do not commit state files into source control.

Clean up resources

Destroy both stacks to remove AWS resources.
Shell
Shell

Next steps

  • Continue with Deploy applications for an end-to-end, multi-step deployment workflow.
  • Review how stacks work in CDKTN and Terraform state in Stacks.