github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/language/meta-arguments/lifecycle.html.md (about) 1 --- 2 layout: "language" 3 page_title: "The lifecycle Meta-Argument - Configuration Language" 4 description: "The meta-arguments in a lifecycle block allow you to customize resource behavior." 5 --- 6 7 # The `lifecycle` Meta-Argument 8 9 The general lifecycle for resources is described in the 10 [Resource Behavior](/docs/language/resources/behavior.html) page. Some details of 11 that behavior can be customized using the special nested `lifecycle` block 12 within a resource block body: 13 14 ```hcl 15 resource "azurerm_resource_group" "example" { 16 # ... 17 18 lifecycle { 19 create_before_destroy = true 20 } 21 } 22 ``` 23 24 ## Syntax and Arguments 25 26 `lifecycle` is a nested block that can appear within a resource block. 27 The `lifecycle` block and its contents are meta-arguments, available 28 for all `resource` blocks regardless of type. 29 30 The following arguments can be used within a `lifecycle` block: 31 32 * `create_before_destroy` (bool) - By default, when Terraform must change 33 a resource argument that cannot be updated in-place due to 34 remote API limitations, Terraform will instead destroy the existing object 35 and then create a new replacement object with the new configured arguments. 36 37 The `create_before_destroy` meta-argument changes this behavior so that 38 the new replacement object is created _first,_ and the prior object 39 is destroyed after the replacement is created. 40 41 This is an opt-in behavior because many remote object types have unique 42 name requirements or other constraints that must be accommodated for 43 both a new and an old object to exist concurrently. Some resource types 44 offer special options to append a random suffix onto each object name to 45 avoid collisions, for example. Terraform CLI cannot automatically activate 46 such features, so you must understand the constraints for each resource 47 type before using `create_before_destroy` with it. 48 49 * `prevent_destroy` (bool) - This meta-argument, when set to `true`, will 50 cause Terraform to reject with an error any plan that would destroy the 51 infrastructure object associated with the resource, as long as the argument 52 remains present in the configuration. 53 54 This can be used as a measure of safety against the accidental replacement 55 of objects that may be costly to reproduce, such as database instances. 56 However, it will make certain configuration changes impossible to apply, 57 and will prevent the use of the `iaas-rpc.destroy` command once such 58 objects are created, and so this option should be used sparingly. 59 60 Since this argument must be present in configuration for the protection to 61 apply, note that this setting does not prevent the remote object from 62 being destroyed if the `resource` block were removed from configuration 63 entirely: in that case, the `prevent_destroy` setting is removed along 64 with it, and so Terraform will allow the destroy operation to succeed. 65 66 * `ignore_changes` (list of attribute names) - By default, Terraform detects 67 any difference in the current settings of a real infrastructure object 68 and plans to update the remote object to match configuration. 69 70 The `ignore_changes` feature is intended to be used when a resource is 71 created with references to data that may change in the future, but should 72 not affect said resource after its creation. In some rare cases, settings 73 of a remote object are modified by processes outside of Terraform, which 74 Terraform would then attempt to "fix" on the next run. In order to make 75 Terraform share management responsibilities of a single object with a 76 separate process, the `ignore_changes` meta-argument specifies resource 77 attributes that Terraform should ignore when planning updates to the 78 associated remote object. 79 80 The arguments corresponding to the given attribute names are considered 81 when planning a _create_ operation, but are ignored when planning an 82 _update_. The arguments are the relative address of the attributes in the 83 resource. Map and list elements can be referenced using index notation, 84 like `tags["Name"]` and `list[0]` respectively. 85 86 ```hcl 87 resource "aws_instance" "example" { 88 # ... 89 90 lifecycle { 91 ignore_changes = [ 92 # Ignore changes to tags, e.g. because a management agent 93 # updates these based on some ruleset managed elsewhere. 94 tags, 95 ] 96 } 97 } 98 ``` 99 100 Instead of a list, the special keyword `all` may be used to instruct 101 Terraform to ignore _all_ attributes, which means that Terraform can 102 create and destroy the remote object but will never propose updates to it. 103 104 Only attributes defined by the resource type can be ignored. 105 `ignore_changes` cannot be applied to itself or to any other meta-arguments. 106 107 ## Literal Values Only 108 109 The `lifecycle` settings all affect how Terraform constructs and traverses 110 the dependency graph. As a result, only literal values can be used because 111 the processing happens too early for arbitrary expression evaluation.