-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
236 lines (208 loc) · 5.59 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
# NETWORK
resource "aws_vpc" "vpc" {
cidr_block = var.cidr_block_range
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Environment = var.environment_tag
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Environment = var.environment_tag
}
}
resource "aws_subnet" "subnet_public_one" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.cidr_subnet_one
map_public_ip_on_launch = "true"
availability_zone = var.availability_zone_one
tags = {
Environment = var.environment_tag
Type = "Public"
}
}
//
// resource "aws_eip" "ip" {
// instance = "${aws_instance.terra-sample0.id}"
// depends_on = ["aws_instance.terra-sample0"]
// }
resource "aws_subnet" "subnet_public_two" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.cidr_subnet_two
map_public_ip_on_launch = "true"
availability_zone = var.availability_zone_two
tags = {
Environment = var.environment_tag
Type = "Public"
}
}
resource "aws_route_table" "rtb_public" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Environment = var.environment_tag
}
}
resource "aws_route_table_association" "rta_subnet_public_one" {
subnet_id = aws_subnet.subnet_public_one.id
route_table_id = aws_route_table.rtb_public.id
}
resource "aws_route_table_association" "rta_subnet_public_two" {
subnet_id = aws_subnet.subnet_public_two.id
route_table_id = aws_route_table.rtb_public.id
}
# SECURITY GROUP
resource "aws_security_group" "sg_22" {
name = "sg_22"
vpc_id = aws_vpc.vpc.id
ingress {
description = "SSH from any IP"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "HTTPS"
from_port = 443
to_port = 443
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 = {
Environment = var.environment_tag
}
}
# EC2 INSTANCE
resource "aws_instance" "test-instance_one" {
ami = var.instance_ami
instance_type = var.instance_type
subnet_id = aws_subnet.subnet_public_one.id
vpc_security_group_ids = [aws_security_group.sg_22.id]
key_name = "testing"
tags = {
Environment = var.environment_tag
}
# Provision with Ansible
connection {
private_key = file(var.private_key)
user = "ubuntu"
# need an IP address for the boxes but we don't really want them acessible (with associate_public_ip_address = true)
# better to add another instance with only SSH open to tunnel through that box (jumpbox server, bastion host)
host = self.public_ip
}
# Ansible requires Python to be installed on the remote machine as well as the local machine.
# SSH into the machine and run the command
provisioner "remote-exec" {
inline = [
"sudo apt-get -qq install python3.8 -y",
"sudo apt-get -y install nginx",
"sudo service nginx start",
]
}
# run ansible locally
provisioner "local-exec" {
command = <<EOT
pwd
EOT
}
}
# count = 2
resource "aws_instance" "test-instance_two" {
ami = var.instance_ami
instance_type = var.instance_type
subnet_id = aws_subnet.subnet_public_two.id
vpc_security_group_ids = [aws_security_group.sg_22.id]
key_name = "testing"
associate_public_ip_address = true
tags = {
Environment = var.environment_tag
}
# Provision with Ansible
connection {
private_key = file(var.private_key)
user = "ubuntu"
host = self.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo apt-get -qq install python3.8 -y",
"sudo apt-get -y install nginx",
"sudo service nginx start",
]
}
provisioner "local-exec" {
command = <<EOT
pwd
EOT
}
}
# LOAD BALANCER
resource "aws_lb" "test" {
name = "test-lb-tf"
internal = true
load_balancer_type = "application"
security_groups = [aws_security_group.sg_22.id]
subnets = [
aws_subnet.subnet_public_one.id,
aws_subnet.subnet_public_two.id,
]
enable_deletion_protection = false
// access_logs {
// bucket = aws_s3_bucket.lb_logs.bucket
// prefix = "test-lb"
// enabled = true
// }
tags = {
Environment = var.environment_tag
}
}
# Adds target group for lb, add instances there and point the lb to the target groups
resource "aws_lb_target_group" "test" {
name = "tf-example-lb-tg"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.vpc.id
}
// ERROR: Error registering targets with target group: ValidationError: The IP address 'i-09fc080c289280529' is not a valid IPv4 address
// resource "aws_lb_target_group_attachment" "test_one" {
// target_group_arn = aws_lb_target_group.test.arn
// target_id = aws_instance.test-instance_one.id
// port = 80
// }
//
// resource "aws_lb_target_group_attachment" "test_two" {
// target_group_arn = aws_lb_target_group.test.arn
// target_id = aws_instance.test-instance_two.id
// port = 80
// }
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.test.arn
port = "80"
protocol = "HTTP"
// ssl_policy = "ELBSecurityPolicy-2016-08"
// certificate_arn = "arn:aws:iam::187416307283:server-certificate/test_cert_rab3wuqwgja25ct3n4jdj2tzu4"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.test.arn
}
}