-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmain.tf
More file actions
240 lines (192 loc) · 6.11 KB
/
main.tf
File metadata and controls
240 lines (192 loc) · 6.11 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.aws_region}"
}
data "aws_ami" "api" {
most_recent = true
filter {
name = "name"
values = ["api-boilerplate-${var.build_env}*"]
}
owners = ["844297601570"]
}
resource "aws_launch_configuration" "launch_config" {
image_id = "${data.aws_ami.api.id}"
instance_type = "${var.instance_type}"
key_name = "${var.key_name}"
security_groups = ["${aws_security_group.api_sg.id}"]
root_block_device {
delete_on_termination = true
volume_type = "gp2"
volume_size = 64
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "api_sg" {
name = "api-boilerplate-security-group"
description = "SG for API boilerplate deployment"
ingress {
from_port = 0
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 81
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "tcp"
self = true
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_autoscaling_group" "main_asg" {
# interpolate the LC into the ASG name so it always forces an update
name = "api-${var.build_env}-asg-${data.aws_ami.api.id}"
# We want this to explicitly depend on the launch config above
depends_on = ["aws_launch_configuration.launch_config"]
# The chosen availability zones *must* match the AZs the VPC subnets are tied to.
availability_zones = ["${split(",", var.availability_zones)}"]
# Uses the ID from the launch config created above
launch_configuration = "${aws_launch_configuration.launch_config.id}"
max_size = "${var.asg_maximum_number_of_instances}"
min_size = "${var.asg_minimum_number_of_instances}"
health_check_grace_period = "${var.health_check_grace_period}"
health_check_type = "${var.health_check_type}"
load_balancers = ["${aws_elb.api_lb.name}"]
wait_for_elb_capacity = "${var.asg_minimum_number_of_instances}"
tag {
key = "environment"
value = "${var.build_env}"
propagate_at_launch = true
}
tag {
key = "Name"
value = "api-${var.build_env}"
propagate_at_launch = true
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_elb" "api_lb" {
name = "api-boilerplate-lb"
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
security_groups = ["${aws_security_group.api_sg.id}"]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
# listener {
# instance_port = 81
# instance_protocol = "http"
# lb_port = 443
# lb_protocol = "https"
# ssl_certificate_id = "arn:aws:iam::123456789012:server-certificate/certName"
# }
health_check {
healthy_threshold = 4
unhealthy_threshold = 2
timeout = 10
target = "HTTP:80/api/health-check"
interval = 15
}
cross_zone_load_balancing = true
idle_timeout = 60
}
resource "aws_autoscaling_policy" "scale_up" {
name = "${aws_autoscaling_group.main_asg.name}-scale-up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.main_asg.name}"
}
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "${aws_autoscaling_group.main_asg.name}-cpu-high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main_asg.name}"
}
alarm_description = "This metric monitors high ec2 cpu utilization"
alarm_actions = ["${aws_autoscaling_policy.scale_up.arn}"]
}
resource "aws_autoscaling_policy" "scale_down" {
name = "${aws_autoscaling_group.main_asg.name}-scale-down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = "${aws_autoscaling_group.main_asg.name}"
}
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
alarm_name = "${aws_autoscaling_group.main_asg.name}-cpu-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "25"
dimensions {
AutoScalingGroupName = "${aws_autoscaling_group.main_asg.name}"
}
alarm_description = "This metric monitors low ec2 cpu utilization"
alarm_actions = ["${aws_autoscaling_policy.scale_down.arn}"]
}
variable "aws_region" {
default = "us-west-2"
}
variable "instance_type" {}
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "key_name" {
description = "The SSH public key name (in EC2 key-pairs) to be injected into instances"
}
variable "asg_maximum_number_of_instances" {
description = "The maximum number of instances the ASG should maintain"
}
variable "asg_minimum_number_of_instances" {
description = "The minimum number of instances the ASG should maintain"
}
variable "health_check_grace_period" {
description = "Number of seconds for a health check to time out"
}
variable "health_check_type" {
description = "The health check used by the ASG to determine health"
}
variable "availability_zones" {
description = "A comma seperated list string of AZs the ASG will be associated with"
}
variable "build_env" {}