CDKTF
CDKTF (Cloud Development Kit for Terraform) is a tool that allows you to define your infrastructure using familiar programming languages such as TypeScript, Python, Java, C#, and Go. Terrateam supports CDKTF, enabling you to manage your CDKTF-based infrastructure using Terrateam’s powerful collaboration and automation features.
Enabling CDKTF
To use CDKTF with Terrateam, you need to update your Terrateam configuration file (.terrateam/config.yml
) to enable CDKTF for specific directories or workflows.
Here’s an example configuration that enables CDKTF for a directory named prod
:
In this configuration:
- The
workflows
section specifies that any directory containing the tagcdktf-code
will enable CDKTF in a Terrateam operation. - The
dirs
section defines theprod
directory, which:- Triggers Terrateam operations if any file matching the
*.ts
pattern is changed in a pull request. - Assigns the tag
cdktf-code
to be used in workflows. - Defines two stacks:
cdktf-stack1
andcdktf-stack2
.
- Triggers Terrateam operations if any file matching the
Planning and Applying with CDKTF
Once CDKTF is enabled in your Terrateam configuration, you can use the standard Terrateam commands to plan and apply your CDKTF-based infrastructure.
For example, to plan any directory with a stack named cdktf-stack1
, you can comment the following in a pull request:
Terrateam will generate the necessary Terraform configuration files from your CDKTF code and execute the plan operation.
Using Environment Variables with CDKTF
Terrateam provides several built-in environment variables that you can use in your CDKTF code to access information about the current workflow execution context. Some useful variables include:
TERRATEAM_DIR
: The directory being processedTERRATEAM_WORKSPACE
: The workspace being usedTERRATEAM_PLAN_FILE
: The path to the generated plan fileTERRATEAM_ROOT
: The absolute path to the root of your checked-out repository You can reference these variables in your CDKTF code to make it more dynamic and adaptable to different environments and workflows.
Here’s an example of using environment variables in a TypeScript CDKTF stack:
In this example, the TERRATEAM_DIR
and TERRATEAM_WORKSPACE
environment variables are used to set the triggers
attribute of a null_resource
, making the resource dependent on the current directory and workspace being processed by Terrateam.
CDKTF Frequently Asked Questions
What Problem Does CDKTF Solve?
CDKTF allows developers to manage their infrastructure using familiar programming languages instead of the Terraform HCL language. This enables developers to leverage their existing programming skills and the ecosystem of their language of choice when developing infrastructure components.
What Languages Does CDKTF Support?
CDKTF supports the following languages:
- TypeScript
- Python
- Java
- C#
- Go
is CDKTF Production-ready?
Yes, CDKTF is generally available and ready for production usage as of August 1, 2022. While it is still a maturing technology, you can confidently use it in a production environment.
What are the Advantages Of CDKTF?
- Developers familiar with programming languages but not HCL can contribute to infrastructure changes.
- Leveraging the ecosystem and mature development environments of supported programming languages.
- Programming languages are more powerful and expressive than HCL.
- More people are familiar with programming languages than HCL.
What are the Disadvantages Of CDKTF?
- CDKTF is still maturing, and the ecosystem of providers and resources is growing.
- Documentation, especially around providers, may be lacking.
- CDKTF adds complexity by converting code written in a programming language to HCL.
- CDKTF introduces new concepts like “stacks,” making it not a direct one-to-one translation of Terraform.
- The increased expressiveness of programming languages can lead to more complex and harder-to-maintain code compared to HCL.
How Does CDKTF Work?
The high-level process of how CDKTF works is as follows:
- Developers write code in their preferred programming language supported by CDKTF.
- The code creates an instance of an “app” and defines “stacks,” “providers,” and “resources.”
- The
synth
function is called to convert the app and its components to a JSON representation of HCL. - The CDKTF CLI runs the code and generates the corresponding Terraform HCL files.
- The CDKTF CLI can then execute
terraform plan
orterraform apply
on the generated HCL.
Where Can I Learn More About CDKTF?
Conclusion
CDKTF is a powerful tool that allows developers to define their infrastructure using familiar programming languages, making it easier for them to contribute to infrastructure changes and leverage their existing skills and ecosystem.