Skip to main content
Welcome to the OpenTofu lab on Tainting and Debugging. In this hands-on tutorial, you will learn:
  1. How to export logs to a specific path using environment variables
  2. Generating and configuring debug log levels
  3. Enabling logging for an OpenTofu project
  4. Tainting and replacing Terraform resources (AWS EC2 example)

1. Environment Variables for Debugging

OpenTofu uses two key environment variables to control logging:
TF_LOG_PATH must be set alongside TF_LOG; otherwise, no logs will be written to disk.

2. Enabling Logging and Exporting Logs

Assume your project directory is /root/OpenTofu/projects/project_a. To enable error-level logging and export output to /tmp/project_a.log, run:
The image shows a Visual Studio Code interface with a task description on the left and a file explorer and terminal on the right. The task involves enabling logging for a project and exporting logs.
When prompted, type yes. Authentication warnings may appear, but the log file will be created at /tmp/project_a.log.
Do not modify any configuration files before exporting logs; this ensures you capture the original error context.
Among the log levels, trace produces the most detailed output.

3. Provisioning an EC2 Instance and Tainting

Navigate to the projectB directory:
Your main.tf defines an AWS EC2 instance:
Initialize and apply the configuration:
The image shows a coding environment with a task description on the left and a Visual Studio Code interface on the right, displaying a project directory and a terminal with an error message related to AWS credentials.
Confirm with yes. After apply completes, the EC2 instance ProjectB appears in your AWS console.

Effects of Tainting

To mark the EC2 instance for replacement:
Review the plan:
Expected output:
This indicates that the tainted resource will be recreated.

Replacing a Tainted Resource

Execute the apply command with -replace:
OpenTofu will destroy the existing instance and create a new one before finalizing the update.

References

This concludes the lab on Tainting and Debugging with OpenTofu. In the next lesson, we’ll explore resource dependencies and outputs.

Watch Video

Practice Lab