-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvpc.tf
More file actions
95 lines (81 loc) · 2.28 KB
/
vpc.tf
File metadata and controls
95 lines (81 loc) · 2.28 KB
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
# create VPC
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
tags = {
Name = "${var.vpc_name}"
}
}
# create subnet
resource "aws_subnet" "public_subnet" {
count = length(var.public_subnet_cidr_blocks)
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.public_subnet_cidr_blocks, count.index)
availability_zone = element(var.az, count.index)
map_public_ip_on_launch = true
tags = {
Name = "${var.vpc_name}-subent-public${count.index + 1}"
}
}
resource "aws_subnet" "private_subnet" {
count = length(var.private_subnet_cidr_blocks)
vpc_id = aws_vpc.vpc.id
cidr_block = element(var.private_subnet_cidr_blocks, count.index)
availability_zone = element(var.az, count.index)
tags = {
Name = "${var.vpc_name}-subent-private${count.index + 1}"
}
}
# create IGW
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${var.vpc_name}-igw"
}
}
# create route table
resource "aws_route_table" "vpc_public_route" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
"Name" = "${var.vpc_name}-public-route"
}
}
# create route table association
# Subnet 과 Route table 을 연결
resource "aws_route_table_association" "vpc_public_routing" {
count = length(var.public_subnet_cidr_blocks)
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
route_table_id = aws_route_table.vpc_public_route.id
}
# create eip for nat
resource "aws_eip" "nat_eip" {
vpc = true
# depends_on = [aws_internet_gateway.igw]
}
# create nat gateway
resource "aws_nat_gateway" "nat_gw" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public_subnet[0].id
# depends_on = [aws_internet_gateway.igw]
tags = {
Name = "${var.vpc_name}-nat"
}
}
resource "aws_route_table" "private_route" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.nat_gw.id
}
tags = {
Name = "${var.vpc_name}-private-route"
}
}
resource "aws_route_table_association" "vpc_private_routing" {
count = length(var.private_subnet_cidr_blocks)
subnet_id = element(aws_subnet.private_subnet.*.id, count.index)
route_table_id = aws_route_table.private_route.id
}