nx-terraform 0.7.2 → 0.8.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nx-terraform",
3
- "version": "0.7.2",
3
+ "version": "0.8.1",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
package/README.md DELETED
@@ -1,558 +0,0 @@
1
- # nx-terraform
2
-
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
-
5
- ## Overview
6
-
7
- The `nx-terraform` plugin enables you to manage Terraform projects alongside your other code in an Nx monorepo. It provides:
8
-
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)
14
-
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
-
37
- - Add `nx-terraform` as a dependency
38
- - Register the plugin in `nx.json`
39
- - Enable automatic Terraform project discovery
40
-
41
- ## Quick Start
42
-
43
- 1. **Initialize the plugin** (if not using `create-nx-terraform-app`):
44
-
45
- ```bash
46
- nx g nx-terraform:init
47
- ```
48
-
49
- 2. **Create a Terraform backend project**:
50
-
51
- ```bash
52
- nx g nx-terraform:terraform-backend my-backend --backendType=aws-s3
53
- ```
54
-
55
- 3. **Apply the backend** (to create the state storage infrastructure):
56
-
57
- ```bash
58
- nx run my-backend:terraform-apply
59
- ```
60
-
61
- 4. **Create a Terraform module**:
62
-
63
- ```bash
64
- nx g nx-terraform:terraform-module my-infra \
65
- --backendProject=my-backend
66
- ```
67
-
68
- 5. **Use Terraform targets**:
69
- ```bash
70
- nx run my-infra:terraform-init
71
- nx run my-infra:terraform-plan
72
- nx run my-infra:terraform-apply
73
- ```
74
-
75
- ## Features
76
-
77
- ### Automatic Project Discovery
78
-
79
- The plugin automatically discovers Terraform projects by looking for `main.tf` files in your workspace. Projects are detected based on:
80
-
81
- - **Presence of `main.tf`**: Required for discovery
82
- - **Project configuration**: Must have `project.json` in the same directory
83
- - **Project type**: Determined by `projectType` and `metadata` in `project.json`
84
-
85
- ### Inferred Tasks
86
-
87
- The plugin automatically creates these targets for each Terraform project:
88
-
89
- - **terraform-init**: Initialize Terraform workspace
90
- - **terraform-plan**: Create execution plan
91
- - **terraform-apply**: Apply changes to infrastructure
92
- - **terraform-destroy**: Destroy infrastructure
93
- - **terraform-validate**: Validate Terraform configuration
94
- - **terraform-fmt**: Format Terraform code
95
- - **terraform-output**: Show Terraform outputs
96
-
97
- ### Project Types
98
-
99
- The plugin supports three types of Terraform projects:
100
-
101
- 1. **Backend Projects** (`application` without `backendProject` metadata):
102
-
103
- - Manage remote state infrastructure (e.g., S3 bucket, DynamoDB table)
104
- - Generate `backend.config` files
105
- - Full target set with caching enabled
106
-
107
- 2. **Stateful Projects** (`application` with `backendProject` metadata):
108
-
109
- - Infrastructure projects that use remote state
110
- - Reference backend project via `metadata['nx-terraform'].backendProject`
111
- - Full target set (no caching for init/plan due to state)
112
-
113
- 3. **Module Projects** (`library`):
114
- - Reusable Terraform modules
115
- - No state management
116
- - Stub targets (modules don't have state)
117
-
118
- ### Smart Dependencies
119
-
120
- The plugin automatically manages dependencies between Terraform projects using the `createDependencies` API. This creates a project dependency graph that ensures proper execution order.
121
-
122
- #### Automatic Dependency Detection
123
-
124
- The plugin automatically detects and creates dependencies in two ways:
125
-
126
- 1. **Backend Project Dependencies**: Projects with `metadata['nx-terraform'].backendProject` in their `project.json` automatically depend on their backend project. This ensures backend projects are applied before stateful projects initialize.
127
-
128
- 2. **Module Reference Dependencies**: The plugin analyzes `.tf` files to detect module references using local paths (e.g., `source = "../../packages/networking"`). When a module block references another Terraform project, a dependency is automatically created.
129
-
130
- **How Module Detection Works:**
131
-
132
- - Scans `.tf` files for `module` blocks
133
- - Extracts `source` attributes that use local paths (`./` or `../`)
134
- - Matches the last path segment to project names in the workspace
135
- - Creates static dependencies from the referencing project to the referenced project
136
-
137
- **Example:**
138
-
139
- ```hcl
140
- # In packages/web-app/main.tf
141
- module "networking" {
142
- source = "../../packages/networking"
143
- # ... module configuration
144
- }
145
- ```
146
-
147
- This automatically creates a dependency: `web-app` → `networking`
148
-
149
- #### Target Dependencies
150
-
151
- In addition to project dependencies, targets have their own dependencies:
152
-
153
- - **Backend first**: `terraform-init` depends on `^terraform-apply` (backend must be applied first)
154
- - **Init before operations**: `terraform-plan`, `terraform-apply`, and `terraform-validate` require `terraform-init`
155
- - **Plan before apply**: `terraform-apply` depends on `terraform-plan`
156
-
157
- ### Caching
158
-
159
- Intelligent caching for safe operations:
160
-
161
- - **Cached**: `terraform-fmt`, `terraform-validate` (when safe)
162
- - **Non-cached**: `terraform-init`, `terraform-plan`, `terraform-apply` (state-dependent)
163
-
164
- ## Generators
165
-
166
- The plugin provides five generators:
167
-
168
- ### `init` Generator
169
-
170
- Registers the `nx-terraform` plugin in your workspace.
171
-
172
- ```bash
173
- nx g nx-terraform:init
174
- ```
175
-
176
- **Documentation**: [`packages/nx-terraform/src/generators/init/README.md`](./src/generators/init/README.md)
177
-
178
- ### `terraform-backend` Generator
179
-
180
- Creates a Terraform backend project for managing remote state.
181
-
182
- ```bash
183
- nx g nx-terraform:terraform-backend my-backend --backendType=aws-s3
184
- ```
185
-
186
- **Options**:
187
-
188
- - `name`: Backend project name (required)
189
- - `backendType`: 'aws-s3' or 'local' (required)
190
- - `bucketNamePrefix`: Prefix for S3 bucket name (optional, AWS S3 only)
191
-
192
- **Documentation**: [`packages/nx-terraform/src/generators/terraform-backend/README.md`](./src/generators/terraform-backend/README.md)
193
-
194
- ### `terraform-module` Generator
195
-
196
- Creates Terraform modules (simple library or stateful application).
197
-
198
- ```bash
199
- # Simple module (library)
200
- nx g nx-terraform:terraform-module my-module
201
-
202
- # Stateful module (application)
203
- nx g nx-terraform:terraform-module my-infra \
204
- --backendProject=my-backend
205
- ```
206
-
207
- **Options**:
208
-
209
- - `name`: Module name (required)
210
- - `backendProject`: Backend project name (optional, creates stateful module if provided). The backend type is automatically derived from the backend project's metadata.
211
-
212
- **Documentation**: [`packages/nx-terraform/src/generators/terraform-module/README.md`](./src/generators/terraform-module/README.md)
213
-
214
- ### `preset` Generator
215
-
216
- Initializes workspace with Terraform setup (used by `create-nx-terraform-app`).
217
-
218
- ```bash
219
- # With backend (creates backend + stateful module)
220
- nx g nx-terraform:preset --projectName=terraform-setup --backendType=aws-s3
221
-
222
- # Without backend (creates simple module only)
223
- nx g nx-terraform:preset --projectName=terraform-setup
224
- ```
225
-
226
- **Options**:
227
-
228
- - `projectName`: Workspace name (required)
229
- - `backendType`: 'aws-s3' or 'local' (optional, if not provided only creates simple module)
230
-
231
- **Documentation**: [`packages/nx-terraform/src/generators/preset/README.md`](./src/generators/preset/README.md)
232
-
233
- ### `sync-terraform-metadata` Generator
234
-
235
- Automatically syncs Terraform project metadata based on `.tf` file analysis.
236
-
237
- ```bash
238
- nx g nx-terraform:sync-terraform-metadata
239
- ```
240
-
241
- This generator scans all Terraform projects and updates `projectType` metadata based on the presence of backend blocks in `.tf` files. It also updates `provider.tf` files with module dependency metadata. It's designed to be run as a global sync generator.
242
-
243
- **Documentation**: [`packages/nx-terraform/src/generators/sync-terraform-metadata/README.md`](./src/generators/sync-terraform-metadata/README.md)
244
-
245
- ## Targets
246
-
247
- Each Terraform project automatically gets these targets:
248
-
249
- ### terraform-init
250
-
251
- Initializes Terraform workspace.
252
-
253
- ```bash
254
- nx run my-project:terraform-init
255
- ```
256
-
257
- **Dependencies**: `^terraform-apply` (backend must be applied first)
258
-
259
- **Sync Generators**: Automatically runs `sync-terraform-metadata` generator before initialization to ensure project metadata is up to date
260
-
261
- ### terraform-plan
262
-
263
- Creates an execution plan.
264
-
265
- ```bash
266
- nx run my-project:terraform-plan
267
- ```
268
-
269
- **Dependencies**: `terraform-init`
270
-
271
- **Configurations**: Supports `dev` and `prod` configurations via `-var-file` arguments
272
-
273
- ### terraform-apply
274
-
275
- Applies changes to infrastructure.
276
-
277
- ```bash
278
- nx run my-project:terraform-apply
279
- ```
280
-
281
- **Dependencies**: `terraform-plan`
282
-
283
- **Configurations**: Supports `dev` and `prod` configurations
284
-
285
- ### terraform-destroy
286
-
287
- Destroys infrastructure.
288
-
289
- ```bash
290
- nx run my-project:terraform-destroy
291
- ```
292
-
293
- **Dependencies**: `terraform-init`
294
-
295
- ### terraform-validate
296
-
297
- Validates Terraform configuration.
298
-
299
- ```bash
300
- nx run my-project:terraform-validate
301
- ```
302
-
303
- **Dependencies**: `terraform-init`
304
-
305
- **Caching**: Enabled (safe operation)
306
-
307
- ### terraform-fmt
308
-
309
- Formats Terraform code.
310
-
311
- ```bash
312
- nx run my-project:terraform-fmt
313
- ```
314
-
315
- **Caching**: Enabled (safe operation)
316
-
317
- ### terraform-output
318
-
319
- Shows Terraform outputs.
320
-
321
- ```bash
322
- nx run my-project:terraform-output
323
- ```
324
-
325
- **Dependencies**: `terraform-init`
326
-
327
- ## Project Structure
328
-
329
- Terraform projects should follow this structure:
330
-
331
- ```
332
- packages/
333
- └── my-terraform-project/
334
- ├── project.json # Nx project configuration
335
- ├── main.tf # Required for discovery
336
- ├── backend.tf # Backend configuration (stateful projects)
337
- ├── provider.tf # Provider requirements
338
- ├── variables.tf # Input variables
339
- ├── outputs.tf # Output values
340
- ├── tfvars/
341
- │ ├── dev.tfvars # Dev environment variables
342
- │ └── prod.tfvars # Prod environment variables
343
- └── backend.config # Generated backend config (backend projects)
344
- ```
345
-
346
- ### Project Configuration
347
-
348
- For **stateful projects**, configure `metadata['nx-terraform'].backendProject`:
349
-
350
- ```json
351
- {
352
- "root": "packages/my-infra",
353
- "projectType": "application",
354
- "metadata": {
355
- "nx-terraform": {
356
- "backendProject": "my-backend"
357
- }
358
- }
359
- }
360
- ```
361
-
362
- ## Configuration
363
-
364
- ### Backend Types
365
-
366
- The plugin supports two backend types:
367
-
368
- #### AWS S3 Backend
369
-
370
- Production-ready remote state storage using AWS S3:
371
-
372
- ```bash
373
- nx g nx-terraform:terraform-backend my-backend --backendType=aws-s3
374
- ```
375
-
376
- **Features**:
377
-
378
- - Versioning enabled
379
- - Object lock for state protection
380
- - Dynamic bucket naming
381
- - Region detection
382
-
383
- #### Local Backend
384
-
385
- Local state files for development:
386
-
387
- ```bash
388
- nx g nx-terraform:terraform-backend my-backend --backendType=local
389
- ```
390
-
391
- **Use Cases**:
392
-
393
- - Development and testing
394
- - Single-user scenarios
395
- - Ephemeral environments
396
-
397
- ### Environment Variables
398
-
399
- Terraform projects support environment-specific variables via `tfvars` files:
400
-
401
- - `tfvars/dev.tfvars` - Development environment
402
- - `tfvars/prod.tfvars` - Production environment
403
-
404
- Targets automatically use the appropriate `-var-file` argument based on configuration:
405
-
406
- ```bash
407
- # Uses tfvars/dev.tfvars
408
- nx run my-project:terraform-plan --configuration=dev
409
-
410
- # Uses tfvars/prod.tfvars
411
- nx run my-project:terraform-plan --configuration=prod
412
- ```
413
-
414
- ## Examples
415
-
416
- ### Complete Workflow
417
-
418
- ```bash
419
- # 1. Create new workspace (creates terraform-setup backend and terraform-infra module)
420
- npx create-nx-terraform-app my-workspace
421
-
422
- # 2. Apply backend (from workspace creation)
423
- cd my-workspace
424
- nx run terraform-setup:terraform-apply
425
-
426
- # 3. Plan and apply infrastructure module (already created by preset)
427
- nx run terraform-infra:terraform-init
428
- nx run terraform-infra:terraform-plan --configuration=dev
429
- nx run terraform-infra:terraform-apply --configuration=dev
430
-
431
- # 4. (Optional) Create additional infrastructure modules
432
- nx g nx-terraform:terraform-module web-infra \
433
- --backendProject=terraform-setup
434
- ```
435
-
436
- ### Multiple Environments
437
-
438
- ```bash
439
- # Create dev environment
440
- nx g nx-terraform:terraform-module dev-infra \
441
- --backendProject=terraform-setup
442
-
443
- # Create prod environment
444
- nx g nx-terraform:terraform-module prod-infra \
445
- --backendProject=terraform-setup
446
-
447
- # Deploy to dev
448
- nx run dev-infra:terraform-plan --configuration=dev
449
- nx run dev-infra:terraform-apply --configuration=dev
450
-
451
- # Deploy to prod
452
- nx run prod-infra:terraform-plan --configuration=prod
453
- nx run prod-infra:terraform-apply --configuration=prod
454
- ```
455
-
456
- ### Reusable Modules with Automatic Dependencies
457
-
458
- ```bash
459
- # Create reusable networking module
460
- nx g nx-terraform:terraform-module networking
461
-
462
- # Create web app that uses the networking module
463
- nx g nx-terraform:terraform-module web-app \
464
- --backendProject=terraform-setup
465
- ```
466
-
467
- Then reference the module in your Terraform code:
468
-
469
- ```hcl
470
- # In packages/web-app/main.tf
471
- module "networking" {
472
- source = "../../packages/networking"
473
- vpc_cidr = "10.0.0.0/16"
474
- }
475
- ```
476
-
477
- **Automatic Dependency Detection**: The plugin automatically detects this module reference and creates a project dependency (`web-app` → `networking`). When you run `nx graph`, you'll see the dependency visualized. This ensures `networking` is built/validated before `web-app`.
478
-
479
- ## Project Discovery
480
-
481
- The plugin automatically discovers Terraform projects using:
482
-
483
- - **Pattern**: `**/main.tf` (looks for `main.tf` files recursively)
484
- - **Requirement**: Projects must have `project.json` in the same directory
485
- - **Type Detection**: Based on `projectType` and `metadata['nx-terraform'].backendProject`
486
-
487
- Projects are discovered on workspace load, so new projects are automatically available without restarting Nx.
488
-
489
- ## Caching Strategy
490
-
491
- Caching behavior varies by project type:
492
-
493
- ### Backend Projects (caching enabled)
494
-
495
- - **terraform-init**: Cached (backend projects don't depend on external state)
496
- - **terraform-plan**: Cached
497
- - **terraform-apply**: Cached
498
- - **terraform-fmt**: Cached (code formatting is deterministic)
499
- - **terraform-validate**: Cached (validation results can be cached when inputs haven't changed)
500
- - **terraform-destroy**: Not cached
501
- - **terraform-output**: Not cached
502
-
503
- ### Stateful Projects (caching disabled for state-dependent operations)
504
-
505
- - **terraform-init**: Not cached (state-dependent, requires backend access)
506
- - **terraform-plan**: Not cached (state-dependent, shows infrastructure changes)
507
- - **terraform-apply**: Not cached (state-dependent, modifies infrastructure)
508
- - **terraform-fmt**: Cached (code formatting is deterministic)
509
- - **terraform-validate**: Cached (validation results can be cached when inputs haven't changed)
510
- - **terraform-destroy**: Not cached
511
- - **terraform-output**: Not cached
512
-
513
- ### Module Projects (library)
514
-
515
- - **terraform-fmt**: Cached
516
- - **terraform-validate**: Cached
517
- - **terraform-apply**: Not cached (though modules typically don't use apply)
518
- - All other targets are stubs (cached but no-op)
519
-
520
- Cache inputs include:
521
-
522
- - All `.tf` and `.tfvars` files
523
- - Relevant environment variables
524
- - Terraform version
525
- - Provider and backend configuration files
526
-
527
- ## Compatibility
528
-
529
- - **Nx**: v22.0.2 or compatible
530
- - **Terraform**: Any version (uses Terraform CLI)
531
- - **Node.js**: v18+ recommended
532
-
533
- ## Documentation
534
-
535
- Comprehensive documentation is available for each component:
536
-
537
- - **Generators**:
538
-
539
- - [`init`](./src/generators/init/README.md) - Plugin initialization
540
- - [`terraform-backend`](./src/generators/terraform-backend/README.md) - Backend project creation
541
- - [`terraform-module`](./src/generators/terraform-module/README.md) - Module creation
542
- - [`preset`](./src/generators/preset/README.md) - Workspace preset
543
- - [`sync-terraform-metadata`](./src/generators/sync-terraform-metadata/README.md) - Metadata synchronization
544
-
545
- - **Workspace Creation**:
546
- - [`create-nx-terraform-app`](../create-nx-terraform-app/README.md) - CLI tool for workspace creation
547
-
548
- ## Contributing
549
-
550
- Contributions are welcome! Please see the repository for contribution guidelines.
551
-
552
- ## License
553
-
554
- MIT
555
-
556
- ## Repository
557
-
558
- https://github.com/alexpialetski/nx-terraform
@@ -1,90 +0,0 @@
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: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:init
53
-
54
- # Now you can create Terraform projects
55
- nx g nx-terraform: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
-