Nx plugin for deploying your resources with Terraform.
- Manage infrastructure as code with Terraform
- Full lifecycle management (init, plan, apply, destroy)
- Workspace management for multiple environments
- Variable and secret management
- Backend configuration support
- State migration and reconfiguration
- Format validation and testing
- Provider lock file management
- CI mode for automated deployments
- Terraform CLI installed and available in PATH
npm install -D @nx-extend/terraform
nx g @nx-extend/terraform:init| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name |
string |
true |
- | Terraform project name |
directory |
string |
false |
- | A directory where the project is placed, based on the sourceRoot |
tags |
string |
false |
- | Comma separated list for tags |
Initialize Terraform working directory:
nx run <terraform-project-name>:initializeGenerate and show an execution plan:
nx run <terraform-project-name>:planApply the changes required to reach the desired state:
nx run <terraform-project-name>:applyDestroy Terraform-managed infrastructure:
nx run <terraform-project-name>:destroyValidate Terraform configuration files:
nx run <terraform-project-name>:validateRun Terraform tests:
nx run <terraform-project-name>:testManage Terraform workspaces:
nx run <terraform-project-name>:workspaceManage provider versions and lock files:
nx run <terraform-project-name>:providersFormat Terraform configuration files:
nx run <terraform-project-name>:fmt| Name | Type | Default | Description | Supported Commands |
|---|---|---|---|---|
ciMode |
boolean |
false |
Enables CI mode (sets TF_IN_AUTOMATION=true and TF_INPUT=0) |
All |
varFile |
string |
- | Path to a variable file (passed as --var-file) |
plan, apply, test |
varString |
string |
- | Inline variables (passed as --var) |
plan, apply, test |
planFile |
string |
- | Path to output the plan file (e.g., tfplan) |
plan, apply |
autoApproval |
boolean |
false |
Skips interactive approval (passed as -auto-approve) |
apply, destroy |
workspace |
string |
- | Name of the workspace. Required for new, select, and delete actions |
workspace |
workspaceAction |
string |
select |
Action to perform on the workspace. Accepted values: select, new, delete, list |
workspace |
backendConfig |
array |
[] |
Backend configuration (e.g., [{ "key": "bucket", "name": "my-bucket" }]) |
init |
reconfigure |
boolean |
false |
Reconfigure the backend (passed as -reconfigure) |
init |
migrateState |
boolean |
false |
Migrate state during init (passed as -migrate-state) |
init |
upgrade |
boolean |
false |
Install the latest module and provider versions (passed as -upgrade) |
init |
formatWrite |
boolean |
false |
If true, updates files in place. If false, only checks formatting |
fmt |
lock |
boolean |
false |
Update the lock file (passed as lock) |
providers |
# Plan with a specific tfvars file
nx run my-project:plan --varFile=config/dev.tfvars
# Apply with inline variables
nx run my-project:apply --varString="region=us-east-1"# List all workspaces
nx run my-project:workspace --workspaceAction=list
# Create a new workspace named 'staging'
nx run my-project:workspace --workspaceAction=new --workspace=staging
# Select 'staging' workspace (default action is select)
nx run my-project:workspace --workspace=staging
# Delete 'staging' workspace
nx run my-project:workspace --workspaceAction=delete --workspace=staging# Run in CI mode with auto-approval
nx run my-project:apply --ciMode --autoApproval{
"initialize": {
"executor": "@nx-extend/terraform:init",
"options": {
"backendConfig": [
{ "key": "bucket", "value": "my-terraform-state" },
{ "key": "prefix", "value": "terraform/state" }
]
}
}
}