cdktf.json file, and CDK Terrain (CDKTN) generates the necessary code bindings for you to use in your application.
Install Modules
You can use modules from the Terraform Registry (or OpenTofu Registry) and other sources like GitHub in your application. For example, the following TypeScript project has amain.ts file that defines AWS resources and uses the AWS VPC module.
Add Module to cdktf.json
To use a module in your application, you must first add it to the terraformModules array in the cdktf.json configuration file.
To add a module from the Terraform Registry or a private registry, provide a fully qualified name: registry-namespace/module-name.
terraform-aws-modules/vpc/aws//submodules/vpc-endpoints, where after the // is the path to the submodule in the modules repository. Refer to the Terraform source specification for more details.
When you are using local modules that reference other local modules you need to add all referenced modules to the terraformModules array.
Generate Module Bindings
Go to the working directory and runcdktn get. CDKTN automatically creates the appropriate module bindings in the ./.gen directory for you to use in your application.
Configure Modules
You can configure modules in the same way as resources, with one exception. For module inputs that use themap type, like map(string) or list(map(string)), you must specify the map values as strings. You must also ensure that the keys follow the required format listed in the module’s documentation. For example, the module may specify that the keys must be in snake case.
Work with Module Outputs
Modules often return data that you can use as inputs to other modules or resources. When this data is only available after Terraform (or OpenTofu) applies the configuration, you must use Terraform Outputs.Examples
The following example uses a local module and references its output as a Terraform output.Create Modules
While we generally recommend generating code bindings for modules, you can also use theTerraformHclModule class to reference any module that Terraform supports. Both methods create identical synthesized Terraform configuration files, but using TerraformHclModule does not generate any types or type-safe inputs or outputs.
The following example uses TerraformHclModule to import an AWS module.