-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
239 lines (195 loc) · 6.11 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
# VPC Block
resource "aws_vpc" "this" {
cidr_block = var.cidr_block
instance_tenancy = var.instance_tenancy
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
# We should never need this again
enable_classiclink = var.enable_classiclink
enable_classiclink_dns_support = var.enable_classiclink_dns_support
assign_generated_ipv6_cidr_block = var.enabled_ipv6
tags = merge(
{
Name = "${var.name}-${var.environment}-vpc"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
depends_on = [var.depends]
}
resource "aws_default_route_table" "rt" {
default_route_table_id = aws_vpc.this.default_route_table_id
tags = merge(
{
Name = "${var.name}-${var.environment}-default-rt"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
resource "aws_default_security_group" "default" {
vpc_id = aws_vpc.this.id
ingress {
protocol = -1
self = true
from_port = 0
to_port = 0
description = "This vpc default sg"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(
{
Name = "${var.name}-${var.environment}-default-sg"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
resource "aws_default_network_acl" "default" {
default_network_acl_id = aws_vpc.this.default_network_acl_id
ingress {
protocol = -1
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
egress {
protocol = -1
rule_no = 100
action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
tags = merge(
{
Name = "${var.name}-${var.environment}-default-acl"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
lifecycle {
ignore_changes = [subnet_ids]
}
}
# Subnets blocks
module "available_zones" {
source = "github.com/vittico/terraform-aws-utils-zones.git?ref=v1.0.0"
}
resource "aws_subnet" "private" {
count = var.az_count
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 8, count.index)
availability_zone = module.available_zones.zones.names[count.index]
vpc_id = aws_vpc.this.id
tags = merge(
{
Name = "${var.name}-${module.available_zones.zones.names[count.index]}-priv-0${count.index}-${var.environment}-subnet"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
resource "aws_subnet" "public" {
count = var.az_count
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 8, var.az_count + count.index)
availability_zone = module.available_zones.zones.names[count.index]
vpc_id = aws_vpc.this.id
map_public_ip_on_launch = true
tags = merge(
{
Name = "${var.name}-${module.available_zones.zones.names[count.index]}-pub-0${count.index}-${var.environment}-subnet"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
# Internet gateway resource
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = merge(
{
Name = "${var.name}-${var.environment}-igw"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
# Elastic IP resource
resource "aws_eip" "this" {
count = var.az_count
vpc = true
tags = merge(
{
Name = "${var.name}-0${count.index}-${var.environment}-eip"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
# Route resource
resource "aws_route" "this" {
route_table_id = aws_vpc.this.default_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
# NAT gateway
resource "aws_nat_gateway" "this" {
count = var.az_count
subnet_id = element(aws_subnet.private.*.id, count.index)
allocation_id = element(aws_eip.this.*.id, count.index)
tags = merge(
{
Name = "${var.name}-eip-0${count.index}-${var.environment}-natwg"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
# Route table resource
resource "aws_route_table" "this" {
count = var.az_count
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = element(aws_nat_gateway.this.*.id, count.index)
}
tags = merge(
{
Name = "${var.name}-0${count.index}-${var.environment}-eip-rt"
Environment = var.environment
Terraform = "True"
AdditionalComments = "This resource created with Terraform using vittico's modules"
},
var.additional_tags
)
}
resource "aws_route_table_association" "this" {
count = var.az_count
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = element(aws_route_table.this.*.id, count.index)
}