-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
111 lines (99 loc) · 2.77 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
resource "aws_vpc" "myvpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
tags = {
"Name" = format("%s-%s", terraform.workspace, "network")
}
}
resource "aws_subnet" "mysubnet" {
count = length(var.cidr)
cidr_block = element(var.cidr, count.index)
vpc_id = aws_vpc.myvpc.id
availability_zone = element(var.availbility_zones, count.index)
map_public_ip_on_launch = true
tags = {
"Name" = format("%s-%s-%s", terraform.workspace, "subnet", count.index + 1)
}
}
resource "aws_internet_gateway" "mygateway" {
vpc_id = aws_vpc.myvpc.id
tags = {
"Name" = format("%s-%s", terraform.workspace, "gateway")
}
}
resource "aws_security_group" "mynsg" {
name = "ssh-http-jenkins"
vpc_id = aws_vpc.myvpc.id
dynamic "ingress" {
for_each = var.ingress_rules
iterator = ingress_port
content {
from_port = ingress_port.value
to_port = ingress_port.value
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
dynamic "egress" {
for_each = var.egress_rules
iterator = egress_port
content {
from_port = egress_port.value
to_port = egress_port.value
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
tags = {
Name = format("%s-%s", terraform.workspace, "ingress-rules")
}
}
# resource "aws_egress_only_internet_gateway" "egw" {
# vpc_id = aws_vpc.myvpc.id
# }
resource "aws_route_table" "myroute" {
vpc_id = aws_vpc.myvpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.mygateway.id
}
tags = {
"Name" = format("%s-%s", terraform.workspace, "routetable")
}
}
resource "aws_route_table_association" "rtallocation" {
count = length(var.cidr)
subnet_id = element(aws_subnet.mysubnet.*.id,count.index)
//gateway_id = aws_internet_gateway.mygateway.id
route_table_id = aws_route_table.myroute.id
}
data "aws_key_pair" "key" {}
resource "aws_instance" "myvms" {
count = (terraform.workspace == "dev" ? 1 : 3)
instance_type = var.instance_type
ami = var.ami_id
//security_groups = [data.aws_security_group.dsg.id]
security_groups = [aws_security_group.mynsg.id]
key_name = data.aws_key_pair.key.key_name
subnet_id = aws_subnet.mysubnet[0].id
connection {
type = "ssh"
user = "ec2-user"
private_key = "${file(var.path)}"
host = "${element(self.*.public_ip,count.index)}"
//host = "${element(aws_instance.myvms.*.public_ip,count.index)}"
}
# provisioner "file" {
# source = "demo.html"
# destination = "/home/ec2-user/demo.html"
# }
provisioner "remote-exec" {
inline = [file("test.sh")]
}
depends_on = [
aws_vpc.myvpc
]
tags = {
"Name" = format("%s-%s-%s",terraform.workspace,"server",count.index+1)
}
}