cdktf-multi-stack-tfe

Setting up Terraform Cloud / Terraform Enterprise workspaces can be tiring when dealing with CDK for Terraform applications spanning multiple stacks and therefore workspaces. This library aims to automate this.
cdktf-multi-stack-tfe is in technical preview, which means it's a community supported project. It still requires extensive testing and polishing to mature into a HashiCorp officially supported project. Please file issues generously and detail your experience while using the library. We welcome your feedback.
By using the software in this repository, you acknowledge that:
- cdktf-multi-stack-tfe is still in development, may change, and has not been released as a commercial product by HashiCorp and is not currently supported in any way by HashiCorp.
- cdktf-multi-stack-tfe is provided on an "as-is" basis, and may include bugs, errors, or other issues.
- cdktf-multi-stack-tfe is NOT INTENDED FOR PRODUCTION USE, use of the Software may result in unexpected results, loss of data, or other unexpected results, and HashiCorp disclaims any and all liability resulting from use of cdktf-multi-stack-tfe.
- HashiCorp reserves all rights to make all decisions about the features, functionality and commercial release (or non-release) of cdktf-multi-stack-tfe, at any time and without any obligation or liability whatsoever.
Compatibility
cdktf
>= 0.21.0
constructs
>= 10.4.2
Usage
You need to create the initial workspace yourself, in this case my-app-base
.
import * as cdktf from "cdktf";
import Construct from "constructs";
import { BaseStack, WorkspaceStack, Variable } from "cdktf-multi-stack-tfe";
class MyAppBaseStack extends BaseStack {
constructor(scope: Construct) {
super(scope, "my-company", "my-app", {
hostname: "app.terraform.io",
token: "my-token",
});
}
}
class VpcStack extends WorkspaceStack {
public vpcId: string
constructor(scope: Construct, stackName: string) {
super(scope, stackName);
this.vpcId = ....
}
}
class WebStack extends WorkspaceStack {
constructor(scope: Construct, stackName: string, vpcId: string) {
super(scope, stackName);
const password = new Variable(this, "password", {
type: "string",
sensitive: true
});
}
}
const app = new cdktf.App();
new MyAppBaseStack(app);
const vpc = new VpcStack(app, "staging-vpc");
new Web(app, "staging-web", vpc.vpcId);
const prodVpc = new VpcStack(app, "production-vpc");
new Web(app, "production-web", prodVpc.vpcId);
app.synth();
Configuration
Workspace naming
To control the workspace naming please implement the following method on the BaseStack to your liking:
public getWorkspaceName(stackName: string): string {
return `${this.prefix}-${stackName}`;
}
Workspace configuration
You configure the created workspaces by settting the defaultWorkspaceConfig
property on the BaseStack.
This config is overwritten by the one specified as the thrid argument of a Stack
(in the super call).
Warning
There are some potentially harmful side effects you could run into, so please always carefully read the diff before applying it.
Renaming stacks
This is not supported by the library, if you rename an already existing stack the workspace hosting it will be destroyed and a new one with the new name will be created. This means all references to the infrastructure provisioned in the old stack will be lost, making it impossible to destroy the infrastructure through terraform. In this case we recommend destroying the stack, renaming it and then re-creating it.
There are some ways around this issue, but the library currently does not support them.