-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.tf
88 lines (73 loc) · 2.55 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
data "aws_region" "current" {}
resource "aws_eip" "eip" {
count = "${var.count}"
vpc = true
}
resource "aws_instance" "instance" {
count = "${var.count}"
ami = "${var.ami}"
instance_type = "${var.instance_type}"
subnet_id = "${element(var.subnet_ids, count.index)}"
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
key_name = "${var.key_name}"
user_data = "${var.user_data}"
ephemeral_block_device {
device_name = "/dev/sdb"
no_device = "true"
virtual_name = "ephemeral0"
}
ephemeral_block_device {
device_name = "/dev/sdc"
no_device = "true"
virtual_name = "ephemeral1"
}
tags {
Name = "${var.name}"
}
}
resource "aws_eip_association" "eip_association" {
count = "${var.count}"
instance_id = "${element(aws_instance.instance.*.id, count.index)}"
allocation_id = "${element(aws_eip.eip.*.id, count.index)}"
}
resource "aws_cloudwatch_metric_alarm" "auto_recovery_alarm" {
count = "${var.count}"
alarm_name = "EC2AutoRecover-${element(aws_instance.instance.*.id, count.index)}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "3"
metric_name = "StatusCheckFailed_System"
namespace = "AWS/EC2"
period = "60"
statistic = "Minimum"
dimensions = {
InstanceId = "${element(aws_instance.instance.*.id, count.index)}"
}
alarm_actions = ["arn:aws:automate:${data.aws_region.current.name}:ec2:recover"]
threshold = "1"
alarm_description = "Auto recover the EC2 instance if Status Check fails."
}
resource "aws_route53_health_check" "route53_health_check" {
count = "${var.count}"
ip_address = "${element(aws_eip.eip.*.public_ip, count.index)}"
port = 80
type = "HTTP"
resource_path = "${var.health_check_path}"
failure_threshold = "5"
request_interval = "10"
tags = {
Name = "${var.name}-${element(aws_instance.instance.*.id, count.index)}"
}
}
resource "aws_route53_record" "route53_record" {
count = "${var.count}"
zone_id = "${var.route53_hosted_zone_id}"
name = "${var.domain}"
type = "A"
ttl = "60"
weighted_routing_policy {
weight = 10
}
set_identifier = "${element(aws_instance.instance.*.id, count.index)}"
records = ["${element(aws_eip.eip.*.public_ip, count.index)}"]
health_check_id = "${element(aws_route53_health_check.route53_health_check.*.id, count.index)}"
}