2014-07-23 18:40:15 +02:00
---
2020-08-15 03:51:06 +02:00
layout: "language"
2014-07-23 18:40:15 +02:00
page_title: "Provisioner: file"
sidebar_current: "docs-provisioners-file"
2014-10-22 05:21:56 +02:00
description: |-
2015-05-08 13:45:31 +02:00
The `file` provisioner is used to copy files or directories from the machine executing Terraform to the newly created resource. The `file` provisioner supports both `ssh` and `winrm` type connections.
2014-07-23 18:40:15 +02:00
---
# File Provisioner
The `file` provisioner is used to copy files or directories from the machine
2015-04-10 21:28:28 +02:00
executing Terraform to the newly created resource. The `file` provisioner
supports both `ssh` and `winrm` type [connections ](/docs/provisioners/connection.html ).
2014-07-23 18:40:15 +02:00
2019-09-06 00:50:04 +02:00
-> **Note:** Provisioners should only be used as a last resort. For most
common situations there are better alternatives. For more information, see
[the main Provisioners page ](./ ).
2014-07-23 18:40:15 +02:00
## Example usage
2017-04-05 17:29:27 +02:00
```hcl
2014-07-23 18:40:15 +02:00
resource "aws_instance" "web" {
2017-02-18 23:48:50 +01:00
# ...
# Copies the myapp.conf file to /etc/myapp.conf
provisioner "file" {
source = "conf/myapp.conf"
destination = "/etc/myapp.conf"
}
# Copies the string in content into /tmp/file.log
provisioner "file" {
content = "ami used: ${self.ami}"
destination = "/tmp/file.log"
}
# Copies the configs.d folder to /etc/configs.d
provisioner "file" {
source = "conf/configs.d"
destination = "/etc"
}
# Copies all files and folders in apps/app1 to D:/IIS/webapp1
provisioner "file" {
source = "apps/app1/"
destination = "D:/IIS/webapp1"
}
2014-07-23 18:40:15 +02:00
}
```
## Argument Reference
2014-07-29 08:51:24 +02:00
The following arguments are supported:
2014-07-23 18:40:15 +02:00
2016-10-31 09:52:27 +01:00
* `source` - This is the source file or folder. It can be specified as
relative to the current working directory or as an absolute path. This
attribute cannot be specified with `content` .
2016-02-22 17:45:15 +01:00
* `content` - This is the content to copy on the destination. If destination is a file,
the content will be written on that file, in case of a directory a file named
2016-10-31 09:52:27 +01:00
`tf-file-content` is created. It's recommended to use a file as the destination. A
2020-12-17 00:17:27 +01:00
[`template_file` ](https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file ) might be referenced in here, or
2016-10-09 18:19:55 +02:00
any interpolation syntax. This attribute cannot be specified with `source` .
2014-07-23 18:40:15 +02:00
* `destination` - (Required) This is the destination path. It must be specified as an
absolute path.
## Directory Uploads
The file provisioner is also able to upload a complete directory to the remote machine.
When uploading a directory, there are a few important things you should know.
2015-04-10 21:28:28 +02:00
First, when using the `ssh` connection type the destination directory must already exist.
If you need to create it, use a remote-exec provisioner just prior to the file provisioner
in order to create the directory. When using the `winrm` connection type the destination
directory will be created for you if it doesn't already exist.
2014-07-23 18:40:15 +02:00
Next, the existence of a trailing slash on the source path will determine whether the
directory name will be embedded within the destination, or whether the destination will
be created. An example explains this best:
If the source is `/foo` (no trailing slash), and the destination is `/tmp` , then the contents
of `/foo` on the local machine will be uploaded to `/tmp/foo` on the remote machine. The
`foo` directory on the remote machine will be created by Terraform.
If the source, however, is `/foo/` (a trailing slash is present), and the destination is
2018-04-17 01:27:52 +02:00
`/tmp` , then the contents of `/foo` will be uploaded directly into `/tmp` .
2014-07-23 18:40:15 +02:00
2016-10-31 09:52:27 +01:00
This behavior was adopted from the standard behavior of
[rsync ](https://linux.die.net/man/1/rsync ).
2016-10-09 18:19:55 +02:00
2016-10-31 09:52:27 +01:00
-> **Note:** Under the covers, rsync may or may not be used.