CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-cdktf

Cloud Development Kit for Terraform - programmatic infrastructure as code using familiar programming languages

Overview
Eval results
Files

core-infrastructure.mddocs/

Core Infrastructure

Core infrastructure components provide the foundational classes for building CDKTF applications, including the root App construct and TerraformStack containers for organizing resources.

Capabilities

App Class

The root construct representing a CDKTF application. All stacks must be contained within an App.

/**
 * The root construct representing a CDKTF application
 */
class App extends Construct {
  /**
   * Create a new CDKTF app
   * @param config - Optional configuration for the app
   */
  constructor(config?: AppConfig);

  /**
   * Synthesize all resources to the output directory
   */
  synth(): void;

  /**
   * Create a cross-stack reference between stacks
   * @param fromStack - The stack making the reference
   * @param toStack - The stack being referenced
   * @param identifier - The identifier for the reference
   * @returns Reference string for use in the fromStack
   */
  crossStackReference(fromStack: TerraformStack, toStack: TerraformStack, identifier: string): string;

  /**
   * Check if a construct is an App
   */
  static isApp(x: any): x is App;

  /**
   * Get the App instance from any construct in the tree
   */
  static of(construct: IConstruct): App;

  readonly outdir: string;
  readonly targetStackId?: string;
  readonly context: {[key: string]: any};
}

Usage Example:

import { App, TerraformStack } from "cdktf";

// Create app with default settings
const app = new App();

// Create app with custom configuration
const customApp = new App({
  outdir: "./terraform-output",
  context: {
    "feature:enableNewBehavior": true
  }
});

// Synthesize to generate Terraform configuration
app.synth();

TerraformStack Class

A container for Terraform resources, equivalent to a Terraform configuration. Each stack becomes a separate Terraform workspace.

/**
 * Container for Terraform resources, equivalent to a Terraform configuration
 */
class TerraformStack extends Construct {
  /**
   * Create a new Terraform stack
   * @param scope - The parent construct (usually an App)
   * @param id - The stack identifier
   */
  constructor(scope: Construct, id: string);

  /**
   * Synthesize the stack to Terraform JSON format
   * @returns Terraform configuration as JSON
   */
  toTerraform(): any;

  /**
   * Synthesize the stack to HCL format
   * @returns Terraform configuration as HCL
   */
  toHclTerraform(): any;

  /**
   * Add an override to the synthesized Terraform configuration
   * @param path - JSON path to override (e.g., "resource.aws_instance.example.tags")
   * @param value - Value to set at the path
   */
  addOverride(path: string, value: any): void;

  /**
   * Get the logical ID for a Terraform element within this stack
   * @param tfElement - The element to get the ID for
   * @returns Logical ID string
   */
  getLogicalId(tfElement: ITerraformAddressable): string;

  /**
   * Run all validations for the stack
   */
  runAllValidations(): void;

  /**
   * Ensure a backend exists for this stack
   * @returns The backend instance
   */
  ensureBackendExists(): TerraformBackend;

  /**
   * Add a dependency on another stack
   * @param dependency - The stack this stack depends on
   */
  addDependency(dependency: TerraformStack): void;

  /**
   * Check if a construct is a TerraformStack
   */
  static isStack(x: any): x is TerraformStack;

  /**
   * Get the TerraformStack from any construct in the stack tree
   */
  static of(construct: IConstruct): TerraformStack;

  readonly dependencies: TerraformStack[];
  readonly synthesizer: IStackSynthesizer;
}

Usage Examples:

import { App, TerraformStack } from "cdktf";
import { Construct } from "constructs";

class MyInfrastructureStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // Add resources, data sources, outputs here
  }
}

// Create multiple stacks
const app = new App();
const networkingStack = new MyInfrastructureStack(app, "networking");
const computeStack = new MyInfrastructureStack(app, "compute");

// Add stack dependency
computeStack.addDependency(networkingStack);

// Add configuration overrides
networkingStack.addOverride("terraform.required_version", ">= 1.0");

// Synthesize all stacks
app.synth();

TerraformElement Class

Abstract base class for all Terraform elements (resources, data sources, providers, etc.).

/**
 * Base class for all Terraform elements
 */
abstract class TerraformElement extends Construct {
  /**
   * Create a new Terraform element
   * @param scope - The parent construct
   * @param id - Element identifier
   * @param elementType - Type of Terraform element
   */
  constructor(scope: Construct, id: string, elementType?: string);

  /**
   * Synthesize element to Terraform JSON format
   * @returns JSON representation
   */
  abstract toTerraform(): any;

  /**
   * Synthesize element to HCL format
   * @returns HCL representation
   */
  toHclTerraform(): any;

  /**
   * Get metadata for the element
   * @returns Metadata object
   */
  toMetadata(): any;

  /**
   * Override synthesized attributes
   * @param path - Path to attribute
   * @param value - Value to set
   */
  addOverride(path: string, value: any): void;

  /**
   * Override the logical ID for this element
   * @param newLogicalId - New logical ID to use
   */
  overrideLogicalId(newLogicalId: string): void;

  /**
   * Reset the logical ID override
   */
  resetOverrideLogicalId(): void;

  /**
   * The fully-qualified name of this element type
   */
  readonly fqn: string;

  /**
   * Unique identifier for this element
   */
  readonly friendlyUniqueId: string;

  /**
   * The stack this element belongs to
   */
  readonly cdktfStack: TerraformStack;

  /**
   * The Terraform resource/element type
   */
  readonly terraformResourceType: string;

  /**
   * Generator metadata for this element
   */
  readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;
}

Configuration Interfaces

interface AppConfig {
  /**
   * Additional context values to make available to the app
   */
  readonly context?: {[key: string]: any};

  /**
   * Output directory for generated Terraform files
   * @default "."
   */
  readonly outdir?: string;

  /**
   * Include stack traces in error messages
   * @default false
   */
  readonly stackTraces?: boolean;

  /**
   * Generate HCL output instead of JSON
   * @default false
   */
  readonly hclOutput?: boolean;

  /**
   * Skip backend validation during synthesis
   * @default false
   */
  readonly skipBackendValidation?: boolean;

  /**
   * Skip all validation during synthesis
   * @default false
   */
  readonly skipValidation?: boolean;
}

interface IStackSynthesizer {
  /**
   * Synthesize the associated stack to the session
   * @param session - The synthesis session
   */
  synthesize(session: ISynthesisSession): void;
}

interface ISynthesisSession {
  /**
   * The output directory for synthesis artifacts
   */
  readonly outdir: string;

  /**
   * Whether to skip validation
   */
  readonly skipValidation: boolean;
}

interface TerraformProviderGeneratorMetadata {
  /**
   * The version of the provider
   */
  readonly providerName: string;

  /**
   * The version of the provider
   */
  readonly providerVersion?: string;

  /**
   * The version of the generator used
   */
  readonly providerVersionConstraint?: string;
}

Install with Tessl CLI

npx tessl i tessl/npm-cdktf

docs

annotations-aspects.md

backend-configuration.md

core-infrastructure.md

index.md

iterators-dynamic.md

providers-modules.md

provisioners.md

resources-data-sources.md

terraform-functions.md

testing.md

tokens-expressions.md

variables-outputs.md

tile.json