forked from terraform-aws-modules/terraform-aws-autoscaling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
191 lines (170 loc) · 6.87 KB
/
main.tf
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#######################
# Launch configuration
#######################
resource "aws_launch_configuration" "this" {
count = var.create_lc ? 1 : 0
name_prefix = "${coalesce(var.lc_name, var.name)}-"
image_id = var.image_id
instance_type = var.instance_type
iam_instance_profile = var.iam_instance_profile
key_name = var.key_name
security_groups = var.security_groups
associate_public_ip_address = var.associate_public_ip_address
user_data = var.user_data
enable_monitoring = var.enable_monitoring
spot_price = var.spot_price
placement_tenancy = var.spot_price == "" ? var.placement_tenancy : ""
ebs_optimized = var.ebs_optimized
dynamic "ebs_block_device" {
for_each = var.ebs_block_device
content {
delete_on_termination = lookup(ebs_block_device.value, "delete_on_termination", null)
device_name = ebs_block_device.value.device_name
encrypted = lookup(ebs_block_device.value, "encrypted", null)
iops = lookup(ebs_block_device.value, "iops", null)
no_device = lookup(ebs_block_device.value, "no_device", null)
snapshot_id = lookup(ebs_block_device.value, "snapshot_id", null)
volume_size = lookup(ebs_block_device.value, "volume_size", null)
volume_type = lookup(ebs_block_device.value, "volume_type", null)
}
}
dynamic "ephemeral_block_device" {
for_each = var.ephemeral_block_device
content {
device_name = ephemeral_block_device.value.device_name
virtual_name = ephemeral_block_device.value.virtual_name
}
}
dynamic "root_block_device" {
for_each = var.root_block_device
content {
delete_on_termination = lookup(root_block_device.value, "delete_on_termination", null)
iops = lookup(root_block_device.value, "iops", null)
volume_size = lookup(root_block_device.value, "volume_size", null)
volume_type = lookup(root_block_device.value, "volume_type", null)
encrypted = lookup(root_block_device.value, "encrypted", null)
}
}
lifecycle {
create_before_destroy = true
}
}
####################
# Autoscaling group
####################
resource "aws_autoscaling_group" "this" {
count = var.create_asg && false == var.create_asg_with_initial_lifecycle_hook ? 1 : 0
name_prefix = "${join(
"-",
compact(
[
coalesce(var.asg_name, var.name),
var.recreate_asg_when_lc_changes ? element(concat(random_pet.asg_name.*.id, [""]), 0) : "",
],
),
)}-"
launch_configuration = var.create_lc ? element(concat(aws_launch_configuration.this.*.name, [""]), 0) : var.launch_configuration
vpc_zone_identifier = var.vpc_zone_identifier
max_size = var.max_size
min_size = var.min_size
desired_capacity = var.desired_capacity
load_balancers = var.load_balancers
health_check_grace_period = var.health_check_grace_period
health_check_type = var.health_check_type
min_elb_capacity = var.min_elb_capacity
wait_for_elb_capacity = var.wait_for_elb_capacity
target_group_arns = var.target_group_arns
default_cooldown = var.default_cooldown
force_delete = var.force_delete
termination_policies = var.termination_policies
suspended_processes = var.suspended_processes
placement_group = var.placement_group
enabled_metrics = var.enabled_metrics
metrics_granularity = var.metrics_granularity
wait_for_capacity_timeout = var.wait_for_capacity_timeout
protect_from_scale_in = var.protect_from_scale_in
service_linked_role_arn = var.service_linked_role_arn
max_instance_lifetime = var.max_instance_lifetime
tags = concat(
[
{
"key" = "Name"
"value" = var.name
"propagate_at_launch" = true
},
],
var.tags,
local.tags_asg_format,
)
lifecycle {
create_before_destroy = true
}
}
################################################
# Autoscaling group with initial lifecycle hook
################################################
resource "aws_autoscaling_group" "this_with_initial_lifecycle_hook" {
count = var.create_asg && var.create_asg_with_initial_lifecycle_hook ? 1 : 0
name_prefix = "${join(
"-",
compact(
[
coalesce(var.asg_name, var.name),
var.recreate_asg_when_lc_changes ? element(concat(random_pet.asg_name.*.id, [""]), 0) : "",
],
),
)}-"
launch_configuration = var.create_lc ? element(aws_launch_configuration.this.*.name, 0) : var.launch_configuration
vpc_zone_identifier = var.vpc_zone_identifier
max_size = var.max_size
min_size = var.min_size
desired_capacity = var.desired_capacity
load_balancers = var.load_balancers
health_check_grace_period = var.health_check_grace_period
health_check_type = var.health_check_type
min_elb_capacity = var.min_elb_capacity
wait_for_elb_capacity = var.wait_for_elb_capacity
target_group_arns = var.target_group_arns
default_cooldown = var.default_cooldown
force_delete = var.force_delete
termination_policies = var.termination_policies
suspended_processes = var.suspended_processes
placement_group = var.placement_group
enabled_metrics = var.enabled_metrics
metrics_granularity = var.metrics_granularity
wait_for_capacity_timeout = var.wait_for_capacity_timeout
protect_from_scale_in = var.protect_from_scale_in
service_linked_role_arn = var.service_linked_role_arn
initial_lifecycle_hook {
name = var.initial_lifecycle_hook_name
lifecycle_transition = var.initial_lifecycle_hook_lifecycle_transition
notification_metadata = var.initial_lifecycle_hook_notification_metadata
heartbeat_timeout = var.initial_lifecycle_hook_heartbeat_timeout
notification_target_arn = var.initial_lifecycle_hook_notification_target_arn
role_arn = var.initial_lifecycle_hook_role_arn
default_result = var.initial_lifecycle_hook_default_result
}
tags = concat(
[
{
"key" = "Name"
"value" = var.name
"propagate_at_launch" = true
},
],
var.tags,
local.tags_asg_format,
)
lifecycle {
create_before_destroy = true
}
}
resource "random_pet" "asg_name" {
count = var.recreate_asg_when_lc_changes ? 1 : 0
separator = "-"
length = 2
keepers = {
# Generate a new pet name each time we switch launch configuration
lc_name = var.create_lc ? element(concat(aws_launch_configuration.this.*.name, [""]), 0) : var.launch_configuration
}
}