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

providers-modules.mddocs/

Providers and Modules

Provider configuration and Terraform module integration for managing external resources and reusable infrastructure components.

Capabilities

TerraformProvider Class

Base class for all Terraform providers, handling authentication and configuration for specific cloud or service providers.

/**
 * Base class for Terraform providers
 */
abstract class TerraformProvider extends TerraformElement {
  /**
   * Create a new Terraform provider
   * @param scope - The parent construct
   * @param id - Provider identifier
   * @param config - Provider configuration
   */
  constructor(scope: Construct, id: string, config: TerraformProviderConfig);

  /**
   * The Terraform resource type for this provider
   */
  readonly terraformResourceType: string;

  /**
   * Provider metadata for code generation
   */
  readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;

  /**
   * Fully qualified name for this provider
   */
  readonly fqn: string;

  /**
   * Alias for this provider instance
   */
  readonly alias?: string;

  /**
   * Meta attributes for the provider
   */
  readonly metaAttributes: {[key: string]: any};

  /**
   * Check if a construct is a TerraformProvider
   */
  static isTerraformProvider(x: any): x is TerraformProvider;
}

Usage Examples:

import { TerraformProvider } from "cdktf";

// Custom provider extending TerraformProvider
class AwsProvider extends TerraformProvider {
  constructor(scope: Construct, id: string, config: AwsProviderConfig) {
    super(scope, id, {
      terraformResourceType: "aws",
      ...config
    });
  }
}

// Using providers
const awsUsEast1 = new AwsProvider(this, "aws-us-east-1", {
  region: "us-east-1"
});

const awsUsWest2 = new AwsProvider(this, "aws-us-west-2", {
  region: "us-west-2",
  alias: "west"
});

// Use aliased provider for resources
new AwsInstance(this, "west-instance", {
  provider: awsUsWest2,
  ami: "ami-12345678",
  instanceType: "t2.micro"
});

TerraformModule Class

Abstract base class for referencing and using Terraform modules from various sources.

/**
 * Reference to external Terraform modules
 */
abstract class TerraformModule extends TerraformElement {
  /**
   * Create a new Terraform module reference
   * @param scope - The parent construct
   * @param id - Module identifier
   * @param options - Module configuration
   */
  constructor(scope: Construct, id: string, options: TerraformModuleConfig);

  /**
   * Create an interpolation for a module output
   * @param moduleOutput - Name of the module output
   * @returns Resolvable interpolation
   */
  interpolationForOutput(moduleOutput: string): IResolvable;

  /**
   * Get a string output from the module
   * @param output - Name of the output
   * @returns String value from module output
   */
  getString(output: string): string;

  /**
   * Add a provider to the module
   * @param provider - Provider instance or configuration
   */
  addProvider(provider: TerraformProvider | TerraformModuleProvider): void;

  /**
   * Module source (registry, git, local path, etc.)
   */
  readonly source: string;

  /**
   * Module version constraint
   */
  readonly version?: string;

  /**
   * Provider configurations for the module
   */
  readonly providers?: (TerraformProvider | TerraformModuleProvider)[];
}

Usage Examples:

import { TerraformModule } from "cdktf";

// Custom module class
class VpcModule extends TerraformModule {
  constructor(scope: Construct, id: string, config: VpcModuleConfig) {
    super(scope, id, {
      source: "terraform-aws-modules/vpc/aws",
      version: "~> 3.0",
      ...config
    });
  }

  get vpcId(): string {
    return this.getString("vpc_id");
  }

  get privateSubnets(): string[] {
    return this.getList("private_subnets");
  }

  get publicSubnets(): string[] {
    return this.getList("public_subnets");
  }
}

// Using the module
const vpc = new VpcModule(this, "vpc", {
  name: "my-vpc",
  cidr: "10.0.0.0/16",
  azs: ["us-east-1a", "us-east-1b"],
  privateSubnets: ["10.0.1.0/24", "10.0.2.0/24"],
  publicSubnets: ["10.0.101.0/24", "10.0.102.0/24"],
  enableNatGateway: true
});

// Add provider to module
vpc.addProvider(awsProvider);

// Use module outputs in resources
new AwsInstance(this, "web", {
  ami: "ami-12345678",
  instanceType: "t2.micro",
  subnetId: vpc.privateSubnets[0],
  vpcSecurityGroupIds: [securityGroup.id]
});

Configuration Interfaces

interface TerraformProviderConfig {
  /**
   * The provider's Terraform resource type
   */
  readonly terraformResourceType: string;

  /**
   * Provider metadata for code generation
   */
  readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;

  /**
   * Alias for this provider instance
   */
  readonly alias?: string;

  /**
   * Provider-specific configuration options
   */
  readonly [key: string]: any;
}

interface TerraformModuleConfig {
  /**
   * Module source (registry, git URL, local path)
   */
  readonly source: string;

  /**
   * Version constraint for registry modules
   */
  readonly version?: string;

  /**
   * Input variables for the module
   */
  readonly [key: string]: any;

  /**
   * Provider configurations to pass to the module
   */
  readonly providers?: (TerraformProvider | TerraformModuleProvider)[];

  /**
   * Explicit dependencies for the module
   */
  readonly dependsOn?: ITerraformDependable[];

  /**
   * Number of module instances
   */
  readonly count?: number | TerraformCount;

  /**
   * Iterator for creating multiple module instances
   */
  readonly forEach?: ITerraformIterator;
}

interface TerraformModuleProvider {
  /**
   * Local name for the provider within the module
   */
  readonly name: string;

  /**
   * Provider instance to use
   */
  readonly provider: TerraformProvider;
}

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

  /**
   * Version of the provider
   */
  readonly providerVersion?: string;

  /**
   * Version constraint for the provider
   */
  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