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.
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.
npm (stable)
npm (development)
Homebrew (macOS)
Shell
npm install --global cdktn-cli@latest
Shell
npm install --global cdktn-cli@next
Shell
brew install cdktn
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.
Confirm that the CLI is on your PATH and review the available subcommands.
Shell
cdktn helpcdktnCommands: cdktn init Create a new cdktn project from a template. cdktn deploy Deploy the given stacks (aliases: apply) cdktn destroy Destroy the given stacks cdktn synth Synthesize Terraform configuration for your appOptions: --version Show version number -h, --help Show help
Use cdktn init --help whenever you need more detail on initialization flags.
Shell
cdktn init --helpCreate a new cdktn project from a template.Options include: --template <name|url> Built-in templates: csharp, go, java, python, python-pip, typescript --project-name <text> Project directory name --providers <list> Comma-separated providers to preconfigure --local Store Terraform state locally (default false) --cdktn-version <tag> CLI version to scaffold with
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 a workspace for the tutorial and move into it.
Shell
mkdir learn-cdktn-dockercd learn-cdktn-docker
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.
cdktn init --template=typescript --providers=kreuzwerker/docker --localNote: By supplying '--local' option you have chosen local storage mode.? Project Name learn-cdktn-docker? Project Description A simple getting started project for cdktn.? Do you want to start from an existing Terraform project? No? Do you want to send crash reports to the CDKTN team? Yesadded 301 packages in 6s========================================================================================================Your cdktn typescript project is ready!
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.
import { Construct } from "constructs";import { App, TerraformStack } from "cdktn";import { DockerProvider } from "@cdktn/provider-docker/lib/provider";import { Image } from "@cdktn/provider-docker/lib/image";import { Container } from "@cdktn/provider-docker/lib/container";class MyStack extends TerraformStack { constructor(scope: Construct, name: string) { super(scope, name); new DockerProvider(this, "docker", {}); const dockerImage = new Image(this, "nginxImage", { name: "nginx:latest", keepLocally: false, }); new Container(this, "nginxContainer", { name: "tutorial", image: dockerImage.name, ports: [ { internal: 80, external: 8000, }, ], }); }}const app = new App();new MyStack(app, "learn-cdktn-docker");app.synth();
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
cdktn deploy learn-cdktn-dockerInitializing the backend...Initializing provider plugins...Terraform used the selected providers to generate the following execution plan.Resource actions are indicated with the following symbols: + create# docker_container.nginxContainer will be created# docker_image.nginxImage will be createdPlan: 2 to add, 0 to change, 0 to destroy.Do you want to perform these actions? Enter a value: approveApply complete! Resources: 2 added, 0 changed, 0 destroyed.
When you finish testing, destroy the stack to remove the Docker container and image.
Shell
cdktn destroy learn-cdktn-dockerPlan: 0 to add, 0 to change, 2 to destroy.Do you really want to destroy all resources? Enter a value: approveDestroy complete! Resources: 2 destroyed.