What are HCP Terraform and Terraform Enterprise?
CDKTN supports HCP Terraform and Terraform Enterprise. HCP Terraform is a SaaS application that runs Terraform in a stable, remote environment and securely stores state and secrets. It includes a user interface that helps you better understand your Terraform operations and resources, allows you to define role-based access controls, and offers a private registry for sharing modules and providers. HCP Terraform also integrates with the Terraform CLI and connects to common version control systems (VCS) like GitHub, GitLab, and Bitbucket. When you connect a HCP Terraform workspace to a VCS repository, new commits and changes can automatically trigger Terraform plans. HCP Terraform also offers an API, allowing you to integrate it into existing workflows. Terraform Enterprise lets you set up a self-hosted distribution of HCP Terraform and is ideal for organizations with strict security and compliance requirements.When to use HCP Terraform or Terraform Enterprise?
Terraform uses persisted state data to keep track of the real-world resources it manages. By default, Terraform writes state to a local file. We recommend integrating with HCP Terraform to store state remotely. This prevents accidental loss of locally stored state and lets multiple people access the state data so they can work together on that collection of infrastructure resources. Other benefits of HCP Terraform and Terraform Enterprise include the ability to run Terraform remotely, manage variables and secrets, and enforce policies for sets of infrastructure.Set up CDKTN with HCP Terraform / Terraform Enterprise
After you sign up for a HCP Terraform Account / create an account in your Terraform Enterprise instance, you must connect your CDKTN project to one or more workspaces. Workspaces function like working directories for distinct Terraform configurations and are associated with a Terraform configuration and its state file. They can also contain variables that you can use to manage credentials. Workspaces can have one of three workflows that determine how HCP Terraform / Enterprise interacts with your CDKTN application: CLI-driven, Version control, or API-driven (a more advanced option).CLI-driven Workflow
You can configure the HCP Terraform CLI-driven workflow for both new and existing CDKTN projects. This workflow lets you run CDKTN CLI commands to deploy your application and run Terraform operations remotely on HCP Terraform / Enterprise.Connect New CDKTN Projects
First usecdktn login to log into HCP Terraform or your Terraform Enterprise instance:
- HCP Terraform
- Terraform Enterprise
This command will take you through an interactive session to log in.If you already have a HCP Terraform API token, you can pass it into stdin:
- Run
cdktn initwithout passing the--localflag. This creates a new template CDKTN project for your chosen programming language. It will be the basis for a new application with the necessary file structure for you to start defining infrastructure. If you’re using Terraform Enterprise, you must also pass the--tfe-hostnameflag with the hostname of your enterprise instance. - Choose a project language and provide a project name and description.
- Select a HCP Terraform organization and a name for your workspace. CDKTN will create a CloudBackend in your project.
- You can choose whether to send crash reports to the CDKTN team and select the providers you want to use.
Connect Existing CDKTN Projects
For existing projects, do the following:- Create a HCP Terraform workspace with the CLI-driven workflow.
-
Add the
RemoteBackendto your CDKTN application. The following example connects the application to a HCP Terraform / Enterprise workspace calledmy-app-prod. -
Run
cdktn login/cdktn login --tfe-hostname=tfe.my-company.comand log in to HCP Terraform / Enterprise.
RemoteBackend. You must set the workspace to Remote Execution mode if you want to store state and run Terraform operations remotely.
VCS-driven Workflow
Check your synthesized code into version control and connect your HCP Terraform / Terraform Enterprise workspace to that repository with the VCS-driven workflow. You can configure the workspace to trigger Terraform runs based on merges and commits to the repository. Refer to Deployment Patterns for more details about how to use CDKTN with the VCS-driven workflow.Managing Variables and Secrets
HCP Terraform / Terraform Enterprise variables let you define the variables and secrets that Terraform uses during remote operations. You can set variables specifically for each workspace or you can create variable sets to reuse the same variables across multiple workspaces. For example, you could define a variable set of provider credentials and automatically apply it to all of the workspaces using that provider. To use variables, set your workspace to Remote Execution Mode. Terraform can only access workspace variables when executing remotely on HCP Terraform / Enterprise.-
Use the
TerraformVariableconstruct to declare variables in your CDKTN application. This creates an undefined Terraform input variable. The following example demonstrates how to create aTerraformVariablecalledmy-var. - Add variables that you declared in your CDKTN application to your HCP Terraform / Enterprise workspace. Refer to Add a Variable for details. This lets Terraform access the value during operations.
Variable Return Values
The return value forTerraformVariable is an object with various representations of the value as attributes. You can pass this object as a string with value.stringValue, as a number with value.numberValue, or as a list with value.listValue.
This means that there is no actual value in the variable field when CDKTN synthesizes your application. Instead, CDKTN uses a Token, which represents a value that is unknown until Terraform applies your configuration. You cannot use tokens in dynamic checks during runtime. For example, if (value.listValue.length > 42) { always returns false because tokenized lists have a static length of one item.