-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
258 lines (209 loc) · 5.76 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "My VPC"
}
}
resource "aws_subnet" "public_us_east_1a" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.0.0/24"
availability_zone = "us-east-1a"
tags = {
Name = "Public Subnet us-east-1a"
}
}
resource "aws_subnet" "public_us_east_1b" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1b"
tags = {
Name = "Public Subnet us-east-1b"
}
}
resource "aws_internet_gateway" "my_vpc_igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "My VPC - Internet Gateway"
}
}
resource "aws_route_table" "my_vpc_public" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_vpc_igw.id
}
tags = {
Name = "Public Subnets Route Table for My VPC"
}
}
resource "aws_route_table_association" "my_vpc_us_east_1a_public" {
subnet_id = aws_subnet.public_us_east_1a.id
route_table_id = aws_route_table.my_vpc_public.id
}
resource "aws_route_table_association" "my_vpc_us_east_1b_public" {
subnet_id = aws_subnet.public_us_east_1b.id
route_table_id = aws_route_table.my_vpc_public.id
}
resource "aws_security_group" "allow_http" {
name = "allow_http"
description = "Allow HTTP inbound connections"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "Allow HTTP Security Group"
}
}
resource "aws_launch_configuration" "web" {
name_prefix = "web-"
image_id = "ami-0947d2ba12ee1ff75" # Amazon Linux 2 AMI (HVM), SSD Volume Type
instance_type = "t2.micro"
key_name = "Lenovo T410"
security_groups = [ aws_security_group.allow_http.id ]
associate_public_ip_address = true
user_data = <<USER_DATA
#!/bin/bash
yum update
yum -y install nginx
echo "$(curl http://169.254.169.254/latest/meta-data/local-ipv4)" > /usr/share/nginx/html/index.html
chkconfig nginx on
service nginx start
USER_DATA
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "elb_http" {
name = "elb_http"
description = "Allow HTTP traffic to instances through Elastic Load Balancer"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "Allow HTTP through ELB Security Group"
}
}
resource "aws_elb" "web_elb" {
name = "web-elb"
security_groups = [
aws_security_group.elb_http.id
]
subnets = [
aws_subnet.public_us_east_1a.id,
aws_subnet.public_us_east_1b.id
]
cross_zone_load_balancing = true
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
interval = 30
target = "HTTP:80/"
}
listener {
lb_port = 80
lb_protocol = "http"
instance_port = "80"
instance_protocol = "http"
}
}
resource "aws_autoscaling_group" "web" {
name = "${aws_launch_configuration.web.name}-asg"
min_size = 1
desired_capacity = 2
max_size = 4
health_check_type = "ELB"
load_balancers = [
aws_elb.web_elb.id
]
launch_configuration = aws_launch_configuration.web.name
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
vpc_zone_identifier = [
aws_subnet.public_us_east_1a.id,
aws_subnet.public_us_east_1b.id
]
# Required to redeploy without an outage.
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "web"
propagate_at_launch = true
}
}
resource "aws_autoscaling_policy" "web_policy_up" {
name = "web_policy_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.web.name
}
resource "aws_cloudwatch_metric_alarm" "web_cpu_alarm_up" {
alarm_name = "web_cpu_alarm_up"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "60"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.web.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [ aws_autoscaling_policy.web_policy_up.arn ]
}
resource "aws_autoscaling_policy" "web_policy_down" {
name = "web_policy_down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.web.name
}
resource "aws_cloudwatch_metric_alarm" "web_cpu_alarm_down" {
alarm_name = "web_cpu_alarm_down"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "10"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.web.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [ aws_autoscaling_policy.web_policy_down.arn ]
}
output "elb_dns_name" {
value = aws_elb.web_elb.dns_name
}