How to Parse Terraform Plan Output in Python
Parse mock Terraform plan output text into structured add, change, and destroy lists using Python.
Python code
31 linesimport json
from typing import Dict, List
def parse_terraform_plan_output(plan_output_text: str) -> Dict[str, List[str]]:
"""
Parses a mock Terraform plan output text into a structured dictionary.
"""
parsed: Dict[str, List[str]] = {"add": [], "change": [], "destroy": []}
for line in plan_output_text.splitlines():
line = line.strip()
if line.startswith("+ "):
parsed["add"].append(line[2:].strip())
elif line.startswith("~ "):
parsed["change"].append(line[2:].strip())
elif line.startswith("- "):
parsed["destroy"].append(line[2:].strip())
return parsed
if __name__ == "__main__":
mock_plan = """
+ aws_instance.web
~ aws_security_group.sg (updated in-place)
- aws_db_instance.db
+ aws_s3_bucket.bucket
~ aws_subnet.subnet (updated in-place)
Not a change line
"""
result = parse_terraform_plan_output(mock_plan)
print(json.dumps(result, indent=2))
Output
{
"add": [
"aws_instance.web",
"aws_s3_bucket.bucket"
],
"change": [
"aws_security_group.sg (updated in-place)",
"aws_subnet.subnet (updated in-place)"
],
"destroy": [
"aws_db_instance.db"
]
}
How it works
The function splits the plan output into lines and strips whitespace for consistent parsing. Each line is checked for Terraform's action prefixes (+, ~, -) to categorize resources into add, change, or destroy lists. The line[2:] slice removes the prefix and an extra space, leaving the resource name. The result is a dictionary with three lists that can be easily inspected or passed to automation logic.
Common mistakes
- Forgetting to strip lines before checking prefixes, causing false negatives
- Assuming the prefix has no trailing space, missing the [2:] slice position
- Including non-resource lines that start with these symbols in logs
- Handling only filtered plan output instead of the full `terraform plan` text
Variations
- Use regex to capture both resource type and name separately
- Read from `terraform show -json` output for more structured data instead
Real-world use cases
- Pre-approval scripts that skip destructive changes in CI pipelines.
- Automated drift detection comparing planned vs applied infrastructure state.
- Alerting systems that summarize permission-scoped plan outputs for audit logs.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.