<p>In this particular case where the path is well-known I would typically use the <code>file</code> function instead of the <code>local_sensitive_file</code> data source, since the data source is primarily intended for reading files that are generated dynamically while Terraform is running.</p>
<pre><code>locals {
  filename = &quot;${path.module}/whatever.txt&quot;
  example  = fileexists(local.filename) ? sensitive(file(local.filename)) : null
}
</code></pre>
<p>However, if in your real configuration this <em>is</em> 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:</p>
<pre><code>locals {
  filename = &quot;${path.module}/whatever.txt&quot;
}

data &quot;local_sensitive_file&quot; &quot;example&quot; {
  count = fileexists(local.filename) ? 1 : 0

  filename = local.filename
}

locals {
  file_content = one(data.local_sensitive_file.example[*].content)
}
</code></pre>
<p>That last expression using <a href="https://developer.hashicorp.com/terraform/language/functions/one" rel="nofollow noreferrer"><code>one</code></a> is a concise way to take either the one value of <code>content</code> or to produce <code>null</code> if there are no instances of this resource. Therefore <code>local.file_content</code> would be either the content of the file or <code>null</code> if there is no file.</p>
<hr />
<p>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 &quot;use this only if it exists&quot;. If applying this Terraform configuration could potentially <em>change</em> 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.</p>
<p>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.</p>
<p>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:</p>
<pre><code>variable &quot;filename&quot; {
  type    = string
  default = null
}

data &quot;local_sensitive_file&quot; &quot;example&quot; {
  count = var.filename != null ? 1 : 0

  filename = var.filename
}

locals {
  file_content = one(data.local_sensitive_file.example[*].content)
}
</code></pre>
<p>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.</p>

CC BY-SA 4.0
2023-02-15T02:20:59.633Z
2023-02-15T02:20:59.633Z
Martin Atkins
<p>In Terraform, you can use the <code>fileexists</code> function to check if a file exists before attempting to use it. This function returns a boolean value indicating whether the specified file exists or not.</p>
<p><a href="https://developer.hashicorp.com/terraform/language/functions/fileexists" rel="nofollow noreferrer">https://developer.hashicorp.com/terraform/language/functions/fileexists</a></p>
<p>in your case since you don't give to much logic behind what you want to do</p>
<pre><code>filename = fileexists(&quot;/tmp/foo&quot;) ? &quot;/tmp/foo&quot; : &quot;/tmp/whateverfile_you_want_if_foo_doesn't_exist&quot;
</code></pre>
<p><code>filename</code> will have <code>/tmp/foo</code> if file exists and <code>/tmp/whateverfile_you_want_if_foo_doesn't_exis</code> if file doesn't exists</p>

CC BY-SA 4.0
2023-02-14T21:07:01.307Z
Yes, but I don't want `/tmp/whateverfile_you_want_if_foo_doesn't_exis` I just want things to continue if the file doesn't exist. I don't have a fallback or default that I know does exist.
Evan Carroll
CC BY-SA 4.0
2023-02-14T21:09:57.437Z
@EvanCarroll what do you want to happen if the file doesn't exist?
Eduard Florinescu
CC BY-SA 4.0
2023-02-14T21:11:32.007Z
I'd like something like `data.local_sensitive_file.foo.exists` to be `null` or `0` or something.
Evan Carroll
CC BY-SA 4.0
2023-02-14T21:14:53.387Z
@EvanCarroll
`data.local_sensitive_file.foo.exists = fileexists("/tmp/foo") ? 1 : 0`

then you should have something like the above
Eduard Florinescu
CC BY-SA 4.0
2023-02-14T21:16:02.270Z
But the whole stanza of `data.local_sensitive_file.foo` will not run if the file does **NOT** exist. It'll just error.
Evan Carroll
CC BY-SA 4.0
2023-02-14T21:48:36.250Z
@EvanCarroll I think there should be a couple of extra-steps using another variable, I don't have a Terraform on this machine to try
Eduard Florinescu
CC BY-SA 4.0
2023-02-14T21:05:58.440Z
2023-02-14T21:05:58.440Z
Eduard Florinescu
<p>Terraform give us the ability to a read a local file, which has a secret..</p>
<pre><code>data &quot;local_sensitive_file&quot; &quot;foo&quot; {
  filename = &quot;/tmp/foo&quot;
}
</code></pre>
<p>But, what is the right way to handle a file that may not exist? In the above, if <code>/tmp/foo</code> doesn't exist, you get this error on compilation:</p>
<blockquote>
<p>Error: Read local file data source error</p>
<pre><code>with data.local_sensitive_file.foo,
on ssh.tf line 12, in data &quot;local_sensitive_file&quot; &quot;foo&quot;:
12: data &quot;local_sensitive_file&quot; &quot;foo&quot; {
</code></pre>
<p>The file at given path cannot be read.</p>
<p>+Original Error: <code>open /tmp/foo: no such file or directory</code></p>
</blockquote>
<p>Seems as if setting the filename to <code>null</code> also doesn't work.</p>

<terraform>
How can I read a file if it exists, but if it doesn't do not throw a fatal error?
Evan Carroll
# How can I read a file if it exists, but if it doesn't do not throw a fatal error?

(Asked by: username_0 on 2023-02-14)

Terraform give us the ability to a read a local file, which has a secret..
data "local_sensitive_file" "foo" {
  filename = "/tmp/foo"
}

But, what is the right way to handle a file that may not exist? In the above, if /tmp/foo doesn't exist, you get this error on compilation:

Error: Read local file data source error
with data.local_sensitive_file.foo,
on ssh.tf line 12, in data "local_sensitive_file" "foo":
12: data "local_sensitive_file" "foo" {

The file at given path cannot be read.
+Original Error: open /tmp/foo: no such file or directory

Seems as if setting the filename to null also doesn't work.

**Tags:** terraform


---

## 2 answers

### Answer by username_1 on 2023-02-15. Score: 2

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.


---

### Answer by username_2 on 2023-02-14. Score: 1

In Terraform, you can use the fileexists function to check if a file exists before attempting to use it. This function returns a boolean value indicating whether the specified file exists or not.
https://developer.hashicorp.com/terraform/language/functions/fileexists
in your case since you don't give to much logic behind what you want to do
filename = fileexists("/tmp/foo") ? "/tmp/foo" : "/tmp/whateverfile_you_want_if_foo_doesn't_exist"

filename will have /tmp/foo if file exists and /tmp/whateverfile_you_want_if_foo_doesn't_exis if file doesn't exists

> **Comments:**
> **username_0**: Yes, but I don't want `/tmp/whateverfile_you_want_if_foo_doesn't_exis` I just want things to continue if the file doesn't exist. I don't have a fallback or default that I know does exist.  
> **username_2**: @EvanCarroll what do you want to happen if the file doesn't exist?  
> **username_0**: I'd like something like `data.local_sensitive_file.foo.exists` to be `null` or `0` or something.  
> **username_2**: @EvanCarroll `data.local_sensitive_file.foo.exists = fileexists("/tmp/foo") ? 1 : 0`  then you should have something like the above  
> **username_0**: But the whole stanza of `data.local_sensitive_file.foo` will not run if the file does **NOT** exist. It'll just error.  
> **username_2**: @EvanCarroll I think there should be a couple of extra-steps using another variable, I don't have a Terraform on this machine to try  

---

