forked from life360/terraform-aws-autoscaling
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
132 lines (114 loc) · 5.68 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
#######################
# Launch configuration
#######################
resource "aws_launch_configuration" "this" {
count = "${var.create_lc}"
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}"
ebs_block_device = "${var.ebs_block_device}"
ephemeral_block_device = "${var.ephemeral_block_device}"
root_block_device = "${var.root_block_device}"
lifecycle {
create_before_destroy = true
}
}
####################
# Autoscaling group
####################
resource "aws_autoscaling_group" "this" {
count = "${var.create_asg && !var.create_asg_with_initial_lifecycle_hook ? 1 : 0}"
name_prefix = "${join("-", compact(list(coalesce(var.asg_name, var.name), var.recreate_asg_when_lc_changes ? element(concat(random_pet.asg_name.*.id, list("")), 0) : "")))}-"
launch_configuration = "${var.create_lc ? element(concat(aws_launch_configuration.this.*.name, list("")), 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}"
tags = ["${concat(
list(map("key", "Name", "value", var.name, "propagate_at_launch", true)),
var.tags,
local.tags_asg_format
)}"]
lifecycle {
create_before_destroy = true
ignore_changes = ["desired_capacity"]
}
}
################################################
# 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(list(coalesce(var.asg_name, var.name), var.recreate_asg_when_lc_changes ? element(concat(random_pet.asg_name.*.id, list("")), 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}"
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(
list(map("key", "Name", "value", var.name, "propagate_at_launch", true)),
var.tags,
local.tags_asg_format
)}"]
lifecycle {
create_before_destroy = true
ignore_changes = ["desired_capacity"]
}
}
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, list("")), 0) : var.launch_configuration}"
}
}