CLI Commands
The CDK for Terraform CLI has the following commands:
- completion
- convert
- debug
- deploy
- destroy
- diff
- get
- init
- login
- output
- provider add
- provider upgrade
- provider list
- synth
- watch
completion
This command outputs a script that you can use to set up autocompletion for bash or zsh.
The output also contains the installation instructions. For example, here are the instructions for Mac OSX:
After you configure auto completion, reload your shell by running source ~/.zshrc
, source ~/.bash_profile
or opening a new terminal window. You can now autocomplete cdktf
commands by pressing the <TAB>
key. You may need to enter a space after cdktf
for autocomplete to take effect.
convert
This command converts Terraform configuration written in HCL to the equivalent configuration in your preferred language.
->Note: The convert
command has known limitations.
Examples
Convert a local file.
Convert a local file and wrap it into a cdktf.TerraformStack
instead of a cdktf.Construct
.
Convert HCL in your clipboard to Python on OSX.
deploy
This command deploys a given application.
Help Output
Note: The parallelism
flag has a different behavior than the terraform parallelism flag. To set the custom terraform parallelism flag, please use the --terraform-parallelism
flag instead.
Examples
Deploy an application.
Deploy an application with automatic approval of the diff (Terraform plan).
Deploy multiple stacks in one run.
Deploy all stacks in one run:
Deploy all stacks ending with -production
in one run:
If the stacks have dependencies (through cross stack references or by calling myStack.addDependency(otherStack)
) deploy will figure out the right order to run.
For more info on the --outputs-file
option, refer to the output
command.
destroy
This command destroys a given application.
Help output:
Note: The parallelism
flag has a different behavior than the terraform parallelism flag. To set the custom terraform parallelism flag, please use the --terraform-parallelism
flag instead.
Examples
Destroy an application.
Destroy an application with automatic approval of the diff (Terraform plan).
Destroy multiple stacks in one run.
Destroy all stacks in one run:
Destroy all stacks ending with production in one run:
If the stacks have dependencies (through cross stack references or by calling myStack.addDependency(otherStack)
) deploy will figure out the right order to run.
diff
This command generates a diff for a given application by running Terraform plan.
Help output:
Note: The parallelism
flag has a different behavior than the terraform parallelism flag. To set the custom terraform parallelism flag, please use the --terraform-parallelism
flag instead.
Examples:
Generate a diff for a given application.
get
This command downloads the providers and modules for an application and
generates CDK constructs for them. It can use the cdktf.json
configuration file to read the list of providers and modules.
This command only generates currently missing provider bindings, so it is very fast if nothing changed.
The command does not update the version when you specify loose version constraints in your cdktf.json
file and have already generated an existing version locally. To update the version anyway, run cdktf get --force
to recreate all bindings.
When you change a version constraint, the cdktf get
command recreates the bindings for that provider.
Help Output
Examples
Download the providers and modules defined in the cdktf.json
configuration file.
If you run into issues generating a lot of bindings you can use the --parallelism
option to limit the number of bindings generated in parallel.
init
This command creates a new CDK for Terraform project using a template.
Help Output
Examples
Create a new Typescript project.
Create a new Python project and use a specific version of the cdktf
package.
Create a new Typescript project from an existing Terraform codebase. Currently, you can only use the --from-terraform-project
flag with TypeScript, and there are some known limitations.
login
This command helps log in to Terraform Cloud by fetching a Terraform Cloud API token.
Help Output
Please note that we currently expect custom TFE instances to be using the https
protocol.
Examples
Fetch an API token from Terraform Cloud.
synth
This command synthesizes Terraform configuration for an application. CDKTF stores the synthesized configuration in the cdktf.out
directory, unless you use the --output
flag to specify a different location. The output folder is ephemeral and might be erased for each synth
that you run manually or that happens automatically when you run deploy
, diff
, or destroy
.
Help Output
Examples
Synthesize code for an application.
Synthesize code when providing a custom command to execute and an output directory.
Synthesize code in Terraform HCL instead of JSON
watch
Warning: The watch
command is experimental, so you should only use it in development environments. It also automatically deploys all changes without asking for confirmation.
When the watch
command is first run it creates a watchPattern
in your cdktf.json
based on the language you configured. It's a list of glob patterns matching all files that should be watched.
Whenever a file matching the watch pattern is changed, the command will run cdktf deploy --auto-approve
for you. It allows for rapid iterations when developing infrastructure, especially when working with serverless services. You can specify the stacks you want to deploy or you can use cdktf watch --auto-approve '*'
to deploy all stacks.
Requirements
Before using watch
you should check your environment. The watch
command should only be used for development environments. We recommend making sure that the terminal where you want to run watch
has no access keys that allow the cdktf-cli
to deploy to your production environment.
Run watch
Help Output
Note: The parallelism
flag has a different behavior than the terraform parallelism flag. To set the custom terraform parallelism flag, please use the --terraform-parallelism
flag instead.
Examples
Run watch
on the development stack (dev). The --auto-approve
flag skips the explicit plan approval step and is currently always required.
Besides for the required --auto-approve
flag, you can use the same configuration options as in cdktf deploy
.
Troubleshoot watch
Set the CDKTF_LOG_LEVEL
environment variable to all
and set CDKTF_LOG_FILE_DIRECTORY
to your projects root directory.
The debug output is directed to a cdktf.log
file in your projects root directory. The log contains information about detected file system changes and the actions they triggered.
The debug output is directed to a cdktf.log
file in your projects root directory. The log contains information about detected file system changes and the actions they triggered.
output
This command gets the outputs defined in the Terraform configuration of the given stack. It uses terraform output
under the hood.
Help Output
--outputs-file
The --outputs-file
option allows you to specify a file where the stack outputs will be written as JSON.
The JSONs structure is matching your construct structure, the name of each construct is used as a key.
Please be aware that sensitive outputs are only written to the file if the --outputs-file-include-sensitive-outputs
option is used.
debug
This command prints debug information about the current project and environment to help troubleshoot issues.
Help Output
The debug information depends on the programming language. The following example is from a Java application, where CDKTF collects information about Java and Maven. CDKTF detects the installed constructs
version through Maven.
provider add
This command facilitates adding Terraform providers to a CDKTF project. If a pre-built provider is available for the CDKTF version you are using and the Terraform provider version you requested (if any), it will be installed using e.g. npm install
or dotnet add
depending on the language you are using. Otherwise, the provider will be added to the cdktf.json
config and cdktf get
will be automatically invoked to generate local provider bindings.
Help Output
Examples
Add the aws
provider to the project. As the namespace of the AWS Terraform provider is hashicorp
it can be left out.
Add a specific version of the aws
provider to the project. You can use the @
symbol to specify a version constraint.
Add multiple providers to the project and force local generation of provider bindings.
Add a provider from a private registry to the project.
provider upgrade
This command lets you upgrade Terraform providers in a CDKTF project to the newest version compatible with your cdktf
version. You can also optionally add a version constraint.
If your project has the associated pre-built provider already installed, CDKTF updates the pre-built provider. Otherwise, CDKTF adds the provider to the cdktf.json
configuration file and invokes cdktf get
to generate local provider bindings.
provider list
This command prints out a table (or json with the --json
option) containing details about the providers installed for the project. It gathers information for both locally built and pre-built providers.
Information printed varies between locally generated and prebuilt providers. The information for each provider includes the Terraform provider version, the CDKTF version, and the package name and version (if pre-built provider).
Help Output
Examples
Upgrade the aws
provider to the newest version available for your cdktf
version.
Upgrade the aws
provider to the newest version available for your cdktf
version and at version 4
of the provider version.
Upgrade multiple providers to the latest version available for your cdktf
version.