github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration-0-11/resources.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Resources - 0.11 Configuration Language" 4 sidebar_current: "docs-conf-old-resources" 5 description: |- 6 The most important thing you'll configure with Terraform are resources. Resources are a component of your infrastructure. It might be some low level component such as a physical server, virtual machine, or container. Or it can be a higher level component such as an email provider, DNS record, or database provider. 7 --- 8 9 # Resources 10 11 -> **Note:** This page is about Terraform 0.11 and earlier. For Terraform 0.12 12 and later, see 13 [Configuration Language: Resources](../configuration/resources.html). 14 15 The most important thing you'll configure with Terraform are 16 resources. Resources are a component of your infrastructure. 17 It might be some low level component such as a physical server, 18 virtual machine, or container. Or it can be a higher level 19 component such as an email provider, DNS record, or database 20 provider. 21 22 This page assumes you're familiar with the 23 [configuration syntax](./syntax.html) 24 already. 25 26 ## Example 27 28 A resource configuration looks like the following: 29 30 ```hcl 31 resource "aws_instance" "web" { 32 ami = "ami-408c7f28" 33 instance_type = "t1.micro" 34 } 35 ``` 36 37 ## Description 38 39 The `resource` block creates a resource of the given `TYPE` (first 40 parameter) and `NAME` (second parameter). The combination of the type 41 and name must be unique. 42 43 Within the block (the `{ }`) is configuration for the resource. The 44 configuration is dependent on the type, and is documented for each 45 resource type in the 46 [providers section](/docs/providers/index.html). 47 48 ### Meta-parameters 49 50 There are **meta-parameters** available to all resources: 51 52 - `count` (int) - The number of identical resources to create. This doesn't 53 apply to all resources. For details on using variables in conjunction with 54 count, see [Using Variables with `count`](#using-variables-with-count) below. 55 56 -> Modules don't currently support the `count` parameter. 57 58 - `depends_on` (list of strings) - Explicit dependencies that this resource has. 59 These dependencies will be created before this resource. For syntax and other 60 details, see the section below on [explicit 61 dependencies](#explicit-dependencies). 62 63 - `provider` (string) - The name of a specific provider to use for this 64 resource. The name is in the format of `TYPE.ALIAS`, for example, `aws.west`. 65 Where `west` is set using the `alias` attribute in a provider. See [multiple 66 provider instances](#multiple-provider-instances). 67 68 - `lifecycle` (configuration block) - Customizes the lifecycle behavior of the 69 resource. The specific options are documented below. 70 71 The `lifecycle` block allows the following keys to be set: 72 73 - `create_before_destroy` (bool) - This flag is used to ensure the replacement 74 of a resource is created before the original instance is destroyed. As an 75 example, this can be used to create an new DNS record before removing an old 76 record. 77 78 - `prevent_destroy` (bool) - This flag provides extra protection against the 79 destruction of a given resource. When this is set to `true`, any plan that 80 includes a destroy of this resource will return an error message. 81 82 - `ignore_changes` (list of strings) - Customizes how diffs are evaluated for 83 resources, allowing individual attributes to be ignored through changes. As 84 an example, this can be used to ignore dynamic changes to the resource from 85 external resources. Other meta-parameters cannot be ignored. 86 87 ~> Ignored attribute names can be matched by their name, not state ID. 88 For example, if an `aws_route_table` has two routes defined and the 89 `ignore_changes` list contains "route", both routes will be ignored. 90 Additionally you can also use a single entry with a wildcard (e.g. `"*"`) 91 which will match all attribute names. Using a partial string together 92 with a wildcard (e.g. `"rout*"`) is **not** supported. 93 94 -> Interpolations are not currently supported in the `lifecycle` configuration block (see [issue #3116](https://github.com/hashicorp/terraform/issues/3116)) 95 96 ### Timeouts 97 98 Individual Resources may provide a `timeouts` block to enable users to configure the 99 amount of time a specific operation is allowed to take before being considered 100 an error. For example, the 101 [aws_db_instance](/docs/providers/aws/r/db_instance.html#timeouts) 102 resource provides configurable timeouts for the 103 `create`, `update`, and `delete` operations. Any Resource that provides Timeouts 104 will document the default values for that operation, and users can overwrite 105 them in their configuration. 106 107 Example overwriting the `create` and `delete` timeouts: 108 109 ```hcl 110 resource "aws_db_instance" "timeout_example" { 111 allocated_storage = 10 112 engine = "mysql" 113 engine_version = "5.6.17" 114 instance_class = "db.t1.micro" 115 name = "mydb" 116 117 # ... 118 119 timeouts { 120 create = "60m" 121 delete = "2h" 122 } 123 } 124 ``` 125 126 Individual Resources must opt-in to providing configurable Timeouts, and 127 attempting to configure the timeout for a Resource that does not support 128 Timeouts, or overwriting a specific action that the Resource does not specify as 129 an option, will result in an error. Valid units of time are `s`, `m`, `h`. 130 131 ### Explicit Dependencies 132 133 Terraform ensures that dependencies are successfully created before a 134 resource is created. During a destroy operation, Terraform ensures that 135 this resource is destroyed before its dependencies. 136 137 A resource automatically depends on anything it references via 138 [interpolations](./interpolation.html). The automatically 139 determined dependencies are all that is needed most of the time. You can also 140 use the `depends_on` parameter to explicitly define a list of additional 141 dependencies. 142 143 The primary use case of explicit `depends_on` is to depend on a _side effect_ 144 of another operation. For example: if a provisioner creates a file, and your 145 resource reads that file, then there is no interpolation reference for Terraform 146 to automatically connect the two resources. However, there is a causal 147 ordering that needs to be represented. This is an ideal case for `depends_on`. 148 In most cases, however, `depends_on` should be avoided and Terraform should 149 be allowed to determine dependencies automatically. 150 151 The syntax of `depends_on` is a list of resources and modules: 152 153 - Resources are `TYPE.NAME`, such as `aws_instance.web`. 154 - Modules are `module.NAME`, such as `module.foo`. 155 156 When a resource depends on a module, _everything_ in that module must be 157 created before the resource is created. 158 159 An example of a resource depending on both a module and resource is shown 160 below. Note that `depends_on` can contain any number of dependencies: 161 162 ```hcl 163 resource "aws_instance" "web" { 164 depends_on = ["aws_instance.leader", "module.vpc"] 165 } 166 ``` 167 168 -> **Use sparingly!** `depends_on` is rarely necessary. 169 In almost every case, Terraform's automatic dependency system is the best-case 170 scenario by having your resources depend only on what they explicitly use. 171 Please think carefully before you use `depends_on` to determine if Terraform 172 could automatically do this a better way. 173 174 ### Connection block 175 176 Within a resource, you can optionally have a **connection block**. 177 Connection blocks describe to Terraform how to connect to the 178 resource for 179 [provisioning](/docs/provisioners/index.html). This block doesn't 180 need to be present if you're using only local provisioners, or 181 if you're not provisioning at all. 182 183 Resources provide some data on their own, such as an IP address, 184 but other data must be specified by the user. 185 186 The full list of settings that can be specified are listed on 187 the [provisioner connection page](/docs/provisioners/connection.html). 188 189 ### Provisioners 190 191 Within a resource, you can specify zero or more **provisioner 192 blocks**. Provisioner blocks configure 193 [provisioners](/docs/provisioners/index.html). 194 195 Within the provisioner block is provisioner-specific configuration, 196 much like resource-specific configuration. 197 198 Provisioner blocks can also contain a connection block 199 (documented above). This connection block can be used to 200 provide more specific connection info for a specific provisioner. 201 An example use case might be to use a different user to log in 202 for a single provisioner. 203 204 ## Using Variables With `count` 205 206 When declaring multiple instances of a resource using [`count`](#count), it is 207 common to want each instance to have a different value for a given attribute. 208 209 You can use the `${count.index}` 210 [interpolation](./interpolation.html) along with a map 211 [variable](./variables.html) to accomplish this. 212 213 For example, here's how you could create three [AWS 214 Instances](/docs/providers/aws/r/instance.html) each with their own 215 static IP address: 216 217 ```hcl 218 variable "instance_ips" { 219 default = { 220 "0" = "10.11.12.100" 221 "1" = "10.11.12.101" 222 "2" = "10.11.12.102" 223 } 224 } 225 226 resource "aws_instance" "app" { 227 count = "3" 228 private_ip = "${lookup(var.instance_ips, count.index)}" 229 # ... 230 } 231 ``` 232 233 To reference a particular instance of a resource you can use `resource.foo.*.id[#]` where `#` is the index number of the instance. 234 235 For example, to create a list of all [AWS subnet](/docs/providers/aws/r/subnet.html) ids vs referencing a specific subnet in the list you can use this syntax: 236 237 ```hcl 238 resource "aws_vpc" "foo" { 239 cidr_block = "198.18.0.0/16" 240 } 241 242 resource "aws_subnet" "bar" { 243 count = 2 244 vpc_id = "${aws_vpc.foo.id}" 245 cidr_block = "${cidrsubnet(aws_vpc.foo.cidr_block, 8, count.index)}" 246 } 247 248 output "vpc_id" { 249 value = "${aws_vpc.foo.id}" 250 } 251 252 output "all_subnet_ids" { 253 value = "${aws_subnet.bar.*.id}" 254 } 255 256 output "subnet_id_0" { 257 value = "${aws_subnet.bar.*.id[0]}" 258 } 259 260 output "subnet_id_1" { 261 value = "${aws_subnet.bar.*.id[1]}" 262 } 263 ``` 264 265 ## Multiple Provider Instances 266 267 By default, a resource targets the provider based on its type. For example 268 an `aws_instance` resource will target the "aws" provider. As of Terraform 269 0.5.0, a resource can target any provider by name. 270 271 The primary use case for this is to target a specific configuration of 272 a provider that is configured multiple times to support multiple regions, etc. 273 274 To target another provider, set the `provider` field: 275 276 ```hcl 277 resource "aws_instance" "foo" { 278 provider = "aws.west" 279 280 # ... 281 } 282 ``` 283 284 The value of the field should be `TYPE` or `TYPE.ALIAS`. The `ALIAS` value 285 comes from the `alias` field value when configuring the 286 [provider](./providers.html). 287 288 ```hcl 289 provider "aws" { 290 alias = "west" 291 292 # ... 293 } 294 ``` 295 296 If no `provider` field is specified, the default provider is used. 297 298 ## Syntax 299 300 The full syntax is: 301 302 ```text 303 resource TYPE NAME { 304 CONFIG ... 305 [count = COUNT] 306 [depends_on = [NAME, ...]] 307 [provider = PROVIDER] 308 309 [LIFECYCLE] 310 311 [CONNECTION] 312 [PROVISIONER ...] 313 } 314 ``` 315 316 where `CONFIG` is: 317 318 ```text 319 KEY = VALUE 320 321 KEY { 322 CONFIG 323 } 324 ``` 325 326 where `LIFECYCLE` is: 327 328 ```text 329 lifecycle { 330 [create_before_destroy = true|false] 331 [prevent_destroy = true|false] 332 [ignore_changes = [ATTRIBUTE NAME, ...]] 333 } 334 ``` 335 336 where `CONNECTION` is: 337 338 ```text 339 connection { 340 KEY = VALUE 341 ... 342 } 343 ``` 344 345 where `PROVISIONER` is: 346 347 ```text 348 provisioner NAME { 349 CONFIG ... 350 351 [when = "create"|"destroy"] 352 [on_failure = "continue"|"fail"] 353 354 [CONNECTION] 355 } 356 ```