nx-terraform 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/README.md +478 -5
  2. package/generators.json +5 -0
  3. package/package.json +1 -1
  4. package/src/generators/init/README.md +90 -0
  5. package/src/generators/preset/README.md +152 -0
  6. package/src/generators/preset/generator.js +7 -0
  7. package/src/generators/preset/generator.js.map +1 -1
  8. package/src/generators/terraform-backend/README.md +211 -0
  9. package/src/generators/terraform-backend/schema.json +2 -2
  10. package/src/generators/terraform-module/README.md +308 -0
  11. package/src/generators/terraform-module/files/simple-module/README.md__tmpl__ +22 -0
  12. package/src/generators/terraform-module/files/simple-module/__ignoreFile__ +3 -0
  13. package/src/generators/terraform-module/files/simple-module/main.tf__tmpl__ +16 -0
  14. package/src/generators/terraform-module/files/simple-module/outputs.tf +7 -0
  15. package/src/generators/terraform-module/files/simple-module/variables.tf +7 -0
  16. package/src/generators/terraform-module/files/stateful-module/README.md__tmpl__ +20 -0
  17. package/src/generators/terraform-module/files/stateful-module/__ignoreFile__ +3 -0
  18. package/src/generators/terraform-module/files/stateful-module/backend.tf__tmpl__ +8 -0
  19. package/src/generators/terraform-module/files/stateful-module/main.tf__tmpl__ +16 -0
  20. package/src/generators/terraform-module/files/stateful-module/outputs.tf +7 -0
  21. package/src/generators/terraform-module/files/stateful-module/provider.tf +16 -0
  22. package/src/generators/terraform-module/files/stateful-module/variables.tf +7 -0
  23. package/src/generators/terraform-module/schema.d.ts +14 -0
  24. package/src/generators/terraform-module/schema.json +37 -0
  25. package/src/generators/terraform-module/terraform-module.d.ts +4 -0
  26. package/src/generators/terraform-module/terraform-module.js +48 -0
  27. package/src/generators/terraform-module/terraform-module.js.map +1 -0
