Manage resource lifecycle
Lifecycle arguments help control the flow of your Terraform operations by creating custom rules for resource creation and destruction. Instead of Terraform managing operations in the built-in dependency graph, lifecycle arguments help minimize potential downtime based on your resource needs as well as protect specific resources from changing or impacting infrastructure.
Prerequisites
This tutorial assumes you are familiar with the standard Terraform workflow. If you are unfamiliar with Terraform, complete the Get Started tutorials first.
For this tutorial, you will need:
- The Terraform CLI, version 0.14 or later.
- AWS Credentials configured for use with Terraform.
- The
awscli
configured.
Create infrastructure
Start by cloning the example repository. This configuration builds an EC2 instance and a security group rule to allow port 8080
access to the instance.
Change into the repository directory.
Confirm your AWS CLI region.
Open the terraform.tfvars
file and edit the region to match your AWS CLI configuration.
Open the main.tf
file and review your configuration. Your two main resources are an EC2 instance and a security group that allows TCP access on port 8080.
Initialize your configuration.
Apply your configuration. Enter yes
when prompted to accept your changes.
When your apply operation completes, run terraform state list
to review the resources managed by Terraform in your state file.
Prevent resource deletion
To prevent destroy operations for specific resources, you can add the prevent_destroy
attribute to your resource definition. This lifecycle option prevents Terraform from accidentally removing critical resources.
Add prevent_destroy
to your EC2 instance.
Run terraform destroy
to observe the behavior.
The prevent_destroy
attribute is useful in situations where a change to an attribute would force a replacement and create downtime.
Create resources before they are destroyed
For changes that may cause downtime but must happen, use the create_before_destroy
attribute to create your new resource before destroying the old resource.
Update your security group rule to allow port 80
access instead of 8080
.
Update your EC2 instance to reflect this change by adding the create_before_destroy
attribute and updating the VM so it runs on port 80
.
Run terraform apply
and observe the changes that force a replacement. Without the create_before_destroy
tag, Terraform would destroy the instance before recreating it, which may lead to downtime. Enter yes
when prompted to accept your changes.
Ignore changes
For changes outside the Terraform workflow that should not impact Terraform operations, use the ignore_changes
argument.
Update the drift_example
tag in the AWS CLI.
Add the ignore_changes
attribute to your lifecycle
block in the EC2 instance.
Run terraform apply
. This apply will refresh your state file with v2
instead of overwriting your tag with v1
as written in your configuration.
Examine your instance in the state file to confirm that your drift_example
tag is v2
.
Clean up your resources
When you are finished with this tutorial, destroy the resources you created. Enter yes
when prompted to confirm your changes.
Next steps
In this tutorial, you learned the different lifecycle management options you can use to prevent resource deletion. You also used lifecycle management to avoid downtime when Terraform recreates your infrastructure and to ignore changes to certain resource attributes.
For more information about Terraform lifecycle management and state drift, review the resources below:
- Drift Management tutorial
- Learn Terraform Import tutorial
- Lifecycle management documentation