CDK for Terraform offers you the possibility to use it in existing Terraform Projects. You can find more information in the HCL Interoperability documentation.
In this example we create a Terraform module written with CDK for Terraform. To show this, we create a Docker Container locally. So make sure you have Docker Desktop running.
- Switch to the cdktf directory:
cd cdktf
- Install the dependencies:
npm install
- Synthesize the Terraform code:
cdktf synth
- Move the modules over to the hcl directory:
cp -rf cdktf.out/stacks/* ../hcl/modules
- Switch to the hcl directory:
cd ../hcl
- Prepare the working directory:
terraform init
- Deploy the infrastructure:
terraform apply
- Check the deployment:
docker container ls
- Destroy the infrastructure:
terraform destroy
-
Switch to the cdktf directory:
cd cdktf
-
Install the dependencies:
npm install
-
Edit the main.ts file and add the following line:
new MyStack(app, "learn-cdktf-docker-2");
-
Synthesize the Terraform code:
cdktf synth
-
Move the modules over to the hcl directory:
cp -rf cdktf.out/stacks/* ../hcl/modules
-
Switch to the hcl directory:
cd ../hcl
-
Edit the main.tf file and add the following lines at the end:
module "learn_cdktf_2" { source = "./modules/learn-cdktf-docker-2" port = 8080 } output "name_2" { value = module.learn_cdktf_2.container_id }
-
Prepare the working directory:
terraform init
-
Deploy the infrastructure:
terraform apply
-
Check the deployment:
docker container ls
-
Destroy the infrastructure:
terraform destroy