forked from terraform-aws-modules/terraform-aws-autoscaling
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
221 lines (193 loc) · 6.92 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#######################
# Launch template
#######################
resource "aws_launch_template" "this" {
count = var.create_lt ? 1 : 0
name_prefix = "${coalesce(var.lt_name, var.name)}-"
image_id = var.image_id
instance_type = var.instance_type
key_name = var.key_name
user_data = base64encode(var.user_data)
ebs_optimized = var.ebs_optimized
dynamic "block_device_mappings" {
for_each = var.block_device_mappings
content {
device_name = lookup(block_device_mappings.value, "device_name", null)
no_device = lookup(block_device_mappings.value, "no_device", null)
virtual_name = lookup(block_device_mappings.value, "virtual_name", null)
dynamic "ebs" {
for_each = lookup(block_device_mappings.value, "ebs", [])
content {
delete_on_termination = lookup(ebs.value, "delete_on_termination", null)
encrypted = lookup(ebs.value, "encrypted", null)
iops = lookup(ebs.value, "iops", null)
kms_key_id = lookup(ebs.value, "kms_key_id", null)
snapshot_id = lookup(ebs.value, "snapshot_id", null)
volume_size = lookup(ebs.value, "volume_size", null)
volume_type = lookup(ebs.value, "volume_type", null)
}
}
}
}
iam_instance_profile {
arn = var.iam_instance_profile
}
network_interfaces {
description = coalesce(var.lt_name, var.name)
device_index = 0
associate_public_ip_address = var.associate_public_ip_address
delete_on_termination = true
security_groups = var.security_groups
}
monitoring {
enabled = var.enable_monitoring
}
placement {
tenancy = var.spot_price == "" ? "default" : var.placement_tenancy
}
tag_specifications {
resource_type = "instance"
tags = var.tags_as_map
}
tag_specifications {
resource_type = "volume"
tags = var.tags_as_map
}
# Create block when var.spot_price is set, else make it run on-demand to mimic behaviour of v3.x
dynamic "instance_market_options" {
for_each = var.spot_price != "" ? [var.spot_price] : []
content {
market_type = "spot"
spot_options {
max_price = instance_market_options.value
}
}
}
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_lt_changes ? element(concat(random_pet.asg_name.*.id, [""]), 0) : "",
],
),
)}-"
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
launch_template {
id = var.create_lt ? element(concat(aws_launch_template.this.*.id, [""]), 0) : var.launch_template
version = "$Latest"
}
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_lt_changes ? element(concat(random_pet.asg_name.*.id, [""]), 0) : "",
],
),
)}-"
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
}
launch_template {
id = var.create_lt ? element(concat(aws_launch_template.this.*.id, [""]), 0) : var.launch_template
version = "$Latest"
}
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_lt_changes ? 1 : 0
separator = "-"
length = 2
keepers = {
# Generate a new pet name each time we switch launch template
lt_name = var.create_lt ? element(concat(aws_launch_template.this.*.name, [""]), 0) : var.launch_template
}
}