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
Section titled “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:
workflows: - tag_query: cdktf-code engine: name: cdktfdirs: prod: tags: [cdktf-code] when_modified: file_patterns: ['${DIR}/*.ts'] stacks: 'cdktf-stack1': tags: [] 'cdktf-stack2': tags: []In this configuration:
- The
workflowssection specifies that any directory containing the tagcdktf-codewill enable CDKTF in a Terrateam operation. - The
dirssection defines theproddirectory, which:- Triggers Terrateam operations if any file matching the
*.tspattern is changed in a pull request. - Assigns the tag
cdktf-codeto be used in workflows. - Defines two stacks:
cdktf-stack1andcdktf-stack2.
- Triggers Terrateam operations if any file matching the
Planning and Applying with CDKTF
Section titled “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 plan stack:cdktf-stack1Terrateam will generate the necessary Terraform configuration files from your CDKTF code and execute the plan operation.
Using Environment Variables with CDKTF
Section titled “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:
import { Construct } from "constructs";import { App, TerraformStack } from "cdktf";import * as Null from './.gen/providers/null';class MyStack extends TerraformStack { constructor(scope: Construct, id: string) { super(scope, id); new Null.provider.NullProvider(this, 'test-provider'); new Null.resource.Resource(this, 'test', { triggers: { dir: process.env.TERRATEAM_DIR, workspace: process.env.TERRATEAM_WORKSPACE } }); }}const app = new App();new MyStack(app, "cdktf-stack1");app.synth();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
Section titled “CDKTF Frequently Asked Questions”What Problem Does CDKTF Solve?
Section titled “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?
Section titled “What Languages Does CDKTF Support?”CDKTF supports the following languages:
- TypeScript
- Python
- Java
- C#
- Go
is CDKTF Production-ready?
Section titled “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?
Section titled “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?
Section titled “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?
Section titled “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
synthfunction 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 planorterraform applyon the generated HCL.
Where Can I Learn More About CDKTF?
Section titled “Where Can I Learn More About CDKTF?”Conclusion
Section titled “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.