-
Notifications
You must be signed in to change notification settings - Fork 3
/
vpc.tf
151 lines (135 loc) · 3.73 KB
/
vpc.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
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr_blocks[module.global_variables.env]
enable_dns_hostnames = true
enable_dns_support = true
instance_tenancy = "default"
lifecycle {
prevent_destroy = true
}
tags = {
Name = "vpc-${module.global_variables.env}"
Environment = module.global_variables.env
}
}
resource "aws_subnet" "public_2a" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true
cidr_block = var.public_2a_cidr_blocks[module.global_variables.env]
lifecycle {
prevent_destroy = true
}
tags = {
Name = "${module.global_variables.env}-public-2a"
}
}
resource "aws_subnet" "public_2c" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-2c"
map_public_ip_on_launch = true
cidr_block = var.public_2c_cidr_blocks[module.global_variables.env]
lifecycle {
prevent_destroy = true
}
tags = {
Name = "${module.global_variables.env}-public-2c"
}
}
resource "aws_subnet" "private_2a" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-2a"
cidr_block = var.private_2a_cidr_blocks[module.global_variables.env]
lifecycle {
prevent_destroy = true
}
tags = {
Name = "${module.global_variables.env}-private-2a"
}
}
resource "aws_subnet" "private_2c" {
vpc_id = aws_vpc.vpc.id
availability_zone = "ap-northeast-2c"
cidr_block = var.private_2c_cidr_blocks[module.global_variables.env]
lifecycle {
prevent_destroy = true
}
tags = {
Name = "${module.global_variables.env}-private-2c"
}
}
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
lifecycle {
prevent_destroy = true
}
tags = {
Name = module.global_variables.env
}
}
resource "aws_default_route_table" "public" {
default_route_table_id = aws_vpc.vpc.default_route_table_id
tags = {
Name = "${module.global_variables.env}-public"
}
}
resource "aws_route_table" "private" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = "${module.global_variables.env}-private"
}
}
resource "aws_route_table_association" "public_2a" {
subnet_id = aws_subnet.public_2a.id
route_table_id = aws_vpc.vpc.default_route_table_id
}
resource "aws_route_table_association" "public_2c" {
subnet_id = aws_subnet.public_2c.id
route_table_id = aws_vpc.vpc.default_route_table_id
}
resource "aws_route_table_association" "private_2a" {
subnet_id = aws_subnet.private_2a.id
route_table_id = aws_route_table.private.id
}
resource "aws_route_table_association" "private_2c" {
subnet_id = aws_subnet.private_2c.id
route_table_id = aws_route_table.private.id
}
resource "aws_route" "public" {
route_table_id = aws_vpc.vpc.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_eip" "nat" {
vpc = true
lifecycle {
prevent_destroy = true
}
}
resource "aws_nat_gateway" "nat_gateway" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public_2a.id
lifecycle {
prevent_destroy = true
}
tags = {
Name = module.global_variables.env
}
}
resource "aws_route" "private" {
route_table_id = aws_route_table.private.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gateway.id
}
resource "aws_vpc_endpoint" "ecr" {
vpc_id = aws_vpc.vpc.id
service_name = "com.amazonaws.ap-northeast-2.ecr.dkr"
vpc_endpoint_type = "Interface"
private_dns_enabled = true
subnet_ids = [
aws_subnet.private_2a.id,
aws_subnet.private_2c.id
]
security_group_ids = [
aws_security_group.ecr_vpc_endpoint.id
]
}