github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/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](/terraform/tutorials/state/resource-lifecycle?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial. 11 12 The [Resource Behavior](/terraform/language/resources/behavior) page describes the general lifecycle for resources. Some details of 13 that behavior can be customized using the special nested `lifecycle` block 14 within a resource block body: 15 16 ```hcl 17 resource "azurerm_resource_group" "example" { 18 # ... 19 20 lifecycle { 21 create_before_destroy = true 22 } 23 } 24 ``` 25 26 ## Syntax and Arguments 27 28 `lifecycle` is a nested block that can appear within a resource block. 29 The `lifecycle` block and its contents are meta-arguments, available 30 for all `resource` blocks regardless of type. 31 32 The arguments available within a `lifecycle` block are `create_before_destroy`, 33 `prevent_destroy`, `ignore_changes`, and `replace_triggered_by`. 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 Note that Terraform propagates and applies the `create_before_destroy` meta-attribute 53 behaviour to all resource dependencies. For example, if `create_before_destroy` is enabled on resource A but not on resource B, but resource A is dependent on resource B, then Terraform enables `create_before_destroy` for resource B 54 implicitly by default and stores it to the state file. You cannot override `create_before_destroy` 55 to `false` on resource B because that would imply dependency cycles in the graph. 56 57 Destroy provisioners of this resource do not run if `create_before_destroy` 58 is set to `true`. This [GitHub issue](https://github.com/hashicorp/terraform/issues/13549) contains more details. 59 60 * `prevent_destroy` (bool) - This meta-argument, when set to `true`, will 61 cause Terraform to reject with an error any plan that would destroy the 62 infrastructure object associated with the resource, as long as the argument 63 remains present in the configuration. 64 65 This can be used as a measure of safety against the accidental replacement 66 of objects that may be costly to reproduce, such as database instances. 67 However, it will make certain configuration changes impossible to apply, 68 and will prevent the use of the `terraform destroy` command once such 69 objects are created, and so this option should be used sparingly. 70 71 Since this argument must be present in configuration for the protection to 72 apply, note that this setting does not prevent the remote object from 73 being destroyed if the `resource` block were removed from configuration 74 entirely: in that case, the `prevent_destroy` setting is removed along 75 with it, and so Terraform will allow the destroy operation to succeed. 76 77 * `ignore_changes` (list of attribute names) - By default, Terraform detects 78 any difference in the current settings of a real infrastructure object 79 and plans to update the remote object to match configuration. 80 81 The `ignore_changes` feature is intended to be used when a resource is 82 created with references to data that may change in the future, but should 83 not affect said resource after its creation. In some rare cases, settings 84 of a remote object are modified by processes outside of Terraform, which 85 Terraform would then attempt to "fix" on the next run. In order to make 86 Terraform share management responsibilities of a single object with a 87 separate process, the `ignore_changes` meta-argument specifies resource 88 attributes that Terraform should ignore when planning updates to the 89 associated remote object. 90 91 The arguments corresponding to the given attribute names are considered 92 when planning a _create_ operation, but are ignored when planning an 93 _update_. The arguments are the relative address of the attributes in the 94 resource. Map and list elements can be referenced using index notation, 95 like `tags["Name"]` and `list[0]` respectively. 96 97 ```hcl 98 resource "aws_instance" "example" { 99 # ... 100 101 lifecycle { 102 ignore_changes = [ 103 # Ignore changes to tags, e.g. because a management agent 104 # updates these based on some ruleset managed elsewhere. 105 tags, 106 ] 107 } 108 } 109 ``` 110 111 Instead of a list, the special keyword `all` may be used to instruct 112 Terraform to ignore _all_ attributes, which means that Terraform can 113 create and destroy the remote object but will never propose updates to it. 114 115 Only attributes defined by the resource type can be ignored. 116 `ignore_changes` cannot be applied to itself or to any other meta-arguments. 117 118 * `replace_triggered_by` (list of resource or attribute references) - 119 _Added in Terraform 1.2._ Replaces the resource when any of the referenced 120 items change. Supply a list of expressions referencing managed resources, 121 instances, or instance attributes. When used in a resource that uses `count` 122 or `for_each`, you can use `count.index` or `each.key` in the expression to 123 reference specific instances of other resources that are configured with the 124 same count or collection. 125 126 References trigger replacement in the following conditions: 127 128 - If the reference is to a resource with multiple instances, a plan to 129 update or replace any instance will trigger replacement. 130 - If the reference is to a single resource instance, a plan to update or 131 replace that instance will trigger replacement. 132 - If the reference is to a single attribute of a resource instance, any 133 change to the attribute value will trigger replacement. 134 135 You can only reference managed resources in `replace_triggered_by` 136 expressions. This lets you modify these expressions without forcing 137 replacement. 138 139 ```hcl 140 resource "aws_appautoscaling_target" "ecs_target" { 141 # ... 142 lifecycle { 143 replace_triggered_by = [ 144 # Replace `aws_appautoscaling_target` each time this instance of 145 # the `aws_ecs_service` is replaced. 146 aws_ecs_service.svc.id 147 ] 148 } 149 } 150 ``` 151 152 `replace_triggered_by` allows only resource addresses because the decision is based on the planned actions for all of the given resources. Plain values such as local values or input variables do not have planned actions of their own, but you can treat them with a resource-like lifecycle by using them with [the `terraform_data` resource type](/terraform/language/resources/terraform-data). 153 154 ## Custom Condition Checks 155 156 You can add `precondition` and `postcondition` blocks with a `lifecycle` block to specify assumptions and guarantees about how resources and data sources operate. The following examples creates a precondition that checks whether the AMI is properly configured. 157 158 ```hcl 159 resource "aws_instance" "example" { 160 instance_type = "t2.micro" 161 ami = "ami-abc123" 162 163 lifecycle { 164 # The AMI ID must refer to an AMI that contains an operating system 165 # for the `x86_64` architecture. 166 precondition { 167 condition = data.aws_ami.example.architecture == "x86_64" 168 error_message = "The selected AMI must be for the x86_64 architecture." 169 } 170 } 171 } 172 ``` 173 174 Custom conditions can help capture assumptions, helping future maintainers understand the configuration design and intent. They also return useful information about errors earlier and in context, helping consumers more easily diagnose issues in their configurations. 175 176 Refer to [Custom Conditions](/terraform/language/expressions/custom-conditions#preconditions-and-postconditions) for more details. 177 178 ## Literal Values Only 179 180 The `lifecycle` settings all affect how Terraform constructs and traverses 181 the dependency graph. As a result, only literal values can be used because 182 the processing happens too early for arbitrary expression evaluation.