In this article we will cover Debugging Terraform Execution & Performance Optimization
When working with Terraform to automate infrastructure deployment, you’ll eventually run into scenarios where terraform apply
becomes slow, stalls, or fails unexpectedly. This can feel overwhelming. But don’t worry—Terraform gives you tools to debug issues and optimize performance so your deployments are fast, smooth, and reliable.
In this article, you’ll learn how to:
- Set up a simple Terraform project
- Use debug logs to diagnose issues
- Speed up
terraform apply
with parallelism - Clean up large outputs for better performance
Table of Contents
Prerequisites
Before you begin:
- You should have an AWS account and credentials configured.
- Terraform must be installed.
Step #1: Create a Simple Terraform Project
First, create a directory and navigate into it:
mkdir terraform
cd terraform

main.tf
– Define Infrastructure:
Let’s create a basic EC2 instance on AWS:
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "example" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
tags = {
Name = "MyExampleInstance"
}
}

Initialize the Project:
terraform init

Terraform will download the AWS provider plugin.
Step #2:Apply Terraform with Debugging & Performance Tips
Now comes the important part—executing and optimizing terraform apply
.
#1.Enable Debug Logs
To understand what Terraform is doing internally (especially if it’s slow or crashing), enable debug logs.
TF_LOG=DEBUG terraform apply

To save logs to a file for later review:
TF_LOG=DEBUG TF_LOG_PATH=debug.log terraform apply

This gives detailed logs of every step, including API calls to AWS and dependency resolution.

#2.Speed Up Execution with Parallelism
Terraform applies resources in parallel by default, but you can increase the parallelism level to speed things up:
terraform apply -parallelism=5

This allows up to 5 resources to be created at once.
Be careful: some resources depend on others and must be created in order. Use this mainly for large, independent deployments.
#3.Optimize Outputs
By default, you may be tempted to output an entire resource:
output "full_instance" {
value = aws_instance.example
}
But this slows things down. It’s better to only output what you need:
output "instance_ip" {
value = aws_instance.example.public_ip
}
Why this matters: Terraform calculates and displays outputs after provisioning. If you output a full resource, it prints a large JSON block, which takes time—especially with many or complex resources.
Bonus Tips for Performance Optimization
Tip | How |
---|---|
Use -target | Apply only a specific resource: terraform apply -target=aws_instance.example |
Use Efficient Backends | For team use, store state in S3 and lock with DynamoDB (for AWS projects) |
Split into Modules | Break large main.tf files into smaller module files |
Limit Data Sources | Avoid unnecessary API calls with data blocks |
Check Resource Dependencies | Use terraform graph or check the plan to avoid unnecessary waits |
Conclusion:
Debugging and optimizing Terraform is important, especially for beginners who face slow terraform apply
times or unexpected errors. In this guide, you learned how to create a basic Terraform setup, use TF_LOG=DEBUG
to see detailed logs, speed up execution using the -parallelism
flag, and reduce unnecessary outputs to make your plans faster. These simple tips can help you solve problems quickly and improve your Terraform experience.
Related Articles:
Managing Large-Scale Infrastructure with Terraform Modules
Reference: