function: direxists

Given a path string, will return true if the directory exists. This function works only with directories. If used with a file, the function will return an error.

This function behaves similar to the built-in fileexists function, however, direxists will not replace filesystem paths including ~ with the current user's home directory path. This functionality can be achieved by using the built-in pathexpand function with direxists, see example below.

Example Usage

Basic Usage

# Configuration using provider functions must include required_providers configuration.
terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
      # Setting the provider version is a strongly recommended practice
      # version = "..."
    }
  }
  # Provider functions require Terraform 1.8 and later.
  required_version = ">= 1.8.0"
}

output "example_output" {
  value = provider::local::direxists("${path.module}/example-directory")
}

Usage with home directory

# Configuration using provider functions must include required_providers configuration.
terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
      # Setting the provider version is a strongly recommended practice
      # version = "..."
    }
  }
  # Provider functions require Terraform 1.8 and later.
  required_version = ">= 1.8.0"
}

output "example_output_homedir" {
  value = provider::local::direxists(pathexpand("~/.ssh"))
}

Signature

direxists(path string) bool

Arguments

  1. path (String) Relative or absolute path to check for the existence of a directory