DEV Community

Revathi Joshi for AWS Community Builders

Posted on

How to debug logging in Terraform setting it to Trace

Logging has to be turned on locally if you want to debug the issues during terraform plan or apply.

Terraform depends on two environment variables being configured. These two variables are TF_LOG and TF_LOG_PATH. I will be setting my TF_LOG environment variable to the TRACE log level and the TF_LOG_PATH environment variable to the logs/terraform_logs.txt file. You can set your TF_LOG environment variable to DEBUG, INFO, WARN, or ERROR.

We will then deploy our resources and check to see if the log is created.

Please visit my GitHub Repository for Terraform articles on various topics being updated on constant basis.

Let’s get started!

Objectives:

1. Login to AWS Management Console

2. Create infrastructure for resources block

3. Under terraform_files resources directory - Create 3 files - main.tf, variables.tf, and outputs.tf.

4. Enable Debug Logging

5. Capture Logging

6. Terraform deployment

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • Cloud9 IDE with AWS CLI.

Resources Used:

Terraform documentation.
Terraform documentation for AMI.
Debugging Terraform
Implement logging-View all Terraform log output

Steps for implementation to this project:

1. Login to AWS Management Console

  • Make sure you're in the N. Virginia (us-east-1) region

2. Create infrastructure for resources block

  • Let’s create the following organizational structure as shown below.

Image description

3. Under terraform_files resources directory - Create 3 files - main.tf, variables.tf, and outputs.tf.

  • 1. main.tf
 terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.23" } } required_version = ">= 0.14.9" } provider "aws" { profile = "default" region = "us-east-1" } resource "aws_instance" "app_server" { ami = var.instance_ami instance_type = "t3.micro" subnet_id = var.subnet_id tags = { Name = var.instance_name } } 
Enter fullscreen mode Exit fullscreen mode
  • 2. variables.tf
 variable "instance_ami" { description = "Value of the AMI used for the EC2 instance" type = string default = "<DUMMY_VALUE_AMI>" } variable "instance_ami" { description = "Value of the AMI used for the EC2 instance" type = string default = "ami-0bb4c991fa89d4b9b" } 
Enter fullscreen mode Exit fullscreen mode
 variable "subnet_id" { description = "Value of the subnet id used for the EC2 instance" type = string default = "<DUMMY_VALUE_SUBNET_ID>" } variable "subnet_id" { description = "Value of the subnet id used for the EC2 instance" type = string default = "subnet-05f279c5812013c5e" } 
Enter fullscreen mode Exit fullscreen mode
 variable "instance_name" { description = "Value of the Name tag for the EC2 instance" type = string default = "MyInstance" } 
Enter fullscreen mode Exit fullscreen mode
  • 3. outputs.tf
 output "instance_id" { description = "ID of the EC2 instance" value = aws_instance.app_server.id } output "instance_ip" { description = "Public IP address of the EC2 instance" value = aws_instance.app_server.public_ip } output "instance_name" { description = "Name of the EC2 instance" value = aws_instance.app_server.tags.Name } 
Enter fullscreen mode Exit fullscreen mode

4. Enable Debug Logging

  • For one time session where you need detailed log information

    1. Set an environment variable for TF_LOG:

For PowerShell

 $env:TF_LOG=TRACE 
Enter fullscreen mode Exit fullscreen mode

For Bash

 export TF_LOG=TRACE 
Enter fullscreen mode Exit fullscreen mode
  • 2. Create the logs directory:
 mkdir logs 
Enter fullscreen mode Exit fullscreen mode
  • 3. Set an environment variable for TF_LOG_PATH to the logs directory:

For PowerShell

 $env:TF_LOG_PATH=terraform.txt 
Enter fullscreen mode Exit fullscreen mode

For Bash

 export TF_LOG_PATH=logs/terraform_logs.txt 
Enter fullscreen mode Exit fullscreen mode

Image description

This outputs your logs to the terraform.txt file.

  • 4. to verify that it worked.

For PowerShell

 > echo $env:TF_LOG TRACE > echo $env:TF_LOG_PATH terraform.txt 
Enter fullscreen mode Exit fullscreen mode

For Bash

 echo $TF_LOG 
Enter fullscreen mode Exit fullscreen mode

Image description

 echo $TF_LOG_PATH 
Enter fullscreen mode Exit fullscreen mode

Image description

5. Capture Logging

  • Check the formatting of your resources: ```

terraform fmt

 - Initialize your working directory: 
Enter fullscreen mode Exit fullscreen mode

terraform init

 - This should have generated some logs. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iici3uju3ho5rsfhb72.png) - Confirm the Log was Written to the Directory - After your working directory is initialized, check your terraform_logs.txt file to confirm it contains logs: 
Enter fullscreen mode Exit fullscreen mode

tail -50 logs/terraform_logs.txt

 - You should see that logs are successfully output to terraform_logs.txt. ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dq1yze1hauo76irk1mqb.png) ## 6. Terraform deployment - Check which version of Terraform you're running - This is useful if you're submitting a bug report to troubleshoot a Terraform issue. 
Enter fullscreen mode Exit fullscreen mode

terraform version

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p34gb6rt8gjeglk6sxv4.png) - Plan your Terraform deployment: 
Enter fullscreen mode Exit fullscreen mode

terraform plan

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vsc2cuhfgmwgw0lwvktf.png) - Apply your Terraform deployment: - When prompted to Enter a value, enter yes. 
Enter fullscreen mode Exit fullscreen mode

terraform apply

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no5igimrsk4fwkhietb4.png) - After the apply completes successfully, check your terraform_logs.txt file to confirm it now contains logs related to your apply: 
Enter fullscreen mode Exit fullscreen mode

tail -50 logs/terraform_logs.txt

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avbu3lnwxugyzaxekqml.png) - MyInstance ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yubm20absdk67xivy2gj.png) # Cleanup 
Enter fullscreen mode Exit fullscreen mode

terraform destroy

 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gud3c27gpb7iya5lywmj.png) # What we have done so far We have successfully enabled debug logging by setting the `TF_LOG` environment variable to the `TRACE` log level and setting `TF_LOG_PATH` environment variable to the `logs/terraform_logs.txt` file. Aftrwards, we have deployed our resources and then checked to see if the log was created. 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)