- configure providers (AWS and random),
- build a reusable construct that adds an
envtag, - create two S3 buckets (one with a random suffix),
- synthesize and deploy using the Terraform engine.
- CDKTF: https://developer.hashicorp.com/terraform/cdktf
- Terraform: https://www.terraform.io/docs
- AWS S3: https://docs.aws.amazon.com/s3/latest/userguide/Welcome.html
Install dependencies
Change into the CDKTF project directory and install dependencies with Yarn:CDKTF TypeScript example (complete)
Below is a concise, complete TypeScript example that demonstrates:- AWS and random provider configuration
- a reusable
S3BucketWithEnvTagconstruct that adds anenvtag - a stack creating two S3 buckets (one with a random suffix)
- app instantiation and synthesize
- The
S3BucketWithEnvTagclass is a reusable construct (component) that encapsulates bucket creation and tagging logic. - The
RandomIdfrom therandomprovider is used to create unique bucket names to avoid collisions. objectLockEnabled: trueis 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: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).
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.