github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/website/docs/configuration/providers.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Configuring Providers" 4 sidebar_current: "docs-config-providers" 5 description: |- 6 Providers are responsible in Terraform for managing the lifecycle of a resource: create, read, update, delete. 7 --- 8 9 # Provider Configuration 10 11 Providers are responsible in Terraform for managing the lifecycle 12 of a [resource](/docs/configuration/resources.html): create, 13 read, update, delete. 14 15 Every resource in Terraform is mapped to a provider based 16 on longest-prefix matching. For example the `aws_instance` 17 resource type would map to the `aws` provider (if that exists). 18 19 Most providers require some sort of configuration to provide 20 authentication information, endpoint URLs, etc. Provider configuration 21 blocks are a way to set this information globally for all 22 matching resources. 23 24 This page assumes you're familiar with the 25 [configuration syntax](/docs/configuration/syntax.html) 26 already. 27 28 ## Example 29 30 A provider configuration looks like the following: 31 32 ```hcl 33 provider "aws" { 34 access_key = "foo" 35 secret_key = "bar" 36 region = "us-east-1" 37 } 38 ``` 39 40 ## Description 41 42 The `provider` block configures the provider of the given `NAME`. 43 Multiple provider blocks can be used to configure multiple providers. 44 45 Terraform matches providers to resources by matching two criteria. 46 Both criteria must be matched for a provider to manage a resource: 47 48 - They must share a common prefix. Longest matching prefixes are tried first. 49 For example, `aws_instance` would choose the `aws` provider. 50 51 - The provider must report that it supports the given resource type. Providers 52 internally tell Terraform the list of resources they support. 53 54 Within the block (the `{ }`) is configuration for the resource. 55 The configuration is dependent on the type, and is documented 56 [for each provider](/docs/providers/index.html). 57 58 ## Initialization 59 60 Each time a new provider is added to configuration -- either explicitly via 61 a `provider` block or by adding a resource from that provider -- it's necessary 62 to initialize that provider before use. Initialization downloads and installs 63 the provider's plugin and prepares it to be used. 64 65 Provider initialization is one of the actions of `terraform init`. Running 66 this command will download and initialize any providers that are not already 67 initialized. 68 69 For more information, see 70 [the `terraform init` command](/docs/commands/init.html). 71 72 ## Provider Versions 73 74 Providers are released on a separate rhythm from Terraform itself, and thus 75 have their own version numbers. For production use, it is recommended to 76 constrain the acceptable provider versions via configuration, to ensure that 77 new versions with breaking changes will not be automatically installed by 78 `terraform init` in future. 79 80 When `terraform init` is run _without_ provider version constraints, it 81 prints a suggested version constraint string for each provider: 82 83 ``` 84 The following providers do not have any version constraints in configuration, 85 so the latest version was installed. 86 87 To prevent automatic upgrades to new major versions that may contain breaking 88 changes, it is recommended to add version = "..." constraints to the 89 corresponding provider blocks in configuration, with the constraint strings 90 suggested below. 91 92 * provider.aws: version = "~> 1.0" 93 ``` 94 95 To constrain the provider version as suggested, add a `version` argument to 96 the provider configuration block: 97 98 ```hcl 99 provider "aws" { 100 version = "~> 1.0" 101 102 access_key = "foo" 103 secret_key = "bar" 104 region = "us-east-1" 105 } 106 ``` 107 108 This special argument applies to _all_ providers. 109 [`terraform providers`](/docs/commands/providers.html) can be used to 110 view the specified version constraints for all providers used in the 111 current configuration. 112 113 When `terraform init` is re-run with providers already installed, it will 114 use an already-installed provider that meets the constraints in preference 115 to downloading a new version. To upgrade to the latest acceptable version 116 of each provider, run `terraform init -upgrade`. This command also upgrades 117 to the latest versions of all Terraform modules. 118 119 ## Multiple Provider Instances 120 121 You can define multiple instances of the same provider in order to support 122 multiple regions, multiple hosts, etc. The primary use case for this is 123 utilizing multiple cloud regions. Other use cases include targeting multiple 124 Docker hosts, multiple Consul hosts, etc. 125 126 To define multiple provider instances, repeat the provider configuration 127 multiple times, but set the `alias` field and name the provider. For 128 example: 129 130 ```hcl 131 # The default provider 132 provider "aws" { 133 # ... 134 } 135 136 # West coast region 137 provider "aws" { 138 alias = "west" 139 region = "us-west-2" 140 } 141 ``` 142 143 After naming a provider, you reference it in resources with the `provider` 144 field: 145 146 ```hcl 147 resource "aws_instance" "foo" { 148 provider = "aws.west" 149 150 # ... 151 } 152 ``` 153 154 If a provider isn't specified, then the default provider configuration 155 is used (the provider configuration with no `alias` set). The value of the 156 `provider` field is `TYPE.ALIAS`, such as "aws.west" above. 157 158 ## Syntax 159 160 The full syntax is: 161 162 ```text 163 provider NAME { 164 CONFIG ... 165 [alias = ALIAS] 166 } 167 ``` 168 169 where `CONFIG` is: 170 171 ```text 172 KEY = VALUE 173 174 KEY { 175 CONFIG 176 } 177 ``` 178 179 ## Interpolation 180 Providers support [interpolation syntax](/docs/configuration/interpolation.html) allowing dynamic configuration at run time. 181 182 ```hcl 183 provider "aws" { 184 region = "${var.aws_region}" 185 } 186 ``` 187 188 An exception to this is the special `version` attribute that applies to all `provider` blocks for specifying [provider versions](#provider-versions); interpolation is not supported for provider versions since provider compatibility is a property of the configuration rather than something dynamic, and provider plugin installation happens too early for variables to be resolvable in this context. 189 190 -> **NOTE:** Because providers are one of the first things loaded when Terraform parses the graph, it is not possible to 191 use the output from modules or resources as inputs to the provider. At this time, only 192 [variables](/docs/configuration/variables.html) and [data sources](/docs/configuration/data-sources.html), including 193 [remote state](/docs/providers/terraform/d/remote_state.html) may be used in an interpolation inside a provider stanza. 194 [Local values](/docs/configuration/locals.html) can also be used, but currently may fail when running `terraform destroy`. 195 196 197 ## Third-party Plugins 198 199 At present Terraform can automatically install only the providers distributed 200 by HashiCorp. Third-party providers can be manually installed by placing 201 their plugin executables in one of the following locations depending on the 202 host operating system: 203 204 * On Windows, in the sub-path `terraform.d/plugins` beneath your user's 205 "Application Data" directory. 206 * On all other systems, in the sub-path `.terraform.d/plugins` in your 207 user's home directory. 208 209 `terraform init` will search this directory for additional plugins during 210 plugin initialization. 211 212 The naming scheme for provider plugins is `terraform-provider-NAME-vX.Y.Z`, 213 and Terraform uses the name to understand the name and version of a particular 214 provider binary. Third-party plugins will often be distributed with an 215 appropriate filename already set in the distribution archive so that it can 216 be extracted directly into the plugin directory described above. 217 218 ## Provider Plugin Cache 219 220 By default, `terraform init` downloads plugins into a subdirectory of the 221 working directory so that each working directory is self-contained. As a 222 consequence, if you have multiple configurations that use the same provider 223 then a separate copy of its plugin will be downloaded for each configuration. 224 225 Given that provider plugins can be quite large (on the order of hundreds of 226 megabytes), this default behavior can be inconvenient for those with slow 227 or metered Internet connections. Therefore Terraform optionally allows the 228 use of a local directory as a shared plugin cache, which then allows each 229 distinct plugin binary to be downloaded only once. 230 231 To enable the plugin cache, use the `plugin_cache_dir` setting in 232 [the CLI configuration file](https://www.terraform.io/docs/commands/cli-config.html). 233 For example: 234 235 ```hcl 236 # (Note that the CLI configuration file is _not_ the same as the .tf files 237 # used to configure infrastructure.) 238 239 plugin_cache_dir = "$HOME/.terraform.d/plugin-cache" 240 ``` 241 242 Please note that on Windows it is necessary to use forward slash separators 243 (`/`) rather than the conventional backslash (`\`) since the configuration 244 file parser considers a backslash to begin an escape sequence. 245 246 Setting this in the configuration file is the recommended approach for a 247 persistent setting. Alternatively, the `TF_PLUGIN_CACHE_DIR` environment 248 variable can be used to enable caching or to override an existing cache 249 directory within a particular shell session: 250 251 ```bash 252 export TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache" 253 ``` 254 255 When a plugin cache directory is enabled, the `terraform init` command will 256 still access the plugin distribution server to obtain metadata about which 257 plugins are available, but once a suitable version has been selected it will 258 first check to see if the selected plugin is already available in the cache 259 directory. If so, the already-downloaded plugin binary will be used. 260 261 If the selected plugin is not already in the cache, it will be downloaded 262 into the cache first and then copied from there into the correct location 263 under your current working directory. 264 265 When possible, Terraform will use hardlinks or symlinks to avoid storing 266 a separate copy of a cached plugin in multiple directories. At present, this 267 is not supported on Windows and instead a copy is always created. 268 269 The plugin cache directory must *not* be the third-party plugin directory 270 or any other directory Terraform searches for pre-installed plugins, since 271 the cache management logic conflicts with the normal plugin discovery logic 272 when operating on the same directory. 273 274 Please note that Terraform will never itself delete a plugin from the 275 plugin cache once it's been placed there. Over time, as plugins are upgraded, 276 the cache directory may grow to contain several unused versions which must be 277 manually deleted.