github.com/pulumi/terraform@v1.4.0/website/docs/language/resources/provisioners/connection.mdx (about) 1 --- 2 page_title: Provisioner Connection Settings 3 description: >- 4 The connection block allows you to manage provisioner connection defaults for 5 SSH and WinRM. 6 --- 7 8 # Provisioner Connection Settings 9 10 Most provisioners require access to the remote resource via SSH or WinRM and 11 expect a nested `connection` block with details about how to connect. 12 13 ~> **Important:** Use provisioners as a last resort. There are better alternatives for most situations. Refer to 14 [Declaring Provisioners](/language/resources/provisioners/syntax) for more details. 15 16 ## Connection Block 17 18 You can create one or more `connection` blocks that describe how to access the remote resource. One use case for providing multiple connections is to have an initial provisioner connect as the `root` user to set up user accounts and then have subsequent provisioners connect as a user with more limited permissions. 19 20 Connection blocks don't take a block label and can be nested within either a 21 `resource` or a `provisioner`. 22 23 * A `connection` block nested directly within a `resource` affects all of 24 that resource's provisioners. 25 * A `connection` block nested in a `provisioner` block only affects that 26 provisioner and overrides any resource-level connection settings. 27 28 Since the SSH connection type is most often used with 29 newly-created remote resources, validation of SSH host keys is disabled by 30 default. If this is not acceptable, you can establish a separate mechanism for key distribution and explicitly set the `host_key` argument (details below) to verify against a specific key or signing CA. 31 32 -> **Note:** In Terraform 0.11 and earlier, providers could set default values 33 for some connection settings, so that `connection` blocks could sometimes be 34 omitted. This feature was removed in 0.12 in order to make Terraform's behavior 35 more predictable. 36 37 38 ### Example usage 39 40 ```hcl 41 # Copies the file as the root user using SSH 42 provisioner "file" { 43 source = "conf/myapp.conf" 44 destination = "/etc/myapp.conf" 45 46 connection { 47 type = "ssh" 48 user = "root" 49 password = "${var.root_password}" 50 host = "${var.host}" 51 } 52 } 53 54 # Copies the file as the Administrator user using WinRM 55 provisioner "file" { 56 source = "conf/myapp.conf" 57 destination = "C:/App/myapp.conf" 58 59 connection { 60 type = "winrm" 61 user = "Administrator" 62 password = "${var.admin_password}" 63 host = "${var.host}" 64 } 65 } 66 ``` 67 68 ### The `self` Object 69 70 Expressions in `connection` blocks cannot refer to their parent resource by name. References create dependencies, and referring to a resource by name within its own block would create a dependency cycle. Instead, expressions can use the `self` object, which represents the connection's parent resource and has all of that resource's attributes. For example, use `self.public_ip` to reference an `aws_instance`'s `public_ip` attribute. 71 72 73 ### Argument Reference 74 75 The `connection` block supports the following argments. Some arguments are only supported by either the SSH or the WinRM connection type. 76 77 78 | Argument | Connection Type | Description | Default | 79 |---------------|--------------|-------------|---------| 80 | `type` | Both | The connection type. Valid values are `"ssh"` and `"winrm"`. Provisioners typically assume that the remote system runs Microsoft Windows when using WinRM. Behaviors based on the SSH `target_platform` will force Windows-specific behavior for WinRM, unless otherwise specified.| `"ssh"` | 81 | `user` | Both | The user to use for the connection. | `root` for type `"ssh"`<br />`Administrator` for type `"winrm"` | 82 | `password` | Both | The password to use for the connection. | | 83 | `host` | Both | **Required** - The address of the resource to connect to. | | 84 | `port` | Both| The port to connect to. | `22` for type `"ssh"`<br />`5985` for type `"winrm"` | 85 | `timeout` | Both | The timeout to wait for the connection to become available. Should be provided as a string (e.g., `"30s"` or `"5m"`.) | `"5m"` | 86 | `script_path` | Both | The path used to copy scripts meant for remote execution. Refer to [How Provisioners Execute Remote Scripts](#how-provisioners-execute-remote-scripts) below for more details. | (details below) | 87 | `private_key` | SSH | The contents of an SSH key to use for the connection. These can be loaded from a file on disk using [the `file` function](/language/functions/file). This takes preference over `password` if provided. | | 88 | `certificate` | SSH | The contents of a signed CA Certificate. The certificate argument must be used in conjunction with a `private_key`. These can be loaded from a file on disk using the [the `file` function](/language/functions/file). | | 89 | `agent` | SSH | Set to `false` to disable using `ssh-agent` to authenticate. On Windows the only supported SSH authentication agent is [Pageant](http://the.earth.li/\~sgtatham/putty/0.66/htmldoc/Chapter9.html#pageant). | | 90 | `agent_identity` | SSH | The preferred identity from the ssh agent for authentication. | | 91 | `host_key` | SSH | The public key from the remote host or the signing CA, used to verify the connection. | | 92 | `target_platform` | SSH | The target platform to connect to. Valid values are `"windows"` and `"unix"`. If the platform is set to `windows`, the default `script_path` is `c:\windows\temp\terraform_%RAND%.cmd`, assuming [the SSH default shell](https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_server_configuration#configuring-the-default-shell-for-openssh-in-windows) is `cmd.exe`. If the SSH default shell is PowerShell, set `script_path` to `"c:/windows/temp/terraform_%RAND%.ps1"` | `"unix"` | 93 | `https` | WinRM | Set to `true` to connect using HTTPS instead of HTTP. | | 94 | `insecure` | WinRM | Set to `true` to skip validating the HTTPS certificate chain. | | 95 | `use_ntlm` | WinRM | Set to `true` to use NTLM authentication rather than default (basic authentication), removing the requirement for basic authentication to be enabled within the target guest. Refer to [Authentication for Remote Connections](https://docs.microsoft.com/en-us/windows/win32/winrm/authentication-for-remote-connections) in the Windows App Development documentation for more details. | | 96 | `cacert` | WinRM | The CA certificate to validate against. | | 97 98 99 <a id="bastion"></a> 100 101 ## Connecting through a Bastion Host with SSH 102 103 The `ssh` connection also supports the following arguments to connect 104 indirectly with a [bastion host](https://en.wikipedia.org/wiki/Bastion_host). 105 106 | Argument | Description | Default | 107 |---------------|-------------|---------| 108 | `bastion_host` | Setting this enables the bastion Host connection. The provisioner will connect to `bastion_host` first, and then connect from there to `host`. | | 109 | `bastion_host_key` | The public key from the remote host or the signing CA, used to verify the host connection. | | 110 | `bastion_port` | The port to use connect to the bastion host. | The value of the `port` field.| 111 | `bastion_user`| The user for the connection to the bastion host. | The value of the `user` field. | 112 | `bastion_password` | The password to use for the bastion host. | The value of the `password` field. | 113 | `bastion_private_key` | The contents of an SSH key file to use for the bastion host. These can be loaded from a file on disk using [the `file` function](/language/functions/file). | The value of the `private_key` field. | 114 | `bastion_certificate` | The contents of a signed CA Certificate. The certificate argument must be used in conjunction with a `bastion_private_key`. These can be loaded from a file on disk using the [the `file` function](/language/functions/file). | 115 116 ## Connection through a HTTP Proxy with SSH 117 118 The `ssh` connection also supports the following fields to facilitate connections by SSH over HTTP proxy. 119 120 | Argument | Description | Default | 121 |---------------|-------------|---------| 122 | `proxy_scheme` | http or https | | 123 | `proxy_host` | Setting this enables the SSH over HTTP connection. This host will be connected to first, and then the `host` or `bastion_host` connection will be made from there. | | 124 | `proxy_port` | The port to use connect to the proxy host. | | 125 | `proxy_user_name` | The username to use connect to the private proxy host. This argument should be specified only if authentication is required for the HTTP Proxy server. | | 126 | `proxy_user_password` | The password to use connect to the private proxy host. This argument should be specified only if authentication is required for the HTTP Proxy server. | | 127 128 ## How Provisioners Execute Remote Scripts 129 130 Provisioners which execute commands on a remote system via a protocol such as 131 SSH typically achieve that by uploading a script file to the remote system 132 and then asking the default shell to execute it. Provisioners use this strategy 133 because it then allows you to use all of the typical scripting techniques 134 supported by that shell, including preserving environment variable values 135 and other context between script statements. 136 137 However, this approach does have some consequences which can be relevant in 138 some unusual situations, even though this is just an implementation detail 139 in typical use. 140 141 Most importantly, there must be a suitable location in the remote filesystem 142 where the provisioner can create the script file. By default, Terraform 143 chooses a path containing a random number using the following patterns 144 depending on how `target_platform` is set: 145 146 * `"unix"`: `/tmp/terraform_%RAND%.sh` 147 * `"windows"`: `C:/windows/temp/terraform_%RAND%.cmd` 148 149 In both cases above, the provisioner replaces the sequence `%RAND%` with 150 some randomly-chosen decimal digits. 151 152 Provisioners cannot react directly to remote environment variables such as 153 `TMPDIR` or use functions like `mktemp` because they run on the system where 154 Terraform is running, not on the remote system. Therefore if your remote 155 system doesn't use the filesystem layout expected by these default paths 156 then you can override it using the `script_path` option in your `connection` 157 block: 158 159 ```hcl 160 connection { 161 # ... 162 script_path = "H:/terraform-temp/script_%RAND%.sh" 163 } 164 ``` 165 166 As with the default patterns, provisioners will replace the sequence `%RAND%` 167 with randomly-selected decimal digits, to reduce the likelihood of collisions 168 between multiple provisioners running concurrently. 169 170 If your target system is running Windows, we recommend using forward slashes 171 instead of backslashes, despite the typical convention on Windows, because 172 the Terraform language uses backslash as the quoted string escape character. 173 174 ### Executing Scripts using SSH/SCP 175 176 When using the SSH protocol, provisioners upload their script files using 177 the Secure Copy Protocol (SCP), which requires that the remote system have 178 the `scp` service program installed to act as the server for that protocol. 179 180 Provisioners will pass the chosen script path (after `%RAND%` 181 expansion) directly to the remote `scp` process, which is responsible for 182 interpreting it. With the default configuration of `scp` as distributed with 183 OpenSSH, you can place temporary scripts in the home directory of the remote 184 user by specifying a relative path: 185 186 ```hcl 187 connection { 188 type = "ssh" 189 # ... 190 script_path = "terraform_provisioner_%RAND%.sh" 191 } 192 ``` 193 194 !> **Warning:** In Terraform v1.0 and earlier, the built-in provisioners 195 incorrectly passed the `script_path` value to `scp` through a remote shell and 196 thus allowed it to be subject to arbitrary shell expansion, and thus created an 197 unintended opportunity for remote code execution. Terraform v1.1 and later 198 will now correctly quote and escape the script path to ensure that the 199 remote `scp` process can always interpret it literally. For modules that will 200 be used with Terraform v1.0 and earlier, avoid using untrusted external 201 values as part of the `script_path` argument.