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