Skip to main content

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.

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, Stack, Variable } from "cdktf-multi-stack-tfe";

// We need to have an already created "base" TFE workspace as a basis.
// It will store the TFE workspace configuration and state for all stacks.
// As it creates all TFE workspaces, it's required to be created first (and as a result will scaffold out all the required workspaces).
class MyAppBaseStack extends BaseStack {
  // The name is set to my-app-base
  constructor(scope: Construct) {
    // This will configure the remote backend to use my-company/my-app-base as a workspace
    // my-company is the Terraform organization
    // my-app is the prefix to use for all workspaces
    super(scope, "my-company", "my-app", {
      hostname: "app.terraform.io", // can be set to configure a different Terraform Cloud hostname, e.g. for privately hosted Terraform Enterprise
      token: "my-token", // can be set to configure a token to use
    });

    // You can do additional things in this stack as well
  }
}

class VpcStack extends Stack {
  public vpcId: string

  // This stack will depend on the base stack and it
  // will use the my-company/my-app-$stackName workspace as a backend
  constructor(scope: Construct, stackName: string) {
    super(scope, stackName);

    // Setup an VPC, etc.

    this.vpcId = ....
  }
}

class WebStack extends Stack {
  constructor(scope: Construct, stackName: string, vpcId: string) {
    super(scope, stackName);

    // This expects a TFC variable called "password" in your base stack and
    // will create a variable called "password" in this stack. You can use
    // password.value to get the value as you would do with TerraformVariable.
    const password = new Variable(this, "password", {
      type: "string",
      sensitive: true
    });


    // Setup your webapp using the vpcId
  }
}

const app = new cdktf.App();
new MyAppBaseStack(app); // the stack name is "base"

// This cross-stack reference will lead to permissions being set up so that
// the staging-web workspace can access the staging-vpc workspace.
const vpc = new VpcStack(app, "staging-vpc"); // the stack name is "staging-vpc"
new Web(app, "staging-web", vpc.vpcId); // the stack name is "staging-web"

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.

Metadata

Release files for cdktf-multi-stack-tfe 0.0.96

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cdktf-multi-stack-tfe 0.0.96
File Size Uploaded Provenance Actions
cdktf-multi-stack-tfe-0.0.96.tar.gz 35.8 kB Without provenance Details for cdktf-multi-stack-tfe-0.0.96.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cdktf-multi-stack-tfe 0.0.96
File Interpreter ABI Platform Actions
cdktf_multi_stack_tfe-0.0.96-py3-none-any.whl Python 3 none any Details for cdktf_multi_stack_tfe-0.0.96-py3-none-any.whl

Total release size: 69.6 kB

Release files / cdktf-multi-stack-tfe-0.0.96.tar.gz

Download URL cdktf-multi-stack-tfe-0.0.96.tar.gz
Size 35.8 kB
Tags Source
SHA-256 checksum
How to use checksums
79e8cecdb0342fa5cbcd1bd0fb194309028702bba22a70b11674ee17fd662c1b
BLAKE2b-256 checksum
How to use checksums
3ec119547424a7ecac85dbb4caa44a54bd81e54dc2b21f1cdefddd8e69441a5f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release files / cdktf_multi_stack_tfe-0.0.96-py3-none-any.whl

Download URL cdktf_multi_stack_tfe-0.0.96-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e89cc7a4f6256aba0f6f490a04a1635752ec833c4100071140a0e66683acc55
BLAKE2b-256 checksum
How to use checksums
ac92208d37f51e904815ae9278730fbcb2815cb5d6104445546f42ef885f7fd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for cdktf-multi-stack-tfe 0.0.96

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.96 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page