terraform-init Target
Initializes a Terraform workspace, downloading required providers and configuring the backend connection.
Usage
nx run my-project:terraform-init
Description
The terraform-init target initializes a Terraform workspace by:
- Downloading required provider plugins
- Configuring the backend connection
- Preparing the workspace for Terraform operations
Dependencies
Project Dependencies
- Backend Projects: No dependencies (backend projects manage their own state)
- Stateful Projects: Depends on
^terraform-applyfrom the backend project (backend must be applied first)
Target Dependencies
- Automatically runs
sync-terraform-metadatagenerator before initialization to ensure project metadata is up to date
Behavior
Backend Projects
- Initializes with local or S3 backend configuration
- Downloads providers specified in
provider.tf - Prepares workspace for backend infrastructure deployment
Stateful Projects
- Reads
backend.configfrom the referenced backend project - Configures backend connection using the backend project's configuration
- Downloads providers specified in
provider.tf - Prepares workspace for infrastructure deployment
Module Projects
- Initializes for validation purposes (modules don't have state)
- Downloads providers for validation
- Stub operation (modules are included in other projects)
Caching
- Backend Projects: ✅ Cached (backend projects don't depend on external state)
- Stateful Projects: ❌ Not cached (state-dependent, requires backend access)
- Module Projects: N/A (stub operation)
Examples
Initialize Backend Project
nx run terraform-setup:terraform-init
Initialize Stateful Project
# Backend must be applied first
nx run terraform-setup:terraform-apply
# Then initialize infrastructure project
nx run my-infra:terraform-init
Initialize with Reconfiguration
If you need to reconfigure the backend:
nx run my-infra:terraform-init -- -reconfigure
Common Use Cases
- First-time setup: Initialize a new Terraform project
- After provider changes: Re-initialize when provider versions change
- Backend changes: Re-initialize when backend configuration changes
- Workspace reset: Re-initialize to reset workspace state
Related Targets
- terraform-plan: Requires
terraform-initto run first - terraform-apply: Requires
terraform-initto run first - terraform-validate: Requires
terraform-initto run first
Notes
- The target automatically runs
sync-terraform-metadatabefore initialization - For stateful projects, the backend project must be applied before initialization
- Provider downloads are cached locally by Terraform
- Backend configuration is read from
backend.configfor stateful projects