github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/provisioners/file.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Provisioner: file"
     4  sidebar_current: "docs-provisioners-file"
     5  description: |-
     6    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.
     7  ---
     8  
     9  # File Provisioner
    10  
    11  The `file` provisioner is used to copy files or directories from the machine
    12  executing Terraform to the newly created resource. The `file` provisioner
    13  supports both `ssh` and `winrm` type [connections](/docs/provisioners/connection.html).
    14  
    15  -> **Note:** Provisioners should only be used as a last resort. For most
    16  common situations there are better alternatives. For more information, see
    17  [the main Provisioners page](./).
    18  
    19  ## Example usage
    20  
    21  ```hcl
    22  resource "aws_instance" "web" {
    23    # ...
    24  
    25    # Copies the myapp.conf file to /etc/myapp.conf
    26    provisioner "file" {
    27      source      = "conf/myapp.conf"
    28      destination = "/etc/myapp.conf"
    29    }
    30  
    31    # Copies the string in content into /tmp/file.log
    32    provisioner "file" {
    33      content     = "ami used: ${self.ami}"
    34      destination = "/tmp/file.log"
    35    }
    36  
    37    # Copies the configs.d folder to /etc/configs.d
    38    provisioner "file" {
    39      source      = "conf/configs.d"
    40      destination = "/etc"
    41    }
    42  
    43    # Copies all files and folders in apps/app1 to D:/IIS/webapp1
    44    provisioner "file" {
    45      source      = "apps/app1/"
    46      destination = "D:/IIS/webapp1"
    47    }
    48  }
    49  ```
    50  
    51  ## Argument Reference
    52  
    53  The following arguments are supported:
    54  
    55  * `source` - This is the source file or folder. It can be specified as
    56    relative to the current working directory or as an absolute path. This
    57    attribute cannot be specified with `content`.
    58  
    59  * `content` - This is the content to copy on the destination. If destination is a file,
    60    the content will be written on that file, in case of a directory a file named
    61    `tf-file-content` is created. It's recommended to use a file as the destination. A
    62    [`template_file`](/docs/providers/template/d/file.html) might be referenced in here, or
    63    any interpolation syntax. This attribute cannot be specified with `source`.
    64  
    65  * `destination` - (Required) This is the destination path. It must be specified as an
    66    absolute path.
    67  
    68  ## Directory Uploads
    69  
    70  The file provisioner is also able to upload a complete directory to the remote machine.
    71  When uploading a directory, there are a few important things you should know.
    72  
    73  First, when using the `ssh` connection type the destination directory must already exist.
    74  If you need to create it, use a remote-exec provisioner just prior to the file provisioner
    75  in order to create the directory. When using the `winrm` connection type the destination
    76  directory will be created for you if it doesn't already exist.
    77  
    78  Next, the existence of a trailing slash on the source path will determine whether the
    79  directory name will be embedded within the destination, or whether the destination will
    80  be created. An example explains this best:
    81  
    82  If the source is `/foo` (no trailing slash), and the destination is `/tmp`, then the contents
    83  of `/foo` on the local machine will be uploaded to `/tmp/foo` on the remote machine. The
    84  `foo` directory on the remote machine will be created by Terraform.
    85  
    86  If the source, however, is `/foo/` (a trailing slash is present), and the destination is
    87  `/tmp`, then the contents of `/foo` will be uploaded directly into `/tmp`.
    88  
    89  This behavior was adopted from the standard behavior of
    90  [rsync](https://linux.die.net/man/1/rsync).
    91  
    92  -> **Note:** Under the covers, rsync may or may not be used.