-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
215 lines (172 loc) · 7.2 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
data "aws_ami" "ami" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-gp2"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = [var.architecture]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
resource "aws_iam_instance_profile" "ec2_instance_profile" {
count = var.instance_profile != null ? 0 : 1
name = "${var.server_name}_instance_profile"
role = aws_iam_role.instance_role[0].name
}
module "ec2_instance" {
count = var.create_autoscaling_group ? 0 : 1
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 4.3"
name = var.server_name
private_ip = var.private_ip
source_dest_check = var.source_dest_check
ami = var.ami != null ? var.ami : data.aws_ami.ami.id
instance_type = var.instance_type
monitoring = var.monitoring
vpc_security_group_ids = var.security_group_ids
subnet_id = var.subnet_id
iam_instance_profile = var.instance_profile != null ? var.instance_profile : aws_iam_instance_profile.ec2_instance_profile[0].name
user_data = base64encode(templatefile(var.user_data_path, var.vars))
user_data_replace_on_change = var.user_data_replace_on_change
root_block_device = var.root_block_device
}
resource "aws_iam_role" "instance_role" {
count = var.instance_profile != null ? 0 : 1
name = "${var.server_name}_ecsInstanceRole"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
managed_policy_arns = concat([
"arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
], var.managed_policy_arns)
}
resource "aws_eip" "this" {
count = var.create_eip ? 1 : 0
instance = var.create_eip && var.create_autoscaling_group == false ? module.ec2_instance[0].id : null
associate_with_private_ip = var.create_eip && var.create_autoscaling_group == false ? module.ec2_instance[0].private_ip : var.private_ip
tags = {
Server_name = var.server_name
}
}
resource "aws_launch_template" "as_template" {
count = var.create_autoscaling_group ? 1 : 0
name_prefix = var.server_name
image_id = var.ami != null ? var.ami : data.aws_ami.ami.id
instance_type = var.instance_type
user_data = base64encode(templatefile(var.user_data_path, var.vars))
iam_instance_profile {
name = var.instance_profile != null ? var.instance_profile : aws_iam_instance_profile.ec2_instance_profile[0].name
}
vpc_security_group_ids = var.security_group_ids
dynamic "block_device_mappings" {
for_each = var.root_block_device
content {
device_name = try(block_device_mappings.value.device_name, "/dev/xvda")
ebs {
delete_on_termination = try(block_device_mappings.value.delete_on_termination, null)
encrypted = try(block_device_mappings.value.encrypted, null)
iops = try(block_device_mappings.value.iops, null)
throughput = try(block_device_mappings.value.throughput, null)
volume_size = try(block_device_mappings.value.volume_size, null)
volume_type = try(block_device_mappings.value.volume_type, null)
}
}
}
network_interfaces {
private_ip_address = var.private_ip
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "this" {
count = var.create_autoscaling_group ? 1 : 0
name = "${var.server_name}-asg"
launch_template {
id = aws_launch_template.as_template[0].id
version = "$Latest"
}
min_size = var.min_size
max_size = var.max_size
vpc_zone_identifier = [var.subnet_id]
termination_policies = var.scaling_termination_policies
default_cooldown = var.scaling_default_cooldown
tag {
key = "Name"
propagate_at_launch = true
value = var.server_name
}
}
# scaling config
resource "aws_autoscaling_policy" "scale_up" {
count = var.create_autoscaling_group && var.scale_up_config != {} ? 1 : 0
name = "${aws_autoscaling_group.this[0].name}-scale_up"
autoscaling_group_name = aws_autoscaling_group.this[0].name
policy_type = try(var.scale_up_config.policy_type, "SimpleScaling")
adjustment_type = try(var.scale_up_config.adjustment_type, "ChangeInCapacity")
scaling_adjustment = try(var.scale_up_config.scaling_adjustment, 1)
cooldown = try(var.scale_up_config.cooldown, 60)
}
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
count = var.create_autoscaling_group && var.scale_up_config != {} ? 1 : 0
alarm_name = "${aws_autoscaling_group.this[0].name}-cpu_high"
comparison_operator = try(var.scale_up_config.comparison_operator, "GreaterThanOrEqualToThreshold")
evaluation_periods = try(var.scale_up_config.evaluation_periods, 1)
metric_name = try(var.scale_up_config.metric_name, "CPUUtilization")
namespace = try(var.scale_up_config.namespace, "AWS/EC2")
period = try(var.scale_up_config.period, 60)
statistic = try(var.scale_up_config.statistic, "Average")
threshold = try(var.scale_up_config.threshold, 50)
alarm_description = "This metric triggers when CPU exceeds ${try(var.scale_up_config.threshold, 50)}%"
alarm_actions = [aws_autoscaling_policy.scale_up[0].arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.this[0].name
}
}
resource "aws_autoscaling_policy" "scale_down" {
count = var.create_autoscaling_group && var.scale_down_config != {} ? 1 : 0
name = "${aws_autoscaling_group.this[0].name}-scale_down"
autoscaling_group_name = aws_autoscaling_group.this[0].name
policy_type = try(var.scale_down_config.policy_type, "SimpleScaling")
adjustment_type = try(var.scale_down_config.adjustment_type, "ChangeInCapacity")
scaling_adjustment = try(var.scale_down_config.scaling_adjustment, -1)
cooldown = try(var.scale_down_config.cooldown, 60)
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
count = var.create_autoscaling_group && var.scale_down_config != {} ? 1 : 0
alarm_name = "${aws_autoscaling_group.this[0].name}-cpu_low"
comparison_operator = try(var.scale_down_config.comparison_operator, "LessThanOrEqualToThreshold")
evaluation_periods = try(var.scale_down_config.evaluation_periods, 1)
metric_name = try(var.scale_down_config.metric_name, "CPUUtilization")
namespace = try(var.scale_down_config.namespace, "AWS/EC2")
period = try(var.scale_down_config.period, 60)
statistic = try(var.scale_down_config.statistic, "Average")
threshold = try(var.scale_down_config.threshold, 40)
alarm_description = "This metric triggers when CPU is below ${try(var.scale_down_config.threshold, 40)}%"
alarm_actions = [aws_autoscaling_policy.scale_down[0].arn]
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.this[0].name
}
}