Listen to this Post
When managing cloud infrastructure with Terraform, you may need to provide detailed reports of deployed resourcesāwhether for documentation, audits, or management requests. Instead of manually collecting data from cloud portals, Terraformās `output` feature allows you to export structured data directly to a CSV or formatted Excel file.
How to Export Terraform Output to CSV/Excel
1. Define Outputs in Terraform Configuration
Add an `output` block in your Terraform file (main.tf
or outputs.tf
) to capture resource details:
output "resource_details" { value = { Resource_Group = azurerm_resource_group.rg.name Location = azurerm_resource_group.rg.location VNet_Name = azurerm_virtual_network.vnet.name Subnet_Name = azurerm_subnet.subnet.name Public_IP = azurerm_public_ip.pip.ip_address VM_Hostname = azurerm_linux_virtual_machine.vm.name } }
2. Generate Output in CLI
After applying Terraform, run:
terraform output -json > deployment_details.json
3. Convert JSON to CSV
Use `jq` (Linux/macOS) or PowerShell (Windows) to format the output:
Linux/macOS:
jq -r 'to_entries[] | [.key, .value] | @csv' deployment_details.json > resources.csv
Windows (PowerShell):
Get-Content deployment_details.json | ConvertFrom-Json | Export-Csv -Path resources.csv -NoTypeInformation
4. Format in Excel
Open `resources.csv` in Excel and apply filters/formatting for better readability.
You Should Know:
- Terraform Output Commands:
– `terraform output` ā Displays outputs in the terminal.
– `terraform output -json` ā Generates JSON for automation.
– `terraform output -raw` ā Extracts a single value. Automating Reports:
Use a CI/CD pipeline (GitHub Actions, Azure DevOps) to auto-generate reports post-deployment.Advanced Filtering with
jq
:jq -r '.resource_details | keys[] as $k | "($k), (.[$k])"' deployment_details.json > filtered_resources.csv
Handling Arrays in Outputs:
If your output contains arrays (e.g., multiple IPs), modify the `jq` query:jq -r '.resource_details[] | [.Resource_Group, .Public_IP] | @csv' deployment_details.json
What Undercode Say
Terraformās `output` is a powerful yet underutilized feature for audit trails and documentation. By automating exports, you eliminate manual errors and save hours. For larger deployments, consider integrating with monitoring tools like Prometheus or Grafana for real-time tracking.
Linux Command Cheat Sheet:
Extract specific output keys terraform output -json | jq '.resource_details.Resource_Group' List all outputs terraform output Redirect to a file terraform output > terraform_outputs.txt
Windows PowerShell Alternative:
(terraform output -json) | ConvertFrom-Json | Select-Object -ExpandProperty resource_details
Expected Output:
A structured CSV file (`resources.csv`) containing:
"Resource_Group","my-rg" "Location","eastus" "VNet_Name","prod-vnet" "Public_IP","20.1.2.3"
Reference URLs:
References:
Reported By: Wanderson Silva – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ā