-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
203 lines (152 loc) · 3.99 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
provider "aws" {
region = "${var.aws_region}"
}
data "aws_availability_zones" "available" {}
# VPC
resource "aws_vpc" "vpc" {
cidr_block = "10.1.0.0/16"
}
#internet gateway
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
# Route tables
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.internet_gateway.id}"
}
tags {
Name = "public"
}
}
resource "aws_default_route_table" "private" {
default_route_table_id = "${aws_vpc.vpc.default_route_table_id}"
tags {
Name = "private"
}
}
resource "aws_subnet" "public1" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.cidrs["public1"]}"
map_public_ip_on_launch = true
availability_zone = "${data.aws_availability_zones.available.names[0]}"
tags {
Name = "public1"
}
}
resource "aws_subnet" "private1" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.cidrs["private1"]}"
map_public_ip_on_launch = false
availability_zone = "${data.aws_availability_zones.available.names[1]}"
tags {
Name = "private1"
}
}
# Subnet Associations
resource "aws_route_table_association" "public1_assoc" {
subnet_id = "${aws_subnet.public1.id}"
route_table_id = "${aws_route_table.public.id}"
}
resource "aws_route_table_association" "private1_assoc" {
subnet_id = "${aws_subnet.private1.id}"
route_table_id = "${aws_route_table.public.id}"
}
#Security groups
resource "aws_security_group" "dev_sg" {
name = "dev_sg"
description = "Used for access to the dev instance"
vpc_id = "${aws_vpc.vpc.id}"
#SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.localip}"]
}
#HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_security_group" "public_sg" {
name = "sg_public"
description = "Used for public and private instances for load balancer access"
vpc_id = "${aws_vpc.vpc.id}"
#SSH
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["${var.localip}"]
}
#HTTP
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#Private Security Group
resource "aws_security_group" "private_sg" {
name = "sg_private"
description = "Used for private instances"
vpc_id = "${aws_vpc.vpc.id}"
# Access from other security groups
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.1.0.0/16"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# key pair
resource "aws_key_pair" "auth" {
key_name = "${var.key_name}"
public_key = "${file(var.public_key_path)}"
}
# server
resource "aws_instance" "dev" {
instance_type = "${var.dev_instance_type}"
ami = "${var.dev_ami}"
tags {
Name = "hello-instance"
}
key_name = "${aws_key_pair.auth.id}"
vpc_security_group_ids = ["${aws_security_group.public_sg.id}"]
subnet_id = "${aws_subnet.public1.id}"
provisioner "local-exec" {
command = <<EOD
cat <<EOF > aws_hosts
[dev]
${aws_instance.dev.public_ip}
EOF
EOD
}
provisioner "local-exec" {
command = "aws ec2 wait instance-status-ok --instance-ids ${aws_instance.dev.id} && ansible-playbook -i aws_hosts nginx.yml"
}
}
#-------OUTPUTS ------------
output "Nginx Address" {
value = "http://${aws_instance.dev.public_ip}"
}