Skip to main content
This guide shows how to deploy Amazon S3 buckets using CDK for Terraform (CDKTF) with TypeScript. You’ll learn how to:
  • configure providers (AWS and random),
  • build a reusable construct that adds an env tag,
  • create two S3 buckets (one with a random suffix),
  • synthesize and deploy using the Terraform engine.
Prerequisites: Node.js, Yarn, and basic TypeScript knowledge. For more details see the CDKTF docs and Terraform docs:

Install dependencies

Change into the CDKTF project directory and install dependencies with Yarn:
Example (trimmed) output:

CDKTF TypeScript example (complete)

Below is a concise, complete TypeScript example that demonstrates:
  • AWS and random provider configuration
  • a reusable S3BucketWithEnvTag construct that adds an env tag
  • a stack creating two S3 buckets (one with a random suffix)
  • app instantiation and synthesize
Notes about the example:
  • The S3BucketWithEnvTag class is a reusable construct (component) that encapsulates bucket creation and tagging logic.
  • The RandomId from the random provider is used to create unique bucket names to avoid collisions.
  • objectLockEnabled: true is set in the example; ensure your bucket configuration and AWS account support object lock if you plan to use it.

Quick reference: common commands

Deploy with CDKTF

When ready to deploy, run:
You will be prompted to confirm the apply, similar to the Terraform CLI. Example (trimmed) output after a successful deploy:

What CDKTF does under the hood

  • Your TypeScript code is synthesized by CDKTF into Terraform configuration (HCL/JSON).
  • The generated Terraform configuration is executed by the Terraform engine and the specified providers, which create the actual cloud resources (S3 buckets in this example).
A truncated example of the synthesized Terraform JSON metadata:

Resources created

After apply completes, verify the two new S3 buckets in the AWS Management Console. The second bucket will include the env: dev tag that the reusable construct added.
If you prefer writing infrastructure in a familiar programming language (TypeScript in this example), CDKTF lets you build reusable constructs and synthesize them into Terraform configuration that still uses the Terraform engine and provider ecosystem.
An overview of how the TypeScript → Terraform flow fits into larger multi-provider and multi-language workflows is discussed later in this article.

Watch Video