Reports unused variables, locals, and data sources in a Terraform module and provides a quick-fix to remove them.

Example of a problem:


data "aws_ami" "latest_amazon_linux" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  owners = ["amazon"]
}

data "aws_vpc" "unused_data_source" {
  default = true
}

resource "aws_instance" "example" {
  ami           = data.aws_ami.latest_amazon_linux.id
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}

variable "used_variable" {
  description = "This variable is used in resource configuration"
  type        = string
  default     = "ami-01456a894f71116f2"
}

locals {
  instance_name = "used-instance"
}

resource "aws_instance" "example1" {
  ami           = var.used_variable
  instance_type = "t2.micro"

  tags = {
    Name = local.instance_name
  }
}

variable "unused_variable1" {
  description = "This variable is not used anywhere in the configuration"
  type        = string
  default     = "default_value1"
}

locals {
  unused_local1 = "This is an unused local value"
}

In this example, unused_data_source, unused_variable1, and unused_local1 are declared in the file, but are not used anywhere.

After the quick-fix is applied:


data "aws_ami" "latest_amazon_linux" {
  most_recent = true

  filter {
    name   = "name"
    values = ["amzn2-ami-hvm-*-x86_64-gp2"]
  }

  owners = ["amazon"]
}

resource "aws_instance" "example" {
  ami           = data.aws_ami.latest_amazon_linux.id
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleInstance"
  }
}

variable "used_variable" {
  description = "This variable is used in resource configuration"
  type        = string
  default     = "ami-01456a894f71116f2"
}

locals {
  instance_name = "used-instance"
}

resource "aws_instance" "example1" {
  ami           = var.used_variable
  instance_type = "t2.micro"

  tags = {
    Name = local.instance_name
  }
}

locals {
}