2018-05-13 21:30:21 +02:00
|
|
|
---
|
|
|
|
layout: "functions"
|
2018-12-20 05:35:11 +01:00
|
|
|
page_title: "file - Functions - Configuration Language"
|
2018-05-13 21:30:21 +02:00
|
|
|
sidebar_current: "docs-funcs-file-file-x"
|
|
|
|
description: |-
|
|
|
|
The file function reads the contents of the file at the given path and
|
|
|
|
returns them as a string.
|
|
|
|
---
|
|
|
|
|
|
|
|
# `file` Function
|
|
|
|
|
2019-01-17 01:33:57 +01:00
|
|
|
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
|
|
|
earlier, see
|
|
|
|
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
|
|
|
|
2018-05-13 21:30:21 +02:00
|
|
|
`file` reads the contents of a file at the given path and returns them as
|
|
|
|
a string.
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
file(path)
|
|
|
|
```
|
|
|
|
|
|
|
|
Strings in the Terraform language are sequences of Unicode characters, so
|
|
|
|
this function will interpret the file contents as UTF-8 encoded text and
|
|
|
|
return the resulting Unicode characters. If the file contains invalid UTF-8
|
|
|
|
sequences then this function will produce an error.
|
|
|
|
|
2018-12-21 02:42:42 +01:00
|
|
|
This function can be used only with files that already exist on disk
|
|
|
|
at the beginning of a Terraform run. Functions do not participate in the
|
|
|
|
dependency graph, so this function cannot be used with files that are generated
|
|
|
|
dynamically during a Terraform operation. We do not recommend using dynamic
|
|
|
|
local files in Terraform configurations, but in rare situations where this is
|
|
|
|
necessary you can use
|
2018-05-13 21:30:21 +02:00
|
|
|
[the `local_file` data source](/docs/providers/local/d/file.html)
|
|
|
|
to read files while respecting resource dependencies.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
|
|
|
> file("${path.module}/hello.txt")
|
|
|
|
Hello World
|
|
|
|
```
|
|
|
|
|
|
|
|
## Related Functions
|
|
|
|
|
|
|
|
* [`filebase64`](./filebase64.html) also reads the contents of a given file,
|
|
|
|
but returns the raw bytes in that file Base64-encoded, rather than
|
|
|
|
interpreting the contents as UTF-8 text.
|
2018-10-11 12:40:09 +02:00
|
|
|
* [`fileexists`](./fileexists.html) determines whether a file exists
|
|
|
|
at a given path.
|
2018-12-21 02:42:42 +01:00
|
|
|
* [`templatefile`](./templatefile.html) renders uses a file from disk as a
|
|
|
|
template.
|