-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathvpc_network.tf
73 lines (68 loc) · 3.52 KB
/
vpc_network.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
////////////////////////////////////////////////////////[ VPC NETWORKING ]////////////////////////////////////////////////
# # ---------------------------------------------------------------------------------------------------------------------#
# Create our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_vpc" "this" {
cidr_block = var.app["cidr_block"]
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "${var.app["brand"]}-vpc"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create subnets for each AZ in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_subnet" "this" {
for_each = data.aws_availability_zone.all
vpc_id = aws_vpc.this.id
availability_zone = each.key
cidr_block = cidrsubnet(aws_vpc.this.cidr_block, 4, var.az_number[each.value.name_suffix])
map_public_ip_on_launch = true
tags = {
Name = "${local.project}-subnet"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create internet gateway in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = {
Name = "${local.project}-internet-gateway"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create route table in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_route" "this" {
route_table_id = aws_vpc.this.main_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.this.id
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Assign AZ subnets to route table in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_route_table_association" "this" {
for_each = aws_subnet.this
subnet_id = aws_subnet.this[each.key].id
route_table_id = aws_vpc.this.main_route_table_id
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Create DHCP options in our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_vpc_dhcp_options" "this" {
domain_name = "${data.aws_region.current.name}.compute.internal"
domain_name_servers = ["AmazonProvidedDNS"]
tags = {
Name = "${local.project}-dhcp"
}
}
# # ---------------------------------------------------------------------------------------------------------------------#
# Assign DHCP options to our dedicated VPC
# # ---------------------------------------------------------------------------------------------------------------------#
resource "aws_vpc_dhcp_options_association" "this" {
vpc_id = aws_vpc.this.id
dhcp_options_id = aws_vpc_dhcp_options.this.id
}