cdktf.json file and generate code bindings manually. To use providers in your application, you can import them from the Terraform Registry or the OpenTofu Registry, or from your local machine.
What Are Providers?
Provider plugins like the AWS provider or the cloud-init provider act as a translation layer that allows Terraform to communicate with many different cloud providers, databases, and services. Terraform uses providers to provision resources, which describe one or more infrastructure objects like virtual networks and compute instances. Each provider on the Terraform Registry (also available on the OpenTofu Registry) has documentation detailing available resources and their configuration options.Install Pre-Built Providers
It can take several minutes for CDKTN to generate the code bindings for providers with very large schemas, so we offer several popular providers as pre-built packages. Pre-built providers are a completely optional performance optimization, and you may prefer to generate the code bindings for these providers yourself. For example, you may want to use a different version of that provider than the one in the pre-built package. The Terraform CDK Providers page has a complete list, but available pre-built providers include the following options:- AWS Provider
- Google Provider
- Azure Provider
- Kubernetes Provider
- Docker Provider
- Github Provider
- Null Provider
npm install to install a pre-built provider, you should not define that provider again in your cdktf.json file. If you receive errors while running cdktn synth because of duplicate providers, remove the duplicates from your cdktf.json file, delete tsbuildinfo.json, and run cdktn synth again.
Add Providers with CLI
Use theprovider add command to automatically install a pre-built provider if available. If a pre-built provider is not available, CDKTN uses local provider bindings.
Import Providers
CDK Terrain lets you import Terraform providers to your project. This TypeScript example project has amain.ts file that defines AWS resources.
Add Provider to cdktf.json
To use a new provider, first add it to the "terraformProviders" array in the cdktf.json file.
The following example adds the DNS Simple provider.
Generate Classes
Go to the working directory and runcdktn get to create the appropriate TypeScript classes for the provider automatically.
Import Classes
Import and use the generated classes in your application. The following example imports theDnsimpleProvider and Record resources from ./.gen/providers/dnsimple and defines them.
synth command to convert your code into a JSON Terraform configuration file.
Provider Caching
Caching prevents CDK Terrain from re-downloading providers between each CLI command. It is also useful when you need to remove thecdktf.out folder and re-synthesize your configuration. Finally, caching is necessary when you use multiple stacks within one application.
Set the Caching Directory
Refer to the Terraform documentation about how to configure your plugin cache. Otherwise, CDKTN automatically sets theTF_PLUGIN_CACHE_DIR environment variable to $HOME/.terraform.d/plugin-cache when you use cdktn cli commands.
To disable this behavior, set CDKTF_DISABLE_PLUGIN_CACHE_ENV to a non null value, like CDKTF_DISABLE_PLUGIN_CACHE_ENV=1. You may want to do this when a different cache directory is configured via a .terraformrc configuration file.
Use a Local Provider
Terraform needs to know the location of local providers to enable CDKTN to generate the appropriate type bindings. You can configure this in two ways: Once configured properly, you can reference these providers in thecdktf.json file the same way that you reference providers from the Terraform Registry. Refer to the project configuration documentation for more details about the cdktf.json specification.