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
Enter fullscreen mode Exit fullscreen mode
  • Initialize your working directory:
terraform init
Enter fullscreen mode Exit fullscreen mode
  • This should have generated some logs.

Image description

  • 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:
tail -50 logs/terraform_logs.txt
Enter fullscreen mode Exit fullscreen mode
  • You should see that logs are successfully output to terraform_logs.txt.

Image description

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.
terraform version
Enter fullscreen mode Exit fullscreen mode

Image description

  • Plan your Terraform deployment:
terraform plan
Enter fullscreen mode Exit fullscreen mode

Image description

  • Apply your Terraform deployment:
  • When prompted to Enter a value, enter yes.
terraform apply
Enter fullscreen mode Exit fullscreen mode

Image description

  • After the apply completes successfully, check your terraform_logs.txt file to confirm it now contains logs related to your apply:
tail -50 logs/terraform_logs.txt
Enter fullscreen mode Exit fullscreen mode

Image description

  • MyInstance

Image description

Cleanup

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Image description

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.

Top comments (0)