-
The
lifecycle
Meta Argument in Terraform is used to control specific aspects of how resources are managed during their lifecycle. -
The
lifecycle
Meta Argument provides fine-grained control over when and how Terraform should create, update, or delete resources. -
lifecycle
Meta Argument options:-
create_before_destroy
:- When set to
true
, this attribute indicates that Terraform should create a new resource before destroying the old one when it needs to replace the resource. - This can help minimize downtime during updates.
- Default option is
false
- When set to
-
prevent_destroy
:- When set to
true
, this attribute prevents the resource from being destroyed or deleted. - This can be useful in protecting critical resources from accidental deletion.
- Default option is
false
- When set to
-
ignore_changes
:- This attribute allows you to specify a list of attributes for which Terraform should ignore changes.
- It's useful to prevent certain attributes from being updated during resource modifications.
- Commonly used when you have some attributes that users are allowed to make changes on AWS console and you want terraform to ignore such changes
- Example : minor version upgrade of RDS instance
- Default option is
false
-
-
Example:
create_before_destroy
00_provider.tfterraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = "us-east-1" default_tags { tags = { Terraform = "yes" Project = "terraform-learning" Owner = "Venkatesh" } } }
resource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" availability_zone = "us-east-1a" # availability_zone = "us-east-1b" tags = { Name = "Linux2023" } # lifecycle { # create_before_destroy = true # } }
-
Lets create an AWS EC2 without
lifecycle
argumentsterraform init
: Initialize terraformterraform validate
: Validate terraform codeterraform fmt
: format terraform codeterraform plan
: Review the terraform planterraform apply
: Create Resources by terraform
-
Now lets try to change the Availability Zone of our EC2 Instance to without
lifecycle
arguments and observe the behaviorresource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" # availability_zone = "us-east-1a" availability_zone = "us-east-1b" tags = { Name = "Linux2023" } # lifecycle { # create_before_destroy = true # } }
-
Lets Execute Terraform commands to understand resource behavior
terraform init
: Initialize terraformterraform validate
: Validate terraform codeterraform fmt
: format terraform codeterraform plan
: Review the terraform planterraform apply
: Create Resources by terraform
-
Now lets try to change the Availability Zone of our EC2 Instance to with
lifecycle
arguments and observe the behaviorresource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" availability_zone = "us-east-1a" # availability_zone = "us-east-1b" tags = { Name = "Linux2023" } lifecycle { create_before_destroy = true } }
-
Example:
prevent_destroy
-
lets use
prevent_destroy
argument and observe the behavior
01_ec2.tfresource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" availability_zone = "us-east-1a" # availability_zone = "us-east-1b" tags = { Name = "Linux2023" } lifecycle { prevent_destroy = true } }
-
Execute
terraform destroy
command and observe the behavior- Terraform shows Error: Instance cannot be destroyed
-
-
Example:
ignore_changes
-
Lets use the tags concept and understand how
ignore_changes
works -
First we are going to manually add
tag
on our AWS EC2 using AWS console ( this tag was not part of terraform code) -
Run terraform plan and see how terraform detects the changes that are made on AWS Console
-
Terraform detects the Manual changes made on AWS console and if we run terraform apply than terraform will remove the manually applied tags on AWS EC2
-
Now, lets add in meta argument
ignore_changes
for tags and see how terraform behaves.resource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" availability_zone = "us-east-1a" # availability_zone = "us-east-1b" tags = { Name = "Linux2023" } lifecycle { ignore_changes = [ tags ] } }
-
Run terraform plan and see how terraform ignores the changes* that are made to tags AWS Console
-
-
ignore_changes
can additionally be used to ignore all the changes for particular resource with option, ignore_changes = all-
Terraform will completely ignore any changes to all attributes of that resource when it attempts to update or modify it
-
Example:
resource "aws_instance" "myec2" { ami = "ami-0df435f331839b2d6" instance_type = "t2.micro" availability_zone = "us-east-1a" # availability_zone = "us-east-1b" tags = { Name = "Linux2023" } lifecycle { ignore_changes = [ all ] } }
-
It's usually a better practice to specify the exact attributes you want to ignore changes for, rather than using "all" to have more fine-grained control over which attributes should be excluded from updates
terraform destroy
: destroy or delete Resources, Cleanup the resources we created-
After you type yes to
terraform destroy
prompt, terraform will start destroying resources -
Once terraform completes the execution you should be able to check on your AWS Console resources are successfully deleted.
-
-
08-05-lifecycle
Folders and files
Name | Name | Last commit date | ||
---|---|---|---|---|
parent directory.. | ||||