Cloud Development Kit for Terraform - programmatic infrastructure as code using familiar programming languages
Terraform resource and data source management providing the foundation for infrastructure provisioning with full attribute access, lifecycle controls, and state management.
Represents a Terraform resource that creates and manages infrastructure components.
/**
* Represents a Terraform resource that creates and manages infrastructure
*/
class TerraformResource extends TerraformElement {
/**
* Create a new Terraform resource
* @param scope - The parent construct
* @param id - Resource identifier
* @param config - Resource configuration
*/
constructor(scope: Construct, id: string, config: TerraformResourceConfig);
/**
* Get a string attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns String value of the attribute
*/
getStringAttribute(terraformAttribute: string): string;
/**
* Get a number attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Numeric value of the attribute
*/
getNumberAttribute(terraformAttribute: string): number;
/**
* Get a list attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Array of string values
*/
getListAttribute(terraformAttribute: string): string[];
/**
* Get a boolean attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Boolean value as resolvable
*/
getBooleanAttribute(terraformAttribute: string): IResolvable;
/**
* Get a number list attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Array of numeric values
*/
getNumberListAttribute(terraformAttribute: string): number[];
/**
* Get a string map attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Map of string values
*/
getStringMapAttribute(terraformAttribute: string): {[key: string]: string};
/**
* Get a number map attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Map of numeric values
*/
getNumberMapAttribute(terraformAttribute: string): {[key: string]: number};
/**
* Get a boolean map attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Map of boolean values
*/
getBooleanMapAttribute(terraformAttribute: string): {[key: string]: boolean};
/**
* Get any map attribute from the resource
* @param terraformAttribute - Name of the attribute
* @returns Map of any values
*/
getAnyMapAttribute(terraformAttribute: string): {[key: string]: any};
/**
* Create an interpolation for an attribute
* @param terraformAttribute - Name of the attribute
* @returns Resolvable interpolation
*/
interpolationForAttribute(terraformAttribute: string): IResolvable;
/**
* Import an existing resource into Terraform state
* @param id - The ID of the existing resource
* @param provider - Optional provider instance
*/
importFrom(id: string, provider?: TerraformProvider): void;
/**
* Move this resource to a new location in the configuration
* @param moveTarget - The move target
* @param index - Optional index for resources with for_each
*/
moveTo(moveTarget: string, index?: string | number): void;
/**
* Move this resource to a resource with the given ID
* @param id - The target resource ID
*/
moveToId(id: string): void;
/**
* Move the resource corresponding to "id" to this resource
* @param id - Full id of resource being moved from
*/
moveFromId(id: string): void;
/**
* Add a target for resource moves
* @param moveTarget - The move target identifier
*/
addMoveTarget(moveTarget: string): void;
/**
* Check if this resource has resource move operations
* @returns True if resource has move operations
*/
hasResourceMove(): boolean;
/**
* Check if a construct is a TerraformResource
*/
static isTerraformResource(x: any): x is TerraformResource;
}Usage Examples:
import { TerraformResource } from "cdktf";
import { Construct } from "constructs";
// Custom resource class extending TerraformResource
class AwsInstance extends TerraformResource {
constructor(scope: Construct, id: string, config: AwsInstanceConfig) {
super(scope, id, {
terraformResourceType: "aws_instance",
terraformGeneratorMetadata: {
providerName: "aws"
},
...config
});
}
// Attribute getters
get id(): string {
return this.getStringAttribute("id");
}
get publicIp(): string {
return this.getStringAttribute("public_ip");
}
get tags(): {[key: string]: string} {
return this.getStringMapAttribute("tags");
}
}
// Using the resource
const instance = new AwsInstance(this, "web-server", {
ami: "ami-12345678",
instanceType: "t2.micro",
tags: {
Name: "Web Server",
Environment: "production"
}
});
// Access computed attributes
const instanceId = instance.id;
const publicIp = instance.publicIp;
// Import existing resource
instance.importFrom("i-1234567890abcdef0");Represents a Terraform data source that reads information about existing infrastructure.
/**
* Represents a Terraform data source for reading existing infrastructure
*/
class TerraformDataSource extends TerraformElement {
/**
* Create a new Terraform data source
* @param scope - The parent construct
* @param id - Data source identifier
* @param config - Data source configuration
*/
constructor(scope: Construct, id: string, config: TerraformResourceConfig);
/**
* Get a string attribute from the data source
* @param terraformAttribute - Name of the attribute
* @returns String value of the attribute
*/
getStringAttribute(terraformAttribute: string): string;
/**
* Get a number attribute from the data source
* @param terraformAttribute - Name of the attribute
* @returns Numeric value of the attribute
*/
getNumberAttribute(terraformAttribute: string): number;
/**
* Get a list attribute from the data source
* @param terraformAttribute - Name of the attribute
* @returns Array of string values
*/
getListAttribute(terraformAttribute: string): string[];
/**
* Get a boolean attribute from the data source
* @param terraformAttribute - Name of the attribute
* @returns Boolean value as resolvable
*/
getBooleanAttribute(terraformAttribute: string): IResolvable;
/**
* All other attribute getter methods same as TerraformResource
*/
/**
* Create an interpolation for an attribute
* @param terraformAttribute - Name of the attribute
* @returns Resolvable interpolation
*/
interpolationForAttribute(terraformAttribute: string): IResolvable;
/**
* Check if a construct is a TerraformDataSource
*/
static isTerraformDataSource(x: any): x is TerraformDataSource;
}Usage Examples:
import { TerraformDataSource } from "cdktf";
import { Construct } from "constructs";
// Custom data source class
class DataAwsAmi extends TerraformDataSource {
constructor(scope: Construct, id: string, config: DataAwsAmiConfig) {
super(scope, id, {
terraformResourceType: "aws_ami",
terraformGeneratorMetadata: {
providerName: "aws"
},
...config
});
}
get id(): string {
return this.getStringAttribute("id");
}
get name(): string {
return this.getStringAttribute("name");
}
get ownerId(): string {
return this.getStringAttribute("owner_id");
}
}
// Using the data source
const ubuntu = new DataAwsAmi(this, "ubuntu", {
mostRecent: true,
owners: ["099720109477"], // Canonical
filters: [{
name: "name",
values: ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}]
});
// Use data source output in resources
const instance = new AwsInstance(this, "server", {
ami: ubuntu.id,
instanceType: "t2.micro"
});Enhanced resource class with built-in import capabilities for managing existing infrastructure.
/**
* Enhanced resource with import capabilities
*/
class ImportableResource extends TerraformResource {
/**
* Import configuration for the resource
*/
readonly importId?: string;
/**
* Generate import statements for this resource
* @returns Import configuration object
*/
generateImportStatements(): {[key: string]: string};
}interface TerraformResourceConfig {
/**
* The Terraform resource type (e.g., "aws_instance")
*/
readonly terraformResourceType: string;
/**
* Provider metadata for code generation
*/
readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;
/**
* The provider instance to use for this resource
*/
readonly provider?: TerraformProvider;
/**
* Resources this resource depends on
*/
readonly dependsOn?: ITerraformDependable[];
/**
* Number of resources to create
*/
readonly count?: number | TerraformCount;
/**
* Iterator for creating multiple similar resources
*/
readonly forEach?: ITerraformIterator;
/**
* Lifecycle configuration for the resource
*/
readonly lifecycle?: TerraformResourceLifecycle;
/**
* Provisioners to run when creating/destroying the resource
*/
readonly provisioners?: (FileProvisioner | LocalExecProvisioner | RemoteExecProvisioner)[];
/**
* Connection configuration for provisioners
*/
readonly connection?: SSHProvisionerConnection | WinrmProvisionerConnection;
}
interface TerraformResourceLifecycle {
/**
* Prevent destruction of this resource
* @default false
*/
readonly preventDestroy?: boolean;
/**
* Create replacement resource before destroying the original
* @default false
*/
readonly createBeforeDestroy?: boolean;
/**
* Ignore changes to specified attributes
*/
readonly ignoreChanges?: string[] | "all";
/**
* Replace resource when any of these attributes change
*/
readonly replaceTriggeredBy?: any[];
/**
* Preconditions that must be met before resource operations
*/
readonly precondition?: TerraformCondition[];
/**
* Postconditions that must be met after resource operations
*/
readonly postcondition?: TerraformCondition[];
}
interface TerraformCondition {
/**
* Condition expression that must evaluate to true
*/
readonly condition: any;
/**
* Error message to display if condition fails
*/
readonly errorMessage: string;
}