Package: cdktf-multi-stack-tfe
cdktf-multi-stack-tfeSetting 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.UsageYou need to create the initial workspace yourself, in this casemy-app-base.import*ascdktffrom"cdktf";importConstructfrom"constructs";import{BaseStack,Stack,Variable}from"cdktf-multi-stack-tfe";//Weneedtohaveanalreadycreated"base"TFEworkspaceasabasis.//ItwillstoretheTFEworkspaceconfigurationandstateforallstacks.//AsitcreatesallTFEworkspaces,it's required to be created first (and as a result will scaffold out all the required workspaces).classMyAppBaseStackextendsBaseStack{//Thenameissettomy-app-baseconstructor(scope:Construct){//Thiswillconfiguretheremotebackendtousemy-company/my-app-baseasaworkspace//my-companyistheTerraformorganization//my-appistheprefixtouseforallworkspacessuper(scope,"my-company","my-app",{hostname:"app.terraform.io",//canbesettoconfigureadifferentTerraformCloudhostname,e.g.forprivatelyhostedTerraformEnterprisetoken:"my-token",//canbesettoconfigureatokentouse});//Youcandoadditionalthingsinthisstackaswell}}classVpcStackextendsStack{publicvpcId:string//Thisstackwilldependonthebasestackandit//willusethemy-company/my-app-$stackNameworkspaceasabackendconstructor(scope:Construct,stackName:string){super(scope,stackName);//SetupanVPC,etc.this.vpcId=....}}classWebStackextendsStack{constructor(scope:Construct,stackName:string,vpcId:string){super(scope,stackName);//ThisexpectsaTFCvariablecalled"password"inyourbasestackand//willcreateavariablecalled"password"inthisstack.Youcanuse//password.valuetogetthevalueasyouwoulddowithTerraformVariable.constpassword=newVariable(this,"password",{type:"string",sensitive:true});//SetupyourwebappusingthevpcId}}constapp=newcdktf.App();newMyAppBaseStack(app);//thestacknameis"base"//Thiscross-stackreferencewillleadtopermissionsbeingsetupsothat//thestaging-webworkspacecanaccessthestaging-vpcworkspace.constvpc=newVpcStack(app,"staging-vpc");//thestacknameis"staging-vpc"newWeb(app,"staging-web",vpc.vpcId);//thestacknameis"staging-web"constprodVpc=newVpcStack(app,"production-vpc");newWeb(app,"production-web",prodVpc.vpcId);app.synth();ConfigurationWorkspace namingTo control the workspace naming please implement the following method on the BaseStack to your liking:publicgetWorkspaceName(stackName:string):string{return`${this.prefix}-${stackName}`;}Workspace configurationYou configure the created workspaces by settting thedefaultWorkspaceConfigproperty on the BaseStack.
This config is overwritten by the one specified as the thrid argument of aStack(in the super call).WarningThere are some potentially harmful side effects you could run into, so please always carefully read the diff before applying it.Renaming stacksThis 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.