Skip to main content
CDK Terrain (CDKTN) lets you define infrastructure in familiar programming languages while still creating plans with Terraform (or OpenTofu). You can use CDKTN with hundreds of providers and existing modules, adopt Terraform Cloud/HCP Terraform workflows, and ship infrastructure with the development practices you already use for application code. Follow this tutorial to install the CDKTN CLI and run the quick start project that provisions an NGINX container with Docker.

Prerequisites

  • Terraform CLI 1.2+ (or OpenTofu)
  • Node.js 16+ and npm 16+
  • Docker Desktop or another Docker runtime
  • Your preferred CDKTN language toolchain (TypeScript v4.4 + Node.js 16.13, Python 3.10 + Pipenv, Go 1.20+, .NET 6+, or Java 17 with Maven)

Install CDKTN

You can install the cdktn CLI globally with npm on any platform or with Homebrew on macOS. Pick the option that matches how you manage global tools.
Shell
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.

Verify the installation

Confirm that the CLI is on your PATH and review the available subcommands.
Shell
Use cdktn init --help whenever you need more detail on initialization flags.
Shell

Quick start tutorial

The following steps install dependencies, generate the sample application, edit the stack, and deploy the Docker container. The commands are identical across languages unless otherwise noted.

Create and initialize the project

Create a workspace for the tutorial and move into it.
Shell
Initialize the project in your preferred language and add the Docker provider. The --local flag keeps Terraform state in the project directory, which is convenient for the quick start.

Edit the code

Replace the generated stack with the following Docker example. Each snippet defines a Docker provider, builds the latest nginx image, and provisions a container that exposes port 8000 on your workstation.

Deploy the container

From the project root, run cdktn deploy (Terraform or OpenTofu will prompt you to approve the plan). This command synthesizes the application, runs terraform plan, and provisions the Docker resources once you enter approve.
Shell
Check the container and the mapped port.
Shell
NGINX running in Docker via Terraform

Destroy the container

When you finish testing, destroy the stack to remove the Docker container and image.
Shell

Next steps

Refer to the navigation sidebar to jump back to Concepts and CLI reference material whenever you need more detail.