Using Terraform Data Sources Effectively
Learn how to use Terraform data sources to query existing resources, look up AMIs, reference remote state, and build dynamic configurations. Complete.
Software Development
Install and configure the HashiCorp Terraform VSCode extension. Enable syntax highlighting, autocomplete, format on save, validation
Ctrl+Shift+X (Windows/Linux) or Cmd+Shift+X (macOS)Or install from the command line:
code --install-extension hashicorp.terraform
The extension ID is hashicorp.terraform. It's the official extension maintained by HashiCorp.
| Feature | Description |
|---|---|
| Syntax highlighting | Color-coded .tf and .tfvars files |
| Autocomplete | Resource types, attributes, functions, variables |
| Go to definition | Ctrl+Click on a variable or module to jump to its definition |
| Hover documentation | Hover over a resource type to see docs |
| Format on save | Auto-runs terraform fmt when you save |
| Validation | Red squiggles for syntax errors |
| Code lens | Reference counts above variables and outputs |
| Module support | Autocomplete works inside modules |
The most useful setting. Add to your VSCode settings.json:
{
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
}
}Now every time you save a .tf file, it automatically runs terraform fmt — consistent formatting without thinking about it.
Ctrl+Shift+P → "Preferences: Open User Settings (JSON)"{} icon (top right)The extension validates your Terraform code in real-time. Make sure validation is enabled:
{
"terraform.validation.enableEnhancedValidation": true
}This catches errors as you type — before you run terraform plan.
{
// Format .tf files on save
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
"[terraform-vars]": {
"editor.defaultFormatter": "hashicorp.terraform",
"editor.formatOnSave": true
},
// Enable enhanced validation
"terraform.validation.enableEnhancedValidation": true,
// Enable code lens (reference counts)
"terraform.codelens.referenceCount": true,
// Path to terraform binary (if not in PATH)
// "terraform.languageServer.path": "/usr/local/bin/terraform",
// Enable experiment features
"terraform.experimentalFeatures.validateOnSave": true,
"terraform.experimentalFeatures.prefillRequiredFields": true
}| Shortcut | Action |
|---|---|
Ctrl+Space | Trigger autocomplete |
Ctrl+Click | Go to definition |
F12 | Go to definition |
Alt+F12 | Peek definition (inline) |
Shift+F12 | Find all references |
Ctrl+Shift+F | Format document |
Ctrl+` | Open integrated terminal |
Ctrl+Shift+P | Command palette |
Run Terraform commands directly in VSCode:
Ctrl+`terraform init
terraform plan
terraform apply
Create .vscode/tasks.json for one-click Terraform commands:
{
"version": "2.0.0",
"tasks": [
{
"label": "Terraform Init",
"type": "shell",
"command": "terraform init",
"group": "build"
},
{
"label": "Terraform Plan",
"type": "shell",
"command": "terraform plan",
"group": "build"
},
{
"label": "Terraform Apply",
"type": "shell",
"command": "terraform apply -auto-approve",
"group": "build"
},
{
"label": "Terraform Validate",
"type": "shell",
"command": "terraform validate",
"group": "test"
}
]
}Run tasks: Ctrl+Shift+P → "Tasks: Run Task" → select task.
| Extension | ID | Purpose |
|---|---|---|
| Terraform doc snippets | run-at-scale.terraform-doc-snippets | Snippet templates |
| TFLint | megnitas.tflint | Linting integration |
| GitLens | eamodio.gitlens | Git blame and history |
| YAML | redhat.vscode-yaml | For Ansible/CI files |
| Remote - SSH | ms-vscode-remote.remote-ssh | Edit files on remote servers |
| Error Lens | usernamehw.errorlens | Inline error highlighting |
Install all at once:
code --install-extension hashicorp.terraform
code --install-extension run-at-scale.terraform-doc-snippets
code --install-extension eamodio.gitlens
code --install-extension usernamehw.errorlensThe extension provides autocomplete for:
Resource types:
resource "aws_ # Shows: aws_instance, aws_s3_bucket, aws_vpc, ...Resource attributes:
resource "aws_instance" "web" {
inst # Shows: instance_type
ami # Shows: ami
}Built-in functions:
locals {
result = leng # Shows: length(), lower(), lookup(), ...
}Variable references:
instance_type = var. # Shows all declared variablesModule outputs:
subnet_id = module.vpc. # Shows all outputs from the vpc moduleterraform init in the project directory firstCtrl+Shift+P → "Terraform: Enable Language Server"hashicorp.terraform is set as the default formatter (see settings above)terraform binary is in your PATH: which terraformCtrl+Shift+P → "Format Document"If you have the older mauve.terraform extension installed, remove it — it conflicts with the official HashiCorp extension.
code --uninstall-extension mauve.terraformThe language server can be CPU-intensive on large projects. If needed:
{
"terraform.languageServer.indexing.ignoreDirectoryNames": [
".terraform",
"node_modules"
]
}Learn by doing with interactive courses on CopyPasteLearn:
Install the HashiCorp Terraform extension (hashicorp.terraform), enable format on save and enhanced validation, and you have a production-ready Terraform IDE. Autocomplete covers resource types, attributes, functions, and module outputs. Add TFLint and GitLens for a complete development environment.
Learn how to use Terraform data sources to query existing resources, look up AMIs, reference remote state, and build dynamic configurations. Complete.
Step-by-step guide to terraform import. Import existing AWS, Azure, and GCP resources into Terraform state. Includes import blocks (Terraform 1.5+)
Enable Terraform debug mode with TF_LOG=DEBUG, save logs to file with TF_LOG_PATH, and troubleshoot terraform plan/apply errors.
Master Terraform version constraints for Terraform core and providers. Covers operators, lock files, required_version, required_providers, and upgrade...