-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2.tf
86 lines (63 loc) · 2.13 KB
/
ec2.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
data "aws_ami" "packer_ami" {
most_recent = true
owners = var.ami_owner
filter {
name = "name"
values = var.ami_name
}
}
# Web servers
resource "aws_instance" "my_ec2_webserver_cluster" {
for_each = var.webserver_instances
ami = data.aws_ami.packer_ami.id
key_name = aws_key_pair.my_key_pair.key_name
instance_type = lookup(each.value, "instance_type", "t2.micro")
vpc_security_group_ids = [aws_security_group.my_public_sg.id]
subnet_id = lookup(aws_subnet.my_public_subnet,
var.public_subnets[0],
var.public_subnets[0]
).id
associate_public_ip_address = true
monitoring = lookup(each.value, "monitoring", true)
tags = {
Name = lookup(each.value, "instance_name", each.key)
Deploy = "Terraform"
}
depends_on = [aws_key_pair.my_key_pair]
}
# Monitoring servers
resource "aws_instance" "my_ec2_monitoring_cluster" {
for_each = var.monitoring_instances
ami = data.aws_ami.packer_ami.id
key_name = aws_key_pair.my_key_pair.key_name
instance_type = lookup(each.value, "instance_type", "t2.micro")
vpc_security_group_ids = [aws_security_group.my_private_with_nat_sg.id]
subnet_id = lookup(aws_subnet.my_private_with_nat_subnet,
var.private_with_nat_subnets[0],
var.private_with_nat_subnets[0]
).id
monitoring = lookup(each.value, "monitoring", true)
tags = {
Name = lookup(each.value, "instance_name", each.key)
Deploy = "Terraform"
}
depends_on = [aws_key_pair.my_key_pair]
}
# Database servers
resource "aws_instance" "my_ec2_database_cluster" {
for_each = var.database_instances
ami = data.aws_ami.packer_ami.id
key_name = aws_key_pair.my_key_pair.key_name
instance_type = lookup(each.value, "instance_type", "t2.micro")
vpc_security_group_ids = [aws_security_group.my_private_sg.id]
subnet_id = lookup(aws_subnet.my_private_subnet,
var.private_subnets[0],
var.private_subnets[0]
).id
monitoring = lookup(each.value, "monitoring", true)
tags = {
Name = lookup(each.value, "instance_name", each.key)
Deploy = "Terraform"
}
depends_on = [aws_key_pair.my_key_pair]
}