-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
186 lines (154 loc) · 4.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
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
# Provider configuration
provider "aws" {
region = "us-east-1"
}
# VPC configuration
resource "aws_vpc" "first_vpc" {
cidr_block = var.vpc_cidr_block
instance_tenancy = "default"
enable_dns_hostnames = true
tags = {
Name = var.vpc_name
}
}
# Public subnet
resource "aws_subnet" "public-1" {
vpc_id = aws_vpc.first_vpc.id
cidr_block = var.public_subnet["cidir_block"]
availability_zone = var.public_subnet["availability_zone"]
map_public_ip_on_launch = var.public_subnet["map_public_ip_on_launch"]
tags = {
Name = var.public_subnet["name"]
}
}
# Private subnet
resource "aws_subnet" "private-1" {
vpc_id = aws_vpc.first_vpc.id
cidr_block = var.private_subnet["cidir_block"]
availability_zone = var.private_subnet["availability_zone"]
tags = {
Name = var.private_subnet["name"]
}
}
# Internet gateway
resource "aws_internet_gateway" "first_vpc_gw" {
vpc_id = aws_vpc.first_vpc.id
tags = {
Name = "${var.vpc_name}_IGW"
}
}
# Elastic IP
resource "aws_eip" "nat_ip" {
}
# Nat gateway
resource "aws_nat_gateway" "first_vpc_nat" {
subnet_id = aws_subnet.public-1.id
allocation_id = aws_eip.nat_ip.id
tags = {
Name = "First vpc nat"
}
depends_on = [aws_internet_gateway.first_vpc_gw]
}
# Route table for Public-1 subnet
resource "aws_route_table" "public-rt" {
vpc_id = aws_vpc.first_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.first_vpc_gw.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.first_vpc_gw.id
}
tags = {
Name = "public-rt"
}
}
# Route table for Private-1 subnet
resource "aws_route_table" "private-rt" {
vpc_id = aws_vpc.first_vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.first_vpc_nat.id
}
tags = {
Name = "private-rt"
}
}
# Route table association with public subnets
resource "aws_route_table_association" "public-rta" {
subnet_id = aws_subnet.public-1.id
route_table_id = aws_route_table.public-rt.id
}
# Route table association with private subnets
resource "aws_route_table_association" "private-rta" {
subnet_id = aws_subnet.private-1.id
route_table_id = aws_route_table.private-rt.id
}
# Security groups
resource "aws_security_group" "allow_ssh" {
name = "allow_ssh"
description = "Allow ssh traffic"
vpc_id = aws_vpc.first_vpc.id
# Inbound traffic
dynamic "ingress" {
for_each = var.inbound_traffic
content {
description = ingress.value["description"]
from_port = ingress.value["from_port"]
to_port = ingress.value["to_port"]
protocol = ingress.value["protocol"]
cidr_blocks = ingress.value["cidr_blocks"]
ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"]
}
}
# Outbound trafic
dynamic "egress" {
for_each = var.outbound_traffic
content {
from_port = egress.value["from_port"]
to_port = egress.value["to_port"]
protocol = egress.value["protocol"]
cidr_blocks = egress.value["cidr_blocks"]
ipv6_cidr_blocks = egress.value["ipv6_cidr_blocks"]
}
}
tags = {
Name = "Security groups"
}
}
# Key pair public
resource "aws_key_pair" "public_key_pair" {
key_name = var.public_key_pair["key_name"]
public_key = var.public_key_pair["public_key"]
}
# Key pair private
resource "aws_key_pair" "private_key_pair" {
key_name = var.private_key_pair["key_name"]
public_key = var.private_key_pair["public_key"]
}
# Public EC2
resource "aws_instance" "public-ec2" {
ami = var.ec2_instance["ami"]
instance_type = var.ec2_instance["instance_type"]
availability_zone = var.ec2_instance["availability_zone"]
subnet_id = aws_subnet.public-1.id
key_name = aws_key_pair.public_key_pair.id
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
tags = {
Name = "Public instance"
}
}
# Private EC2
resource "aws_instance" "private-ec2" {
ami = var.ec2_instance["ami"]
instance_type = var.ec2_instance["instance_type"]
availability_zone = var.ec2_instance["availability_zone"]
associate_public_ip_address = var.ec2_instance["associate_public_ip_address"]
subnet_id = aws_subnet.private-1.id
key_name = aws_key_pair.private_key_pair.id
vpc_security_group_ids = [aws_security_group.allow_ssh.id]
tags = {
Name = "Private instance"
}
}