In this particular case where the path is well-known I would typically use the file
function instead of the local_sensitive_file
data source, since the data source is primarily intended for reading files that are generated dynamically while Terraform is running.
locals {
filename = "${path.module}/whatever.txt"
example = fileexists(local.filename) ? sensitive(file(local.filename)) : null
}
However, if in your real configuration this is a file that comes from outside of your Terraform configuration or is generated dynamically by your configuration then you can get a similar effect by telling Terraform that there should be no instances of this data resource if the file doesn't exist:
locals {
filename = "${path.module}/whatever.txt"
}
data "local_sensitive_file" "example" {
count = fileexists(local.filename) ? 1 : 0
filename = local.filename
}
locals {
file_content = one(data.local_sensitive_file.example[*].content)
}
That last expression using one
is a concise way to take either the one value of content
or to produce null
if there are no instances of this resource. Therefore local.file_content
would be either the content of the file or null
if there is no file.
Although I suspect this won't be a problem in your case due to this being temporary local files on disk rather than long-lived objects in a remote system, I suggest caution when trying to declare rules like "use this only if it exists". If applying this Terraform configuration could potentially change whether this file exists then you risk declaring something contradictory that will cause Terraform to exhibit unspecified behavior, because it'll make assumptions during the planning phase that won't necessarily hold during the apply phase.
In general I'd recommend instead being explicit about whether you expect the file to exist or not, so that Terraform can tell you immediately if your assumption is incorrect, rather than relying on your assumption and then acting strangely if it turns out not to be true.
For example, you can declare an input variable whose presence explicitly represents that the file ought to exist, but that can be omitted when it need not exist:
variable "filename" {
type = string
default = null
}
data "local_sensitive_file" "example" {
count = var.filename != null ? 1 : 0
filename = var.filename
}
locals {
file_content = one(data.local_sensitive_file.example[*].content)
}
This is essentially the same as my previous example except now the predicate for deciding whether to read the file is whether a filename was specified at all, and so Terraform can therefore give quick feedback if the caller of this module makes a typo when writing the filename, or otherwise assumes a file to exist that doesn't actually exist.