package/README.md CHANGED
@@ -1,11 +1,484 @@
1
1
  # nx-terraform
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ An [Nx](https://nx.dev) plugin for managing Terraform projects within an Nx monorepo. This plugin provides generators, automatic project discovery, and inferred tasks for Terraform infrastructure-as-code projects.
4
4
 
5
- ## Building
5
+ ## Overview
6
6
 
7
- Run `nx build nx-terraform` to build the library.
7
+ The `nx-terraform` plugin enables you to manage Terraform projects alongside your other code in an Nx monorepo. It provides:
8
8
 
9
- ## Running unit tests
9
+ - **Automatic Project Discovery**: Automatically discovers Terraform projects based on `main.tf` files
10
+ - **Inferred Tasks**: Automatically creates Terraform targets (init, plan, apply, destroy, validate, fmt, output)
11
+ - **Generators**: Scaffold Terraform backend projects and modules
12
+ - **Target Dependencies**: Smart dependency management between Terraform projects
13
+ - **Caching**: Intelligent caching for safe operations (fmt, validate)
10
14
 
11
- Run `nx test nx-terraform` to execute the unit tests via [Jest](https://jestjs.io).
15
+ ## Installation
16
+
17
+ ### For New Workspaces
18
+
19
+ Create a new workspace with Terraform support:
20
+
21
+ ```bash
22
+ npx create-nx-terraform-app my-workspace
23
+ ```
24
+
25
+ This creates a new Nx workspace with the `nx-terraform` plugin already configured.
26
+
27
+ ### For Existing Workspaces
28
+
29
+ Install the plugin in your existing Nx workspace:
30
+
31
+ ```bash
32
+ nx add nx-terraform
33
+ ```
34
+
35
+ This will:
36
+ - Add `nx-terraform` as a dependency
37
+ - Register the plugin in `nx.json`
38
+ - Enable automatic Terraform project discovery
39
+
40
+ ## Quick Start
41
+
42
+ 1. **Initialize the plugin** (if not using `create-nx-terraform-app`):
43
+ ```bash
44
+ nx g @nx-terraform/plugin:init
45
+ ```
46
+
47
+ 2. **Create a Terraform backend project**:
48
+ ```bash
49
+ nx g @nx-terraform/plugin:terraform-backend my-backend --backendType=aws-s3
50
+ ```
51
+
52
+ 3. **Apply the backend** (to create the state storage infrastructure):
53
+ ```bash
54
+ nx run my-backend:terraform-apply
55
+ ```
56
+
57
+ 4. **Create a Terraform module**:
58
+ ```bash
59
+ nx g @nx-terraform/plugin:terraform-module my-infra \
60
+ --backendProject=my-backend \
61
+ --backendType=aws-s3
62
+ ```
63
+
64
+ 5. **Use Terraform targets**:
65
+ ```bash
66
+ nx run my-infra:terraform-init
67
+ nx run my-infra:terraform-plan
68
+ nx run my-infra:terraform-apply
69
+ ```
70
+
71
+ ## Features
72
+
73
+ ### Automatic Project Discovery
74
+
75
+ The plugin automatically discovers Terraform projects by looking for `main.tf` files in your workspace. Projects are detected based on:
76
+
77
+ - **Presence of `main.tf`**: Required for discovery
78
+ - **Project configuration**: Must have `project.json` in the same directory
79
+ - **Project type**: Determined by `projectType` and `metadata` in `project.json`
80
+
81
+ ### Inferred Tasks
82
+
83
+ The plugin automatically creates these targets for each Terraform project:
84
+
85
+ - **terraform-init**: Initialize Terraform workspace
86
+ - **terraform-plan**: Create execution plan
87
+ - **terraform-apply**: Apply changes to infrastructure
88
+ - **terraform-destroy**: Destroy infrastructure
89
+ - **terraform-validate**: Validate Terraform configuration
90
+ - **terraform-fmt**: Format Terraform code
91
+ - **terraform-output**: Show Terraform outputs
92
+
93
+ ### Project Types
94
+
95
+ The plugin supports three types of Terraform projects:
96
+
97
+ 1. **Backend Projects** (`application` without `backendProject` metadata):
98
+ - Manage remote state infrastructure (e.g., S3 bucket, DynamoDB table)
99
+ - Generate `backend.config` files
100
+ - Full target set with caching enabled
101
+
102
+ 2. **Stateful Projects** (`application` with `backendProject` metadata):
103
+ - Infrastructure projects that use remote state
104
+ - Reference backend project via `metadata.backendProject`
105
+ - Full target set (no caching for init/plan due to state)
106
+
107
+ 3. **Module Projects** (`library`):
108
+ - Reusable Terraform modules
109
+ - No state management
110
+ - Stub targets (modules don't have state)
111
+
112
+ ### Smart Dependencies
113
+
114
+ The plugin automatically manages dependencies between Terraform projects:
115
+
116
+ - **Backend first**: Stateful projects depend on backend projects being applied
117
+ - **Init before operations**: Plan, apply, and validate require init
118
+ - **Plan before apply**: Apply depends on plan
119
+
120
+ ### Caching
121
+
122
+ Intelligent caching for safe operations:
123
+
124
+ - **Cached**: `terraform-fmt`, `terraform-validate` (when safe)
125
+ - **Non-cached**: `terraform-init`, `terraform-plan`, `terraform-apply` (state-dependent)
126
+
127
+ ## Generators
128
+
129
+ The plugin provides four generators:
130
+
131
+ ### `init` Generator
132
+
133
+ Registers the `nx-terraform` plugin in your workspace.
134
+
135
+ ```bash
136
+ nx g @nx-terraform/plugin:init
137
+ ```
138
+
139
+ **Documentation**: [`packages/nx-terraform/src/generators/init/README.md`](./src/generators/init/README.md)
140
+
141
+ ### `terraform-backend` Generator
142
+
143
+ Creates a Terraform backend project for managing remote state.
144
+
145
+ ```bash
146
+ nx g @nx-terraform/plugin:terraform-backend my-backend --backendType=aws-s3
147
+ ```
148
+
149
+ **Options**:
150
+ - `name`: Backend project name (required)
151
+ - `backendType`: 'aws-s3' or 'local' (required)
152
+ - `bucketNamePrefix`: Prefix for S3 bucket name (optional, AWS S3 only)
153
+
154
+ **Documentation**: [`packages/nx-terraform/src/generators/terraform-backend/README.md`](./src/generators/terraform-backend/README.md)
155
+
156
+ ### `terraform-module` Generator
157
+
158
+ Creates Terraform modules (simple library or stateful application).
159
+
160
+ ```bash
161
+ # Simple module (library)
162
+ nx g @nx-terraform/plugin:terraform-module my-module --backendType=local
163
+
164
+ # Stateful module (application)
165
+ nx g @nx-terraform/plugin:terraform-module my-infra \
166
+ --backendProject=my-backend \
167
+ --backendType=aws-s3
168
+ ```
169
+
170
+ **Options**:
171
+ - `name`: Module name (required)
172
+ - `backendType`: 'aws-s3' or 'local' (required)
173
+ - `backendProject`: Backend project name (optional, creates stateful module if provided)
174
+
175
+ **Documentation**: [`packages/nx-terraform/src/generators/terraform-module/README.md`](./src/generators/terraform-module/README.md)
176
+
177
+ ### `preset` Generator
178
+
179
+ Initializes workspace with Terraform setup (used by `create-nx-terraform-app`).
180
+
181
+ ```bash
182
+ nx g @nx-terraform/plugin:preset --projectName=terraform-setup --backendType=aws-s3
183
+ ```
184
+
185
+ **Documentation**: [`packages/nx-terraform/src/generators/preset/README.md`](./src/generators/preset/README.md)
186
+
187
+ ## Targets
188
+
189
+ Each Terraform project automatically gets these targets:
190
+
191
+ ### terraform-init
192
+
193
+ Initializes Terraform workspace.
194
+
195
+ ```bash
196
+ nx run my-project:terraform-init
197
+ ```
198
+
199
+ **Dependencies**: `^terraform-apply` (backend must be applied first)
200
+
201
+ ### terraform-plan
202
+
203
+ Creates an execution plan.
204
+
205
+ ```bash
206
+ nx run my-project:terraform-plan
207
+ ```
208
+
209
+ **Dependencies**: `terraform-init`
210
+
211
+ **Configurations**: Supports `dev` and `prod` configurations via `-var-file` arguments
212
+
213
+ ### terraform-apply
214
+
215
+ Applies changes to infrastructure.
216
+
217
+ ```bash
218
+ nx run my-project:terraform-apply
219
+ ```
220
+
221
+ **Dependencies**: `terraform-plan`
222
+
223
+ **Configurations**: Supports `dev` and `prod` configurations
224
+
225
+ ### terraform-destroy
226
+
227
+ Destroys infrastructure.
228
+
229
+ ```bash
230
+ nx run my-project:terraform-destroy
231
+ ```
232
+
233
+ **Dependencies**: `terraform-init`
234
+
235
+ ### terraform-validate
236
+
237
+ Validates Terraform configuration.
238
+
239
+ ```bash
240
+ nx run my-project:terraform-validate
241
+ ```
242
+
243
+ **Dependencies**: `terraform-init`
244
+
245
+ **Caching**: Enabled (safe operation)
246
+
247
+ ### terraform-fmt
248
+
249
+ Formats Terraform code.
250
+
251
+ ```bash
252
+ nx run my-project:terraform-fmt
253
+ ```
254
+
255
+ **Caching**: Enabled (safe operation)
256
+
257
+ ### terraform-output
258
+
259
+ Shows Terraform outputs.
260
+
261
+ ```bash
262
+ nx run my-project:terraform-output
263
+ ```
264
+
265
+ **Dependencies**: `terraform-init`
266
+
267
+ ## Project Structure
268
+
269
+ Terraform projects should follow this structure:
270
+
271
+ ```
272
+ packages/
273
+ └── my-terraform-project/
274
+ ├── project.json # Nx project configuration
275
+ ├── main.tf # Required for discovery
276
+ ├── backend.tf # Backend configuration (stateful projects)
277
+ ├── provider.tf # Provider requirements
278
+ ├── variables.tf # Input variables
279
+ ├── outputs.tf # Output values
280
+ ├── tfvars/
281
+ │ ├── dev.tfvars # Dev environment variables
282
+ │ └── prod.tfvars # Prod environment variables
283
+ └── backend.config # Generated backend config (backend projects)
284
+ ```
285
+
286
+ ### Project Configuration
287
+
288
+ For **stateful projects**, configure `metadata.backendProject`:
289
+
290
+ ```json
291
+ {
292
+ "root": "packages/my-infra",
293
+ "projectType": "application",
294
+ "metadata": {
295
+ "backendProject": "my-backend"
296
+ }
297
+ }
298
+ ```
299
+
300
+ ## Configuration
301
+
302
+ ### Backend Types
303
+
304
+ The plugin supports two backend types:
305
+
306
+ #### AWS S3 Backend
307
+
308
+ Production-ready remote state storage using AWS S3:
309
+
310
+ ```bash
311
+ nx g @nx-terraform/plugin:terraform-backend my-backend --backendType=aws-s3
312
+ ```
313
+
314
+ **Features**:
315
+ - Versioning enabled
316
+ - Object lock for state protection
317
+ - Dynamic bucket naming
318
+ - Region detection
319
+
320
+ #### Local Backend
321
+
322
+ Local state files for development:
323
+
324
+ ```bash
325
+ nx g @nx-terraform/plugin:terraform-backend my-backend --backendType=local
326
+ ```
327
+
328
+ **Use Cases**:
329
+ - Development and testing
330
+ - Single-user scenarios
331
+ - Ephemeral environments
332
+
333
+ ### Environment Variables
334
+
335
+ Terraform projects support environment-specific variables via `tfvars` files:
336
+
337
+ - `tfvars/dev.tfvars` - Development environment
338
+ - `tfvars/prod.tfvars` - Production environment
339
+
340
+ Targets automatically use the appropriate `-var-file` argument based on configuration:
341
+
342
+ ```bash
343
+ # Uses tfvars/dev.tfvars
344
+ nx run my-project:terraform-plan --configuration=dev
345
+
346
+ # Uses tfvars/prod.tfvars
347
+ nx run my-project:terraform-plan --configuration=prod
348
+ ```
349
+
350
+ ## Examples
351
+
352
+ ### Complete Workflow
353
+
354
+ ```bash
355
+ # 1. Create new workspace (creates terraform-setup backend and terraform-infra module)
356
+ npx create-nx-terraform-app my-workspace
357
+
358
+ # 2. Apply backend (from workspace creation)
359
+ cd my-workspace
360
+ nx run terraform-setup:terraform-apply
361
+
362
+ # 3. Plan and apply infrastructure module (already created by preset)
363
+ nx run terraform-infra:terraform-init
364
+ nx run terraform-infra:terraform-plan --configuration=dev
365
+ nx run terraform-infra:terraform-apply --configuration=dev
366
+
367
+ # 4. (Optional) Create additional infrastructure modules
368
+ nx g @nx-terraform/plugin:terraform-module web-infra \
369
+ --backendProject=terraform-setup \
370
+ --backendType=aws-s3
371
+ ```
372
+
373
+ ### Multiple Environments
374
+
375
+ ```bash
376
+ # Create dev environment
377
+ nx g @nx-terraform/plugin:terraform-module dev-infra \
378
+ --backendProject=terraform-setup \
379
+ --backendType=aws-s3
380
+
381
+ # Create prod environment
382
+ nx g @nx-terraform/plugin:terraform-module prod-infra \
383
+ --backendProject=terraform-setup \
384
+ --backendType=aws-s3
385
+
386
+ # Deploy to dev
387
+ nx run dev-infra:terraform-plan --configuration=dev
388
+ nx run dev-infra:terraform-apply --configuration=dev
389
+
390
+ # Deploy to prod
391
+ nx run prod-infra:terraform-plan --configuration=prod
392
+ nx run prod-infra:terraform-apply --configuration=prod
393
+ ```
394
+
395
+ ### Reusable Modules
396
+
397
+ ```bash
398
+ # Create reusable networking module
399
+ nx g @nx-terraform/plugin:terraform-module networking --backendType=local
400
+
401
+ # Use in other projects via module reference:
402
+ # module "networking" {
403
+ # source = "../../packages/networking"
404
+ # ...
405
+ # }
406
+ ```
407
+
408
+ ## Project Discovery
409
+
410
+ The plugin automatically discovers Terraform projects using:
411
+
412
+ - **Pattern**: `**/main.tf` (looks for `main.tf` files recursively)
413
+ - **Requirement**: Projects must have `project.json` in the same directory
414
+ - **Type Detection**: Based on `projectType` and `metadata.backendProject`
415
+
416
+ Projects are discovered on workspace load, so new projects are automatically available without restarting Nx.
417
+
418
+ ## Caching Strategy
419
+
420
+ Caching behavior varies by project type:
421
+
422
+ ### Backend Projects (caching enabled)
423
+
424
+ - **terraform-init**: Cached (backend projects don't depend on external state)
425
+ - **terraform-plan**: Cached
426
+ - **terraform-apply**: Cached
427
+ - **terraform-fmt**: Cached (code formatting is deterministic)
428
+ - **terraform-validate**: Cached (validation results can be cached when inputs haven't changed)
429
+ - **terraform-destroy**: Not cached
430
+ - **terraform-output**: Not cached
431
+
432
+ ### Stateful Projects (caching disabled for state-dependent operations)
433
+
434
+ - **terraform-init**: Not cached (state-dependent, requires backend access)
435
+ - **terraform-plan**: Not cached (state-dependent, shows infrastructure changes)
436
+ - **terraform-apply**: Not cached (state-dependent, modifies infrastructure)
437
+ - **terraform-fmt**: Cached (code formatting is deterministic)
438
+ - **terraform-validate**: Cached (validation results can be cached when inputs haven't changed)
439
+ - **terraform-destroy**: Not cached
440
+ - **terraform-output**: Not cached
441
+
442
+ ### Module Projects (library)
443
+
444
+ - **terraform-fmt**: Cached
445
+ - **terraform-validate**: Cached
446
+ - **terraform-apply**: Not cached (though modules typically don't use apply)
447
+ - All other targets are stubs (cached but no-op)
448
+
449
+ Cache inputs include:
450
+ - All `.tf` and `.tfvars` files
451
+ - Relevant environment variables
452
+ - Terraform version
453
+ - Provider and backend configuration files
454
+
455
+ ## Compatibility
456
+
457
+ - **Nx**: v22.0.2 or compatible
458
+ - **Terraform**: Any version (uses Terraform CLI)
459
+ - **Node.js**: v18+ recommended
460
+
461
+ ## Documentation
462
+
463
+ Comprehensive documentation is available for each component:
464
+
465
+ - **Generators**:
466
+ - [`init`](./src/generators/init/README.md) - Plugin initialization
467
+ - [`terraform-backend`](./src/generators/terraform-backend/README.md) - Backend project creation
468
+ - [`terraform-module`](./src/generators/terraform-module/README.md) - Module creation
469
+ - [`preset`](./src/generators/preset/README.md) - Workspace preset
470
+
471
+ - **Workspace Creation**:
472
+ - [`create-nx-terraform-app`](../create-nx-terraform-app/README.md) - CLI tool for workspace creation
473
+
474
+ ## Contributing
475
+
476
+ Contributions are welcome! Please see the repository for contribution guidelines.
477
+
478
+ ## License
479
+
480
+ MIT
481
+
482
+ ## Repository
483
+
484
+ https://github.com/alexpialetski/nx-terraform
package/generators.json CHANGED
@@ -15,6 +15,11 @@
15
15
  "factory": "./src/generators/init/init",
16
16
  "schema": "./src/generators/init/schema.json",
17
17
  "description": "init generator"
18
+ },
19
+ "terraform-module": {
20
+ "factory": "./src/generators/terraform-module/terraform-module",
21
+ "schema": "./src/generators/terraform-module/schema.json",
22
+ "description": "terraform-module generator"
18
23
  }
19
24
  }
20
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nx-terraform",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -0,0 +1,90 @@
1
+ # Init Generator
2
+
3
+ ## Overview
4
+
5
+ The `init` generator adds the `nx-terraform` plugin to your Nx workspace configuration. This is the first step required when adding Terraform support to an existing Nx workspace. It registers the plugin in `nx.json`, enabling automatic discovery of Terraform projects and inferred task generation.
6
+
7
+ ## Usage
8
+
9
+ ```bash
10
+ nx g @nx-terraform/plugin:init
11
+ ```
12
+
13
+ ## Options
14
+
15
+ This generator has no options or parameters. It simply registers the plugin in your workspace configuration.
16
+
17
+ ## What It Does
18
+
19
+ - Reads the current `nx.json` configuration
20
+ - Checks if the `nx-terraform` plugin is already registered
21
+ - Adds the plugin to `nx.json.plugins` array if not present:
22
+ ```json
23
+ {
24
+ "plugin": "nx-terraform",
25
+ "options": {}
26
+ }
27
+ ```
28
+ - Formats modified files
29
+
30
+ ## Implementation Details
31
+
32
+ **Key Functions:**
33
+ - `readNxJson(tree)`: Reads current workspace configuration
34
+ - `updateNxJson(tree, nxJson)`: Updates nx.json with plugin registration
35
+ - `formatFiles(tree)`: Formats generated files
36
+
37
+ **Behavior:**
38
+ - **Idempotent**: Safely re-runnable - checks if plugin already exists before adding
39
+ - **Non-destructive**: Only adds plugin entry, doesn't modify existing configuration
40
+ - No file generation: Only modifies `nx.json`
41
+
42
+ **Code Location:**
43
+ - Implementation: `packages/nx-terraform/src/generators/init/init.ts`
44
+ - Schema: `packages/nx-terraform/src/generators/init/schema.json`
45
+
46
+ ## Examples
47
+
48
+ ### Adding Terraform Support to Existing Workspace
49
+
50
+ ```bash
51
+ # Add nx-terraform plugin to workspace
52
+ nx g @nx-terraform/plugin:init
53
+
54
+ # Now you can create Terraform projects
55
+ nx g @nx-terraform/plugin:terraform-backend my-backend --backendType=local
56
+ ```
57
+
58
+ ### Verifying Plugin Registration
59
+
60
+ After running the generator, check `nx.json`:
61
+
62
+ ```json
63
+ {
64
+ "plugins": [
65
+ {
66
+ "plugin": "nx-terraform",
67
+ "options": {}
68
+ }
69
+ ]
70
+ }
71
+ ```
72
+
73
+ ## When to Use
74
+
75
+ - **Adding Terraform support** to an existing Nx workspace
76
+ - **Manual setup** when you don't want to use the preset generator
77
+ - **Incremental adoption** of Terraform in a workspace that already has other projects
78
+
79
+ ## Related Generators
80
+
81
+ - **preset**: Automatically runs `init` as part of complete workspace setup
82
+ - **terraform-backend**: Creates backend projects (requires init to be run first)
83
+ - **terraform-module**: Creates module projects (requires init to be run first)
84
+
85
+ ## Notes
86
+
87
+ - This generator is automatically invoked by the `preset` generator
88
+ - The plugin registration enables automatic discovery of Terraform projects via `**/main.tf` pattern
89
+ - Once registered, the plugin will automatically create Terraform targets for discovered projects
90
+