Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

CDK for Terraform and HCL Interoperability

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.

Steps to deploy one module

  1. Switch to the cdktf directory: cd cdktf
  2. Install the dependencies: npm install
  3. Synthesize the Terraform code: cdktf synth
  4. Move the modules over to the hcl directory: cp -rf cdktf.out/stacks/* ../hcl/modules
  5. Switch to the hcl directory: cd ../hcl
  6. Prepare the working directory: terraform init
  7. Deploy the infrastructure: terraform apply
  8. Check the deployment: docker container ls
  9. Destroy the infrastructure: terraform destroy

Steps to deploy two modules

  1. Switch to the cdktf directory: cd cdktf

  2. Install the dependencies: npm install

  3. Edit the main.ts file and add the following line:

    new MyStack(app, "learn-cdktf-docker-2");

  4. Synthesize the Terraform code: cdktf synth

  5. Move the modules over to the hcl directory: cp -rf cdktf.out/stacks/* ../hcl/modules

  6. Switch to the hcl directory: cd ../hcl

  7. 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
    }
    
  8. Prepare the working directory: terraform init

  9. Deploy the infrastructure: terraform apply

  10. Check the deployment: docker container ls

  11. Destroy the infrastructure: terraform destroy