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
git clone https://github.com/hashicorp-education/learn-cdktf-assets-stacks-lambda
cd learn-cdktf-assets-stacks-lambda
cd cdktf

View application stacks

Install dependencies.
Shell
npm install
added 305 packages, and audited 357 packages in 5s
found 0 vulnerabilities
Add the AWS and Random providers.
Shell
cdktn provider add "aws@~>4.0" random
Found pre-built provider.
Installing package @cdktn/provider-aws ...
Package installed.
Found pre-built provider.
Installing package @cdktn/provider-random ...
Package installed.
List the stacks in the application.
Shell
cdktn list

Stack name           Path
lambda-hello-world   cdktf.out/stacks/lambda-hello-world
lambda-hello-name    cdktf.out/stacks/lambda-hello-name

Deploy Hello World function

Deploy the lambda-hello-world stack.
Shell
cdktn deploy lambda-hello-world
...
Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:
lambda-hello-world url = "https://bur6dfzia5.execute-api.us-west-2.amazonaws.com"
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
cdktn deploy lambda-hello-name
...
Apply complete! Resources: 8 added, 0 changed, 0 destroyed.

Outputs:
lambda-hello-name url = "https://oogzjki3cb.execute-api.us-west-2.amazonaws.com"
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/
cdktf.out/
|-- manifest.json
`-- stacks
    |-- lambda-hello-name
    |   |-- assets
    |   |   `-- lambda-asset
    |   |       `-- ACDAAB9A40AD1E0EC3B40C4B53527E85
    |   |           `-- archive.zip
    |   |-- cdk.tf.json
    |   `-- plan
    `-- lambda-hello-world
        |-- assets
        |   `-- lambda-asset
        |       `-- BD42BA06A24B006B4907A4F399734ACC
        |           `-- archive.zip
        |-- cdk.tf.json
        `-- plan
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
cdktn destroy lambda-hello-world
...
Destroy complete! Resources: 8 destroyed.
Shell
cdktn destroy lambda-hello-name
...
Destroy complete! Resources: 8 destroyed.

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.