Repository navigation
Expand file tree
/
Copy pathlifecycle.tf
More file actions
70 lines (61 loc) · 2.02 KB
/
Copy pathlifecycle.tf
File metadata and controls
70 lines (61 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# ============================================================
# Lifecycle Meta-Argument — Control resource behavior
# ============================================================
# Every resource supports a lifecycle block that changes how
# Terraform creates, updates, and destroys it.
# --- Example 1: prevent_destroy ---
# Terraform will refuse to destroy this resource (even with terraform destroy).
# You must remove the lifecycle block first to destroy it.
# Great for databases, S3 buckets with important data, etc.
# resource "aws_db_instance" "production" {
# identifier = "prod-db"
# engine = "postgres"
# instance_class = "db.t3.micro"
#
# lifecycle {
# prevent_destroy = true
# }
# }
# --- Example 2: ignore_changes ---
# Terraform ignores changes to specified attributes.
# Useful when something external (autoscaler, CI/CD) modifies the resource.
# resource "aws_instance" "app" {
# ami = "ami-0c55b159cbfafe1f0"
# instance_type = "t2.micro"
#
# tags = {
# Name = "app-server"
# }
#
# lifecycle {
# # Ignore tag changes made by AWS or other tools
# ignore_changes = [tags]
# }
# }
# --- Example 3: create_before_destroy ---
# Create the replacement resource BEFORE destroying the old one.
# Prevents downtime during resource replacement.
# resource "aws_instance" "web" {
# ami = "ami-0c55b159cbfafe1f0"
# instance_type = "t2.micro"
#
# lifecycle {
# create_before_destroy = true
# }
# }
# --- Example 4: replace_triggered_by ---
# Force replacement when another resource changes.
# resource "aws_instance" "app" {
# ami = "ami-0c55b159cbfafe1f0"
# instance_type = "t2.micro"
#
# lifecycle {
# # Recreate this instance whenever the security group changes
# replace_triggered_by = [aws_security_group.app.id]
# }
# }
# --- Common patterns ---
# Database: prevent_destroy = true
# ASG targets: ignore_changes = [desired_capacity]
# Zero-downtime: create_before_destroy = true
# Config reload: replace_triggered_by = [resource.id]