0
# Providers and Modules
1
2
Provider configuration and Terraform module integration for managing external resources and reusable infrastructure components.
3
4
## Capabilities
5
6
### TerraformProvider Class
7
8
Base class for all Terraform providers, handling authentication and configuration for specific cloud or service providers.
9
10
```typescript { .api }
11
/**
12
* Base class for Terraform providers
13
*/
14
abstract class TerraformProvider extends TerraformElement {
15
/**
16
* Create a new Terraform provider
17
* @param scope - The parent construct
18
* @param id - Provider identifier
19
* @param config - Provider configuration
20
*/
21
constructor(scope: Construct, id: string, config: TerraformProviderConfig);
22
23
/**
24
* The Terraform resource type for this provider
25
*/
26
readonly terraformResourceType: string;
27
28
/**
29
* Provider metadata for code generation
30
*/
31
readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;
32
33
/**
34
* Fully qualified name for this provider
35
*/
36
readonly fqn: string;
37
38
/**
39
* Alias for this provider instance
40
*/
41
readonly alias?: string;
42
43
/**
44
* Meta attributes for the provider
45
*/
46
readonly metaAttributes: {[key: string]: any};
47
48
/**
49
* Check if a construct is a TerraformProvider
50
*/
51
static isTerraformProvider(x: any): x is TerraformProvider;
52
}
53
```
54
55
**Usage Examples:**
56
57
```typescript
58
import { TerraformProvider } from "cdktf";
59
60
// Custom provider extending TerraformProvider
61
class AwsProvider extends TerraformProvider {
62
constructor(scope: Construct, id: string, config: AwsProviderConfig) {
63
super(scope, id, {
64
terraformResourceType: "aws",
65
...config
66
});
67
}
68
}
69
70
// Using providers
71
const awsUsEast1 = new AwsProvider(this, "aws-us-east-1", {
72
region: "us-east-1"
73
});
74
75
const awsUsWest2 = new AwsProvider(this, "aws-us-west-2", {
76
region: "us-west-2",
77
alias: "west"
78
});
79
80
// Use aliased provider for resources
81
new AwsInstance(this, "west-instance", {
82
provider: awsUsWest2,
83
ami: "ami-12345678",
84
instanceType: "t2.micro"
85
});
86
```
87
88
### TerraformModule Class
89
90
Abstract base class for referencing and using Terraform modules from various sources.
91
92
```typescript { .api }
93
/**
94
* Reference to external Terraform modules
95
*/
96
abstract class TerraformModule extends TerraformElement {
97
/**
98
* Create a new Terraform module reference
99
* @param scope - The parent construct
100
* @param id - Module identifier
101
* @param options - Module configuration
102
*/
103
constructor(scope: Construct, id: string, options: TerraformModuleConfig);
104
105
/**
106
* Create an interpolation for a module output
107
* @param moduleOutput - Name of the module output
108
* @returns Resolvable interpolation
109
*/
110
interpolationForOutput(moduleOutput: string): IResolvable;
111
112
/**
113
* Get a string output from the module
114
* @param output - Name of the output
115
* @returns String value from module output
116
*/
117
getString(output: string): string;
118
119
/**
120
* Add a provider to the module
121
* @param provider - Provider instance or configuration
122
*/
123
addProvider(provider: TerraformProvider | TerraformModuleProvider): void;
124
125
/**
126
* Module source (registry, git, local path, etc.)
127
*/
128
readonly source: string;
129
130
/**
131
* Module version constraint
132
*/
133
readonly version?: string;
134
135
/**
136
* Provider configurations for the module
137
*/
138
readonly providers?: (TerraformProvider | TerraformModuleProvider)[];
139
}
140
```
141
142
**Usage Examples:**
143
144
```typescript
145
import { TerraformModule } from "cdktf";
146
147
// Custom module class
148
class VpcModule extends TerraformModule {
149
constructor(scope: Construct, id: string, config: VpcModuleConfig) {
150
super(scope, id, {
151
source: "terraform-aws-modules/vpc/aws",
152
version: "~> 3.0",
153
...config
154
});
155
}
156
157
get vpcId(): string {
158
return this.getString("vpc_id");
159
}
160
161
get privateSubnets(): string[] {
162
return this.getList("private_subnets");
163
}
164
165
get publicSubnets(): string[] {
166
return this.getList("public_subnets");
167
}
168
}
169
170
// Using the module
171
const vpc = new VpcModule(this, "vpc", {
172
name: "my-vpc",
173
cidr: "10.0.0.0/16",
174
azs: ["us-east-1a", "us-east-1b"],
175
privateSubnets: ["10.0.1.0/24", "10.0.2.0/24"],
176
publicSubnets: ["10.0.101.0/24", "10.0.102.0/24"],
177
enableNatGateway: true
178
});
179
180
// Add provider to module
181
vpc.addProvider(awsProvider);
182
183
// Use module outputs in resources
184
new AwsInstance(this, "web", {
185
ami: "ami-12345678",
186
instanceType: "t2.micro",
187
subnetId: vpc.privateSubnets[0],
188
vpcSecurityGroupIds: [securityGroup.id]
189
});
190
```
191
192
## Configuration Interfaces
193
194
```typescript { .api }
195
interface TerraformProviderConfig {
196
/**
197
* The provider's Terraform resource type
198
*/
199
readonly terraformResourceType: string;
200
201
/**
202
* Provider metadata for code generation
203
*/
204
readonly terraformGeneratorMetadata?: TerraformProviderGeneratorMetadata;
205
206
/**
207
* Alias for this provider instance
208
*/
209
readonly alias?: string;
210
211
/**
212
* Provider-specific configuration options
213
*/
214
readonly [key: string]: any;
215
}
216
217
interface TerraformModuleConfig {
218
/**
219
* Module source (registry, git URL, local path)
220
*/
221
readonly source: string;
222
223
/**
224
* Version constraint for registry modules
225
*/
226
readonly version?: string;
227
228
/**
229
* Input variables for the module
230
*/
231
readonly [key: string]: any;
232
233
/**
234
* Provider configurations to pass to the module
235
*/
236
readonly providers?: (TerraformProvider | TerraformModuleProvider)[];
237
238
/**
239
* Explicit dependencies for the module
240
*/
241
readonly dependsOn?: ITerraformDependable[];
242
243
/**
244
* Number of module instances
245
*/
246
readonly count?: number | TerraformCount;
247
248
/**
249
* Iterator for creating multiple module instances
250
*/
251
readonly forEach?: ITerraformIterator;
252
}
253
254
interface TerraformModuleProvider {
255
/**
256
* Local name for the provider within the module
257
*/
258
readonly name: string;
259
260
/**
261
* Provider instance to use
262
*/
263
readonly provider: TerraformProvider;
264
}
265
266
interface TerraformProviderGeneratorMetadata {
267
/**
268
* Name of the provider
269
*/
270
readonly providerName: string;
271
272
/**
273
* Version of the provider
274
*/
275
readonly providerVersion?: string;
276
277
/**
278
* Version constraint for the provider
279
*/
280
readonly providerVersionConstraint?: string;
281
}
282
```