Custom construct classes are reusable infrastructure configurations written in a programming language. After you write a custom construct, you can translate it to multiple languages and publish it for others to import and use in CDK Terrain (CDKTN) applications.Documentation Index
Fetch the complete documentation index at: https://cdktn.io/docs/llms.txt
Use this file to discover all available pages before exploring further.
Translate Constructs to Terraform Modules
You can write a CDKTN construct class and then synthesize the code into an HCL Terraform module. When you use this approach, you are limited to the structure and functionality available in a Terraform module. For example, you cannot use enums or methods to provide advanced configuration options. Use the cdktf-tf-module-stack to transform your code into a Terraform module. This library exposesTFModuleStack, TFModuleVariable, TFModuleOutput constructs that you can use as replacements for TerraformStack, TerraformVariable, TerraformOutput.
The library also offers a ProviderRequirement construct that replaces the provider construct you typically use in CDKTN applications. Typical provider constructs pin a specific provider version, but the ProviderRequirement construct lets you select a version range. This behavior more closely matches how many modules specify provider versions.
The following example shows a CDKTN program written using the cdktf-tf-module-stack library. CDKTN could then synthesize this code into a cdktf.tf.json file to use with Terraform. If you want to use this module, you need to list the full path to the folder containing the synthesized file, such as ../my-cdktn-application/cdktf.out/stacks/my-awesome-module.
Publish Translated HCL Modules
Follow the Terraform module publishing requirements and instructions. You can use the projen-cdktf-hybrid-construct to make this workflow easier. Refer to the CDK Day 2022 talk on this topic for more details.Publish Constructs with Projen
We recommend using thecdktn-construct projen template to package and publish your constructs to either public or private registries. Projen synthesizes files and directories into different target languages based on a Javascript or Typescript file. The process is similar to how CDKTN synthesizes code into JSON configuration files for Terraform.
The projen deployment mechanism uses GitHub Actions, so cdktn-construct expects GitHub to host the Git repository for your construct package. If you prefer to use a different CI provider, you can use projen to bootstrap the project, run yarn run eject to opt out of projen, and then customize the CI implementation.
Use the following steps to publish your constructs with the cdktn-construct projen template.
Initialize Construct Package
Navigate into your construct directory and runnpx projen new cdktn-construct to initialize a new construct package. The script creates a new project that you can configure and then deploy to private or public registries.
Configure the Construct Project
Projen manages all non-code files, like the package.json or JSII configuration, so must modify the.projenrc.js configuration to include your construct’s code files.
The following example shows how to configure your .projenrc.js file to publish your project to public or private registries.
npm run projen to generate the setup and workflow files for your project You must do this every time you change the .projenrc.js file.
Set GitHub Actions Secrets
The GitHub Action requires the following environment variables for credentials.NPM_TOKEN: An npm API token or a token for the registry of your choiceTWINE_USERNAME: The username to use for PyPi, if using a token it should be “token”TWINE_PASSWORD: The PyPi API token to authenticate against PyPi or the registry of your choiceNUGET_API_KEY: The API key to authenticate against NuGet
MAVEN_GPG_PRIVATE_KEYMAVEN_GPG_PRIVATE_KEY_PASSPHRASEMAVEN_PASSWORDMAVEN_USERNAMEMAVEN_STAGING_PROFILE_ID