cdktn init will configure a HCP Terraform workspace and a corresponding remote backend to store state for the new project. If you run cdktn init --local to configure your new project to use a local backend to store state, you can still migrate the state to a remote backend later.
You can configure your CDK Terrain (CDKTN) remote backend to be HCP Terraform, another Terraform supported backend, or a custom location.
When to Use Remote Backends
Consider using a remote backend when multiple individuals or teams need access to your infrastructure state data. Remote state makes it easier for teams to work together because all members have access to the latest state data in the remote store. It also allows you to share output values with other configurations, allowing groups to share infrastructure resources. For example, a core infrastructure team can handle building the core machines and then expose some information that other teams can use for their own infrastructure.Define Remote Backends
You can define a JSON configuration for a remote backend with aTerraformBackend subclass or a JSON configuration file.
The following example uses the TerraformBackend subclass CloudBackend.
cdktn synth, CDKTN stores the backend metadata like the organization name and workspace name in the cdk.tf.json file within the cdktf.out stack sub-directory containing the synthesized CDKTN code. For example, CDKTN creates the output for a stack called hello-terraform in cdktf.out/stacks/hello-terraform.
The following example shows the stack output directory.
cdk.tf.json file.
Initialize Remote Backends
Allcdktn operations perform an automatic terraform init, but you can also initialize manually.
To manually initialize a remote backend, go to the corresponding stack output directory in the cdktf.out folder and run terraform init.
Migrate Local State Storage to Remote
After you define your remote backend, you can migrate existing local state files to the designated remote location. This requires moving Terraform state files to the CDKTN output directory. Consider an example project calledhello-terraform that is using local storage to store the Terraform state. To migrate the local stage files to the remote backend:
- Navigate into the main project directory.
-
Use
CloudBackendto add a new remote backend.
-
Run
cdktn diff <stack name> --migrate-stateto migrate the state into HCP Terraform or Terraform Enterprise.
Supported Backends
In addition to HCP Terraform, Terraform and CDKTN support the following backends.CDK Terrain v0.14 deprecated the artifactory, etcd, etcdv3, manta, and swift backends, and removed them in v0.20. Terraform removed these backends in v1.3. For migration paths from these removed backends, refer to Upgrading to Terraform v1.3.
State Locking with the S3 Backend
When several people or automated pipelines run against the same state, Terraform (or OpenTofu) uses state locking to prevent concurrent runs from corrupting the state file. The S3 backend supports two locking mechanisms:- S3-native locking (recommended) via the
useLockfileproperty. Terraform writes a lock file alongside your state object in the same bucket — at<key>.tflock— and removes it when the operation completes. No additional infrastructure is required. S3-native locking is available in Terraform 1.10 and later and OpenTofu 1.10 and later. - DynamoDB locking via the
dynamodbTableproperty, which points the backend at a DynamoDB table whose partition key isLockID. This is the legacy mechanism and is now deprecated in favor ofuseLockfile.
Enable S3-Native Locking
SetuseLockfile to true on the S3Backend configuration.
cdktn operations needs s3:GetObject, s3:PutObject, and s3:DeleteObject permissions on the lock file path so Terraform can create and release the lock.
S3-native locking requires Terraform 1.10 or OpenTofu 1.10 (or later).
Declare the runtime versions your project supports in the
targetVersions
field of cdktf.json so that synthesis validates native locking against the
versions you target — the default targets (terraform >=1.5.7,
opentofu >=1.6.0) predate it. This is the same declarative,
binary-free check CDK Terrain applies to
function usage at synth time.Migrate from DynamoDB to S3-Native Locking
If you already lock state with a DynamoDB table, you can move to S3-native locking without a window where state is unprotected by enabling both mechanisms for a single run, then removing the DynamoDB table.-
Keep
dynamodbTableand adduseLockfile: trueto yourS3Backendconfiguration. With both set, Terraform acquires both locks. -
Run
cdktn deploy(orcdktn diff) once so the change takes effect and the backend is reconfigured. -
Remove
dynamodbTable, leaving onlyuseLockfile: true. After your next run no longer references the table, you can delete the DynamoDB table.
dynamodbTable remains available for backwards compatibility, but it is deprecated. Terraform deprecated the dynamodb_table argument in the S3 backend starting with v1.11 and may remove DynamoDB-based locking in a future release. Prefer useLockfile for new projects.Escape Hatches
Escape hatches can add to or override existing resources, and you can use them for backends or backend constructs that CDKTN does not natively support. Escape hatch methods have anOverride suffix (e.g., addOverride).
The following example uses an escape hatch to add an unsupported remote backend on a Stack object.