github.com/hugorut/terraform@v1.1.3/website/docs/language/resources/behavior.mdx (about) 1 --- 2 page_title: Resource Behavior - Configuration Language 3 description: >- 4 Learn how Terraform uses resource blocks to create infrastructure objects. 5 Also learn about resource dependencies and how to access resource attributes. 6 --- 7 8 # Resource Behavior 9 10 A `resource` block declares that you want a particular infrastructure object 11 to exist with the given settings. If you are writing a new configuration for 12 the first time, the resources it defines will exist _only_ in the configuration, 13 and will not yet represent real infrastructure objects in the target platform. 14 15 _Applying_ a Terraform configuration is the process of creating, updating, 16 and destroying real infrastructure objects in order to make their settings 17 match the configuration. 18 19 ## How Terraform Applies a Configuration 20 21 When Terraform creates a new infrastructure object represented by a `resource` 22 block, the identifier for that real object is saved in Terraform's 23 [state](/language/state), allowing it to be updated and destroyed 24 in response to future changes. For resource blocks that already have an 25 associated infrastructure object in the state, Terraform compares the 26 actual configuration of the object with the arguments given in the 27 configuration and, if necessary, updates the object to match the configuration. 28 29 In summary, applying a Terraform configuration will: 30 31 - _Create_ resources that exist in the configuration but are not associated with a real infrastructure object in the state. 32 - _Destroy_ resources that exist in the state but no longer exist in the configuration. 33 - _Update in-place_ resources whose arguments have changed. 34 - _Destroy and re-create_ resources whose arguments have changed but which cannot be updated in-place due to remote API limitations. 35 36 This general behavior applies for all resources, regardless of type. The 37 details of what it means to create, update, or destroy a resource are different 38 for each resource type, but this standard set of verbs is common across them 39 all. 40 41 The meta-arguments within `resource` blocks, documented in the 42 sections below, allow some details of this standard resource behavior to be 43 customized on a per-resource basis. 44 45 ## Accessing Resource Attributes 46 47 [Expressions](/language/expressions) within a Terraform module can access 48 information about resources in the same module, and you can use that information 49 to help configure other resources. Use the `<RESOURCE TYPE>.<NAME>.<ATTRIBUTE>` 50 syntax to reference a resource attribute in an expression. 51 52 In addition to arguments specified in the configuration, resources often provide 53 read-only attributes with information obtained from the remote API; this often 54 includes things that can't be known until the resource is created, like the 55 resource's unique random ID. 56 57 Many providers also include [data sources](/language/data-sources), 58 which are a special type of resource used only for looking up information. 59 60 For a list of the attributes a resource or data source type provides, consult 61 its documentation; these are generally included in a second list below its list 62 of configurable arguments. 63 64 For more information about referencing resource attributes in expressions, see 65 [Expressions: References to Resource Attributes](/language/expressions/references#references-to-resource-attributes). 66 67 ## Resource Dependencies 68 69 Most resources in a configuration don't have any particular relationship, and 70 Terraform can make changes to several unrelated resources in parallel. 71 72 However, some resources must be processed after other specific resources; 73 sometimes this is because of how the resource works, and sometimes the 74 resource's configuration just requires information generated by another 75 resource. 76 77 Most resource dependencies are handled automatically. Terraform analyses any 78 [expressions](/language/expressions) within a `resource` block to find references 79 to other objects, and treats those references as implicit ordering requirements 80 when creating, updating, or destroying resources. Since most resources with 81 behavioral dependencies on other resources also refer to those resources' data, 82 it's usually not necessary to manually specify dependencies between resources. 83 84 However, some dependencies cannot be recognized implicitly in configuration. For 85 example, if Terraform must manage access control policies _and_ take actions 86 that require those policies to be present, there is a hidden dependency between 87 the access policy and a resource whose creation depends on it. In these rare 88 cases, 89 [the `depends_on` meta-argument](/language/meta-arguments/depends_on) 90 can explicitly specify a dependency. 91 92 ## Local-only Resources 93 94 While most resource types correspond to an infrastructure object type that 95 is managed via a remote network API, there are certain specialized resource 96 types that operate only within Terraform itself, calculating some results and 97 saving those results in the state for future use. 98 99 For example, local-only resource types exist for 100 [generating private keys](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/private_key), 101 [issuing self-signed TLS certificates](https://registry.terraform.io/providers/hashicorp/tls/latest/docs/resources/self_signed_cert), 102 and even [generating random ids](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id). 103 While these resource types often have a more marginal purpose than those 104 managing "real" infrastructure objects, they can be useful as glue to help 105 connect together other resources. 106 107 The behavior of local-only resources is the same as all other resources, but 108 their result data exists only within the Terraform state. "Destroying" such 109 a resource means only to remove it from the state, discarding its data.