Lesson 2 of 2 Resource Dependencies and Lifecycle Rules

The lifecycle Meta-Argument

The lifecycle meta-argument changes how Terraform handles a resource's creation, replacement and destruction. This lesson covers create_before_destroy in depth and the other lifecycle settings.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the default replacement order and how create_before_destroy changes it.
  • Describe prevent_destroy, ignore_changes and replace_triggered_by.
  • Identify the constraints that create_before_destroy places on a resource.

Default replacement order

When an argument that cannot be updated in place changes, such as an instance's image, Terraform must replace the object. By default it destroys the old object first and then creates the new one, shown as -/+ in the plan. For many resources this causes an outage between the destroy and the create, and if the create fails, nothing is left running.

True or false

Replacement order

Exam skills 4.6.3

Check what happens by default when a resource must be replaced.

Log in to try this activity Interactive activities are free with an account.

create_before_destroy

create_before_destroy = true reverses the order. Terraform creates the replacement, updates any dependent resources to point at it, and only then destroys the old object. The plan shows +/-.

resource "aws_launch_template" "web" {
  name_prefix   = "web-"
  image_id      = var.ami_id
  instance_type = "t3.micro"

  lifecycle {
    create_before_destroy = true
  }
}

Because both objects exist at the same time, the resource must allow duplicates. Resources whose names must be unique, such as a launch template with a fixed name, would fail with a conflict, which is why the example uses name_prefix so that the provider generates a unique name. Quotas must also allow the extra object for a short time.

Terraform propagates create_before_destroy to the resources that this resource depends on: if a resource with the setting depends on another that is also being replaced, that dependency is replaced create-before-destroy as well, because the new object must exist before the old one is removed. This is why the setting can appear in a plan for resources whose own blocks do not set it.

  1. Create the replacement

    The new object is created while the old one keeps running.

  2. Update dependents

    Resources that reference the object are updated to use the new one.

  3. Destroy the original

    The old object is destroyed last.

The following diagram compares the default replacement order with create_before_destroy.

Replacing a certificate that a listener still uses. The default order destroys the old certificate first, which fails because it is attached. With create_before_destroy, Terraform creates the new certificate, updates the listener to reference it, and then deletes the old one. Certificates are not uniquely named, so both can exist at once.
A certificate attached to a listener is a typical case.
Scenario questions

Replacing without an outage

Exam skills 4.6.3

Apply create_before_destroy correctly.

Log in to try this activity Interactive activities are free with an account.

Other lifecycle settings

The lifecycle block has four more settings, plus the precondition and postcondition blocks taught in the next module. All values must be literals, because Terraform processes them before evaluating expressions.

SettingEffectTypical use
prevent_destroy = trueAny plan that would destroy the resource failsProduction databases and state buckets
ignore_changes = [attr, ...] or allChanges to the listed attributes are not plannedAttributes changed by another process, such as an autoscaler's desired capacity
replace_triggered_by = [ref, ...]Replace this resource when a referenced resource or attribute changesRecreate instances when their launch configuration changes
create_before_destroy = trueCreate the replacement before destroying the originalReduce downtime for replaceable resources
resource "aws_autoscaling_group" "web" {
  # ...
  desired_capacity = 2

  lifecycle {
    ignore_changes = [desired_capacity]
  }
}

resource "aws_instance" "app" {
  # ...
  lifecycle {
    replace_triggered_by = [terraform_data.app_version]
  }
}

resource "terraform_data" "app_version" {
  input = var.app_version
}

replace_triggered_by accepts references to managed resources or their attributes. To trigger replacement from a variable, as here, wrap the variable in a terraform_data resource, whose input change triggers the replacement.

ignore_changes is one-way

ignore_changes does not stop the change from happening outside Terraform; it only stops Terraform from planning to revert it. It also ignores changes made in the configuration for those attributes after creation.

Match the pairs

lifecycle settings

Exam skills 4.6.3

Match each requirement with the lifecycle setting that meets it.

Log in to try this activity Interactive activities are free with an account.
Worked example

Zero-downtime certificate rotation

A load balancer listener uses a TLS certificate managed by Terraform. When the certificate's domain list changes, the plan destroys the certificate first, and the destroy fails because the listener still uses it.

The decisive fact is the default destroy-then-create order.

The certificate cannot be deleted while it is attached, so the default order cannot work. Adding lifecycle { create_before_destroy = true } to the certificate resource makes Terraform create the new certificate, update the listener to reference it, and then delete the old certificate, which is no longer in use. Certificates are not uniquely named, so both can exist at once.

Check yourself

Why can create_before_destroy fail for a resource with a fixed, unique name?

Show answer

Because the replacement is created while the original still exists, so both objects would need the same name.

Exam Tips

Remember for the exam

  • Default replacement: destroy then create (-/+).
  • create_before_destroy: create, update dependents, destroy (+/-).
  • create_before_destroy needs resources that allow duplicates, and it propagates to dependencies.
  • prevent_destroy, ignore_changes and replace_triggered_by complete the lifecycle settings.