Skip to main content
In this tutorial, you use CDK Terrain (CDKTN) to deploy an example web application on Kubernetes. You will:
  • Create a local Kubernetes cluster with kind.
  • Convert an existing Terraform configuration into TypeScript.
  • Refactor into reusable constructs and add tests.
  • Deploy custom frontend and backend images via a local registry.
  • Deploy a second stack representing a test environment.

Prerequisites

  • Terraform v1.2+ (or OpenTofu)
  • CDKTN v0.15+
  • Docker Desktop (or equivalent) installed and running
  • Node.js (v22.19+) and npm (v10+)
  • kubectl
  • kind
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.

Clone example repository

Shell
Shell

Start a local registry

Start a local Docker registry on 127.0.0.1:5000.
Shell

Create a kind cluster

Create the cluster.
Shell
Verify the cluster.
Shell
Shell
Write a kubeconfig for this cluster that you will reference from CDKTN.
Shell
Connect the local registry to the kind network and apply the registry hosting config.
Shell
Shell

Initialize your CDKTN application

Create the app/ directory and move into it.
Shell
Shell
Initialize a TypeScript CDKTN project using the Kubernetes provider.
Shell

Create a Kubernetes Deployment

Convert the provided Terraform configuration into TypeScript.
Shell
Install dependencies.
Shell
Synthesize the application.
Shell
Deploy the stack.
Shell
Verify the deployment.
Shell

Scale the deployment

After you update the deployment to use 4 replicas, deploy again.
Shell
Verify the updated replica count.
Shell

Refactor your deployment

Create a constructs/ directory.
Shell
Create constructs/kubernetes-web-app.ts.
TypeScript
Export the construct from constructs/index.ts.
TypeScript
Update app/main.ts to use your new construct.
TypeScript
Deploy the refactored application.
Shell
Verify the new deployment.
Shell

Add tests

Create app/jest.setup.js.
JavaScript
Create app/__tests__/kubernetes-web-app-test.ts.
TypeScript
Run tests.
Shell

Add a NodePort Service

Add a NodePort service interface.
TypeScript
Add the NodePort service construct.
TypeScript
Add a test for the new construct.
TypeScript
Run tests.
Shell

Add a NodePortService to your application

Add the NodePort service in main.ts.
TypeScript
Deploy.
Shell
Verify the service and request it.
Shell
Shell

Refactor constructs

Destroy the stack before continuing.
Shell
Add a Terraform output import to constructs/kubernetes-web-app.ts.
TypeScript
Add a new construct that represents an application composed of a Deployment and NodePort Service.
TypeScript
Add tests for the new construct.
TypeScript
Run tests.
Shell

Use the SimpleKubernetesWebApp construct

Update your imports.
TypeScript
Replace the previous deployment + service with a single construct.
TypeScript
Deploy.
Shell
Verify and request it.
Shell
Shell

Deploy a custom image

Build and push the frontend image.
Shell
Shell
Shell
Shell
Deploy the updated frontend image.
Shell
Shell
Verify deployment.
Shell
Verify the response.
Shell

Add a backend service

Build and push the backend image.
Shell
Shell
Update main.ts to add a backend application.
TypeScript
Deploy.
Shell
Shell
Verify deployments.
Shell
Verify the backend.
Shell

Deploy another application stack

Refactor the stack to accept frontend and backend configuration.
TypeScript
Deploy.
Shell
Add a second stack to represent a test environment.
TypeScript
Deploy the new stack.
Shell
Verify both environments.
Shell

Clean up resources

Destroy the test stack.
Shell
Destroy the dev stack.
Shell
Delete the kind cluster and remove the local registry.
Shell
Shell
Shell

Next steps