Cloud Development Kit for Terraform - programmatic infrastructure as code using familiar programming languages
Input variables and output values for parameterizing infrastructure and exposing computed values, enabling flexible and reusable Terraform configurations.
Input variables that parameterize your Terraform configuration, allowing customization without modifying the code.
/**
* Terraform input variable for parameterizing configurations
*/
class TerraformVariable extends TerraformElement {
/**
* Create a new Terraform variable
* @param scope - The parent construct
* @param id - Variable identifier
* @param config - Variable configuration
*/
constructor(scope: Construct, id: string, config: TerraformVariableConfig);
/**
* Add a validation rule to the variable
* @param validation - Validation configuration
*/
addValidation(validation: TerraformVariableValidationConfig): void;
/**
* The variable value as a string
*/
readonly stringValue: string;
/**
* The variable value as a number
*/
readonly numberValue: number;
/**
* The variable value as a list of strings
*/
readonly listValue: string[];
/**
* The variable value as a boolean
*/
readonly booleanValue: IResolvable;
/**
* The raw variable value
*/
readonly value: any;
/**
* Default value for the variable
*/
readonly default?: any;
/**
* Description of the variable
*/
readonly description?: string;
/**
* Type constraint for the variable
*/
readonly type?: string;
/**
* Whether the variable is sensitive
*/
readonly sensitive?: boolean;
/**
* Whether the variable can be null
*/
readonly nullable?: boolean;
/**
* Validation rules for the variable
*/
readonly validation?: TerraformVariableValidationConfig[];
}Usage Examples:
import { TerraformVariable, VariableType } from "cdktf";
// Simple string variable
const region = new TerraformVariable(this, "region", {
type: VariableType.STRING,
default: "us-east-1",
description: "AWS region for resources"
});
// Number variable with validation
const instanceCount = new TerraformVariable(this, "instance_count", {
type: VariableType.NUMBER,
default: 1,
description: "Number of instances to create"
});
instanceCount.addValidation({
condition: `var.instance_count >= 1 && var.instance_count <= 10`,
errorMessage: "Instance count must be between 1 and 10"
});
// List variable
const availabilityZones = new TerraformVariable(this, "availability_zones", {
type: VariableType.list(VariableType.STRING),
default: ["us-east-1a", "us-east-1b"],
description: "List of availability zones"
});
// Object variable
const instanceConfig = new TerraformVariable(this, "instance_config", {
type: VariableType.object({
instance_type: VariableType.STRING,
key_name: VariableType.STRING,
monitoring: VariableType.BOOL
}),
default: {
instance_type: "t2.micro",
key_name: "default",
monitoring: false
}
});
// Sensitive variable
const dbPassword = new TerraformVariable(this, "db_password", {
type: VariableType.STRING,
description: "Database password",
sensitive: true
});
// Using variable values in resources
new AwsInstance(this, "web", {
ami: "ami-12345678",
instanceType: instanceConfig.value.instance_type,
availabilityZone: availabilityZones.listValue[0],
keyName: instanceConfig.value.key_name,
tags: {
Environment: region.stringValue,
InstanceCount: instanceCount.stringValue
}
});Output values that expose computed attributes and results from your Terraform configuration.
/**
* Terraform output for exposing computed values
*/
class TerraformOutput extends TerraformElement {
/**
* Create a new Terraform output
* @param scope - The parent construct
* @param id - Output identifier
* @param config - Output configuration
*/
constructor(scope: Construct, id: string, config: TerraformOutputConfig);
/**
* The output value
*/
readonly value: any;
/**
* Description of the output
*/
readonly description?: string;
/**
* Whether the output is sensitive
*/
readonly sensitive?: boolean;
/**
* Resources this output depends on
*/
readonly dependsOn?: ITerraformDependable[];
/**
* Static ID for the output (overrides generated ID)
*/
readonly staticId?: boolean;
/**
* Check if a construct is a TerraformOutput
*/
static isTerraformOutput(x: any): x is TerraformOutput;
}Usage Examples:
import { TerraformOutput } from "cdktf";
class MyStack extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
const vpc = new AwsVpc(this, "vpc", {
cidrBlock: "10.0.0.0/16"
});
const instance = new AwsInstance(this, "web", {
ami: "ami-12345678",
instanceType: "t2.micro",
vpcSecurityGroupIds: [securityGroup.id],
subnetId: subnet.id
});
// Simple string output
new TerraformOutput(this, "vpc_id", {
value: vpc.id,
description: "VPC ID"
});
// Sensitive output
new TerraformOutput(this, "instance_private_ip", {
value: instance.privateIp,
description: "Private IP address of the instance",
sensitive: true
});
// Complex object output
new TerraformOutput(this, "instance_info", {
value: {
id: instance.id,
public_ip: instance.publicIp,
private_ip: instance.privateIp,
availability_zone: instance.availabilityZone
},
description: "Complete instance information"
});
// Output with dependencies
new TerraformOutput(this, "database_endpoint", {
value: database.endpoint,
description: "RDS database endpoint",
dependsOn: [database]
});
// List output
new TerraformOutput(this, "subnet_ids", {
value: subnets.map(subnet => subnet.id),
description: "List of subnet IDs"
});
}
}Utility class for defining variable type constraints.
/**
* Type constraint definitions for Terraform variables
*/
class VariableType {
/**
* String type constraint
*/
static readonly STRING: string;
/**
* Number type constraint
*/
static readonly NUMBER: string;
/**
* Boolean type constraint
*/
static readonly BOOL: string;
/**
* Any type constraint (no validation)
*/
static readonly ANY: string;
/**
* List type constraint
*/
static readonly LIST: string;
/**
* Map type constraint
*/
static readonly MAP: string;
/**
* Set type constraint
*/
static readonly SET: string;
/**
* Create a list type with element type constraint
* @param elementType - Type of list elements
* @returns List type string
*/
static list(elementType: string): string;
/**
* Create a map type with element type constraint
* @param elementType - Type of map values
* @returns Map type string
*/
static map(elementType: string): string;
/**
* Create a set type with element type constraint
* @param elementType - Type of set elements
* @returns Set type string
*/
static set(elementType: string): string;
/**
* Create a tuple type with specified element types
* @param elements - Array of element type strings
* @returns Tuple type string
*/
static tuple(...elements: string[]): string;
/**
* Create an object type with specified attribute types
* @param attributes - Map of attribute names to types
* @returns Object type string
*/
static object(attributes: {[key: string]: string}): string;
/**
* Create an optional object attribute
* @param type - The attribute type
* @returns Optional attribute type string
*/
static optional(type: string): string;
}Usage Examples:
import { TerraformVariable, VariableType } from "cdktf";
// Basic types
const region = new TerraformVariable(this, "region", {
type: VariableType.STRING
});
const count = new TerraformVariable(this, "count", {
type: VariableType.NUMBER
});
const enabled = new TerraformVariable(this, "enabled", {
type: VariableType.BOOL
});
// Collection types
const zones = new TerraformVariable(this, "zones", {
type: VariableType.list(VariableType.STRING)
});
const tags = new TerraformVariable(this, "tags", {
type: VariableType.map(VariableType.STRING)
});
const uniquePorts = new TerraformVariable(this, "ports", {
type: VariableType.set(VariableType.NUMBER)
});
// Complex types
const serverConfig = new TerraformVariable(this, "server_config", {
type: VariableType.object({
instance_type: VariableType.STRING,
disk_size: VariableType.NUMBER,
monitoring: VariableType.BOOL,
backup_retention: VariableType.optional(VariableType.NUMBER)
})
});
const vpcCidrs = new TerraformVariable(this, "vpc_cidrs", {
type: VariableType.tuple(
VariableType.STRING, // VPC CIDR
VariableType.STRING, // Public subnet CIDR
VariableType.STRING // Private subnet CIDR
)
});interface TerraformVariableConfig {
/**
* Default value for the variable
*/
readonly default?: any;
/**
* Description of the variable's purpose
*/
readonly description?: string;
/**
* Type constraint for the variable
*/
readonly type?: string;
/**
* Whether the variable value is sensitive
* @default false
*/
readonly sensitive?: boolean;
/**
* Whether the variable can be set to null
* @default true
*/
readonly nullable?: boolean;
/**
* Validation rules for the variable
*/
readonly validation?: TerraformVariableValidationConfig[];
}
interface TerraformVariableValidationConfig {
/**
* Condition expression that must evaluate to true
*/
readonly condition: string;
/**
* Error message to show when validation fails
*/
readonly errorMessage: string;
}
interface TerraformOutputConfig {
/**
* The value to output (can be a literal, reference, or expression)
*/
readonly value: any;
/**
* Description of what this output represents
*/
readonly description?: string;
/**
* Whether the output value is sensitive
* @default false
*/
readonly sensitive?: boolean;
/**
* Explicit dependencies for this output
*/
readonly dependsOn?: ITerraformDependable[];
/**
* Use a static ID instead of generating one
* @default false
*/
readonly staticId?: boolean;
/**
* Conditions that must be met before creating the output
*/
readonly precondition?: TerraformCondition[];
}