github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/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 arguments available within a `lifecycle` block are `create_before_destroy`,
    34  `prevent_destroy`, `ignore_changes`, and `replace_triggered_by`.
    35  
    36  * `create_before_destroy` (bool) - By default, when Terraform must change
    37    a resource argument that cannot be updated in-place due to
    38    remote API limitations, Terraform will instead destroy the existing object
    39    and then create a new replacement object with the new configured arguments.
    40  
    41    The `create_before_destroy` meta-argument changes this behavior so that
    42    the new replacement object is created _first,_ and the prior object
    43    is destroyed after the replacement is created.
    44  
    45    This is an opt-in behavior because many remote object types have unique
    46    name requirements or other constraints that must be accommodated for
    47    both a new and an old object to exist concurrently. Some resource types
    48    offer special options to append a random suffix onto each object name to
    49    avoid collisions, for example. Terraform CLI cannot automatically activate
    50    such features, so you must understand the constraints for each resource
    51    type before using `create_before_destroy` with it.
    52  
    53    Destroy provisioners of this resource will not run if `create_before_destroy`
    54    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.
    55  
    56  * `prevent_destroy` (bool) - This meta-argument, when set to `true`, will
    57    cause Terraform to reject with an error any plan that would destroy the
    58    infrastructure object associated with the resource, as long as the argument
    59    remains present in the configuration.
    60  
    61    This can be used as a measure of safety against the accidental replacement
    62    of objects that may be costly to reproduce, such as database instances.
    63    However, it will make certain configuration changes impossible to apply,
    64    and will prevent the use of the `terraform destroy` command once such
    65    objects are created, and so this option should be used sparingly.
    66  
    67    Since this argument must be present in configuration for the protection to
    68    apply, note that this setting does not prevent the remote object from
    69    being destroyed if the `resource` block were removed from configuration
    70    entirely: in that case, the `prevent_destroy` setting is removed along
    71    with it, and so Terraform will allow the destroy operation to succeed.
    72  
    73  * `ignore_changes` (list of attribute names) - By default, Terraform detects
    74    any difference in the current settings of a real infrastructure object
    75    and plans to update the remote object to match configuration.
    76  
    77    The `ignore_changes` feature is intended to be used when a resource is
    78    created with references to data that may change in the future, but should
    79    not affect said resource after its creation. In some rare cases, settings
    80    of a remote object are modified by processes outside of Terraform, which
    81    Terraform would then attempt to "fix" on the next run. In order to make
    82    Terraform share management responsibilities of a single object with a
    83    separate process, the `ignore_changes` meta-argument specifies resource
    84    attributes that Terraform should ignore when planning updates to the
    85    associated remote object.
    86  
    87    The arguments corresponding to the given attribute names are considered
    88    when planning a _create_ operation, but are ignored when planning an
    89    _update_. The arguments are the relative address of the attributes in the
    90    resource. Map and list elements can be referenced using index notation,
    91    like `tags["Name"]` and `list[0]` respectively.
    92  
    93    ```hcl
    94    resource "aws_instance" "example" {
    95      # ...
    96  
    97      lifecycle {
    98        ignore_changes = [
    99          # Ignore changes to tags, e.g. because a management agent
   100          # updates these based on some ruleset managed elsewhere.
   101          tags,
   102        ]
   103      }
   104    }
   105    ```
   106  
   107    Instead of a list, the special keyword `all` may be used to instruct
   108    Terraform to ignore _all_ attributes, which means that Terraform can
   109    create and destroy the remote object but will never propose updates to it.
   110  
   111    Only attributes defined by the resource type can be ignored.
   112    `ignore_changes` cannot be applied to itself or to any other meta-arguments.
   113  
   114  * `replace_triggered_by` (list of resource or attribute references) -
   115    _Added in Terraform 1.2._ Replaces the resource when any of the referenced
   116    items change. Supply a list of expressions referencing managed resources,
   117    instances, or instance attributes. When used in a resource that uses `count`
   118    or `for_each`, you can use `count.index` or `each.key` in the expression to
   119    reference specific instances of other resources that are configured with the
   120    same count or collection.
   121  
   122    References trigger replacement in the following conditions:
   123  
   124    - If the reference is to a resource with multiple instances, a plan to
   125      update or replace any instance will trigger replacement.
   126    - If the reference is to a single resource instance, a plan to update or
   127      replace that instance will trigger replacement.
   128    - If the reference is to a single attribute of a resource instance, any
   129      change to the attribute value will trigger replacement.
   130  
   131    You can only reference managed resources in `replace_triggered_by`
   132    expressions. This lets you modify these expressions without forcing
   133    replacement.
   134  
   135    ```hcl
   136    resource "aws_appautoscaling_target" "ecs_target" {
   137      # ...
   138      lifecycle {
   139        replace_triggered_by = [
   140          # Replace `aws_appautoscaling_target` each time this instance of
   141          # the `aws_ecs_service` is replaced.
   142          aws_ecs_service.svc.id
   143        ]
   144      }
   145    }
   146    ```
   147  
   148  ## Custom Condition Checks
   149  
   150  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.
   151  
   152  ```hcl
   153  resource "aws_instance" "example" {
   154    instance_type = "t2.micro"
   155    ami           = "ami-abc123"
   156  
   157    lifecycle {
   158      # The AMI ID must refer to an AMI that contains an operating system
   159      # for the `x86_64` architecture.
   160      precondition {
   161        condition     = data.aws_ami.example.architecture == "x86_64"
   162        error_message = "The selected AMI must be for the x86_64 architecture."
   163      }
   164    }
   165  }
   166  ```
   167  
   168  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.
   169  
   170  Refer to [Custom Conditions](/language/expressions/custom-conditions#preconditions-and-postconditions) for more details.
   171  
   172  ## Literal Values Only
   173  
   174  The `lifecycle` settings all affect how Terraform constructs and traverses
   175  the dependency graph. As a result, only literal values can be used because
   176  the processing happens too early for arbitrary expression evaluation.