-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
132 lines (114 loc) · 3.14 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
module "label_vpc" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
version = "0.25.0"
context = module.base_label.context
name = "vpc"
attributes = ["main"]
}
module "label_igw" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
version = "0.25.0"
context = module.base_label.context
name = "igw"
attributes = ["main"]
}
module "label_public_route" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
version = "0.25.0"
context = module.base_label.context
name = "route"
attributes = ["public"]
}
module "labels_subnet_default" {
source = "cloudposse/label/null"
version = "0.25.0"
namespace = "feuji"
stage = "test"
name = "subnetaz"
attributes = ["availability-zone"]
tags = {}
label_order = ["name", "stage", "namespace", "attributes"]
}
module "label_private_route" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
version = "0.25.0"
context = module.base_label.context
name = "route"
attributes = ["private"]
}
module "label_public_subnet" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
name = "subnet"
version = "0.25.0"
attributes = ["public"]
delimiter = "-"
tags = {
ManagedBy = "Terraform"
}
}
module "label_private_subnet" {
source = "cloudposse/label/null"
namespace = "feuji"
stage = "test"
name = "subnet"
version = "0.25.0"
attributes = ["private"]
delimiter = "-"
tags = {
ManagedBy = "Terraform"
}
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = module.label_vpc.tags
}
# =========================
# Create your subnets here
# =========================
# Data source to fetch available AZs
data "aws_availability_zones" "available" {
state = "available"
}
# Manually creating subnets without using an external module
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 4, 0) # Example CIDR calculation
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = true
tags = module.label_public_subnet.tags
}
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.main.id
cidr_block = cidrsubnet(var.vpc_cidr, 4, 1) # Example CIDR calculation
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = false
tags = module.label_private_subnet.tags
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main.id
tags = module.label_igw.tags
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.gw.id
}
tags = module.label_public_route.tags
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public.id
}