0

Instead of referring terraform module in cdktf i want to refer CDKTF typescript files to combine several resources as one. How can i achieve this?

Below is for terraform module but expecting something like this for cdktf

"terraformModules": [   
{
"name": "my-local-module",
"source": "./New folder"
}
],

2 Answers2

1

You can use import {myExport} from "./path-to-ts-file"; and export myExport = ... in the file you are exporting from. You can see an example for this in the serverless typescript demo: https://github.com/hashicorp/cdktf-integration-serverless-example/blob/main/main.ts#L5 & https://github.com/hashicorp/cdktf-integration-serverless-example/blob/main/posts/index.ts#L13

Daniel Schmidt
  • 11,605
  • 5
  • 38
  • 70
  • can i write something like this for my existing cdktf scripts to convert into modules .... module "rg" { source = "./Modules/01_Azure_ResGrp" Resource_Group_Name = var.Resource_Group_Name Resource_Group_Location = var.Resource_Group_Location } – Amoghavarsh P Jun 16 '22 at 05:30
  • If you want to make your cdktf code available I'd encourage you to check out my talk on CDK day around this topic: https://www.youtube.com/watch?v=s8tO-ymVQPg&t=10260s – Daniel Schmidt Jun 16 '22 at 09:19
  • TL;DR: https://github.com/DanielMSchmidt/cdktf-tf-module-stack is a good way to solve this problem – Daniel Schmidt Jun 16 '22 at 09:20
0

You need to import the Terraform module, then run cdktf get and finally import it, e.g.:

import { MyLocalModule } from "./.gen/modules/my-local-module";
javierlga
  • 1,409
  • 9
  • 14
  • This is only for terraform module but i have cdktf scripts and i want to create module for that something like module "rg" { source = "./Modules/01_Azure_ResGrp" Purpose = var.Purpose Project_Name = var.Project_Name Environment = var.Environment Resource_Group_Name = var.Resource_Group_Name Resource_Group_Location = var.Resource_Group_Location } – Amoghavarsh P Jun 16 '22 at 05:29
  • Yes, once you import you're module into TS, you can create a class that extends TerraformStack, for example: `class MyLocalModule extends TerraformStack`, that's where you will start setting the variables and everything as a regular module. – javierlga Jun 16 '22 at 16:06
  • An example for the import statement in Python would be `from imports.terraform_google_modules.google import ProjectFactory` – Mikkel Aug 08 '22 at 17:58