github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/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. 11 12 The [Resource Behavior](/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 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/hashicorp/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 * `replace_triggered_by` (list of resource or attribute references) - 114 _Added in Terraform 1.2._ Replaces the resource when any of the referenced 115 items change. Supply a list of expressions referencing managed resources, 116 instances, or instance attributes. When used in a resource that uses `count` 117 or `for_each`, you can use `count.index` or `each.key` in the expression to 118 reference specific instances of other resources that are configured with the 119 same count or collection. 120 121 References trigger replacement in the following conditions: 122 123 - If the reference is to a resource with multiple instances, a plan to 124 update or replace any instance will trigger replacement. 125 - If the reference is to a single resource instance, a plan to update or 126 replace that instance will trigger replacement. 127 - If the reference is to a single attribute of a resource instance, any 128 change to the attribute value will trigger replacement. 129 130 You can only reference managed resources in `replace_triggered_by` 131 expressions. This lets you modify these expressions without forcing 132 replacement. 133 134 ```hcl 135 resource "aws_appautoscaling_target" "ecs_target" { 136 # ... 137 lifecycle { 138 replace_triggered_by = [ 139 # Replace `aws_appautoscaling_target` each time this instance of 140 # the `aws_ecs_service` is replaced. 141 aws_ecs_service.svc.id 142 ] 143 } 144 } 145 ``` 146 147 ## Custom Condition Checks 148 149 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. 150 151 ```hcl 152 resource "aws_instance" "example" { 153 instance_type = "t2.micro" 154 ami = "ami-abc123" 155 156 lifecycle { 157 # The AMI ID must refer to an AMI that contains an operating system 158 # for the `x86_64` architecture. 159 precondition { 160 condition = data.aws_ami.example.architecture == "x86_64" 161 error_message = "The selected AMI must be for the x86_64 architecture." 162 } 163 } 164 } 165 ``` 166 167 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. 168 169 Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details. 170 171 ## Literal Values Only 172 173 The `lifecycle` settings all affect how Terraform constructs and traverses 174 the dependency graph. As a result, only literal values can be used because 175 the processing happens too early for arbitrary expression evaluation.