1- # Security Group for VPC Endpoints
2- resource "aws_security_group" "vpc_endpoints" {
3- name_prefix = " hpc-${ var . environment } -vpc-endpoints-"
4- vpc_id = module. vpc . vpc_id
5- description = " Security group for VPC endpoints"
6-
7- ingress {
8- from_port = 443
9- to_port = 443
10- protocol = " tcp"
11- cidr_blocks = [var . vpc_cidr ]
12- description = " HTTPS from VPC"
13- }
14-
15- egress {
16- from_port = 0
17- to_port = 0
18- protocol = " -1"
19- cidr_blocks = [" 0.0.0.0/0" ]
20- description = " All outbound traffic"
21- }
22-
23- tags = merge (var. common_tags , {
24- Name = " hpc-${ var . environment } -vpc-endpoints-sg"
25- Type = " VPC-Endpoints-SecurityGroup"
1+ # Simple VPC Configuration for Testing
2+ resource "aws_vpc" "main" {
3+ cidr_block = var. cidr
4+ enable_dns_hostnames = true
5+ enable_dns_support = true
6+
7+ tags = merge (var. tags , {
8+ Name = var.name
269 })
27-
28- lifecycle {
29- create_before_destroy = true
30- }
3110}
3211
33- # VPC Endpoints for AWS services
34- resource "aws_vpc_endpoint" "ec2" {
35- vpc_id = module. vpc . vpc_id
36- service_name = " com.amazonaws.${ var . region } .ec2"
37- vpc_endpoint_type = " Interface"
38- subnet_ids = module. vpc . private_subnets
39- security_group_ids = [aws_security_group . vpc_endpoints . id ]
40- private_dns_enabled = true
41-
42- tags = merge (var. common_tags , {
43- Name = " hpc-${ var . environment } -ec2-endpoint"
44- Type = " VPC-Endpoint"
12+ # Internet Gateway
13+ resource "aws_internet_gateway" "main" {
14+ vpc_id = aws_vpc. main . id
15+
16+ tags = merge (var. tags , {
17+ Name = " ${ var . name } -igw"
4518 })
4619}
4720
48- resource "aws_vpc_endpoint" "ec2messages" {
49- vpc_id = module. vpc . vpc_id
50- service_name = " com.amazonaws.${ var . region } .ec2messages"
51- vpc_endpoint_type = " Interface"
52- subnet_ids = module. vpc . private_subnets
53- security_group_ids = [aws_security_group . vpc_endpoints . id ]
54- private_dns_enabled = true
55-
56- tags = merge (var. common_tags , {
57- Name = " hpc-${ var . environment } -ec2messages-endpoint"
58- Type = " VPC-Endpoint"
21+ # Public Subnets
22+ resource "aws_subnet" "public" {
23+ count = length (var. public_subnets )
24+
25+ vpc_id = aws_vpc. main . id
26+ cidr_block = var. public_subnets [count . index ]
27+ availability_zone = var. azs [count . index ]
28+ map_public_ip_on_launch = true
29+
30+ tags = merge (var. public_subnet_tags , {
31+ Name = " ${ var . name } -public-${ count . index + 1 } "
32+ })
33+ }
34+
35+ # Private Subnets
36+ resource "aws_subnet" "private" {
37+ count = length (var. private_subnets )
38+
39+ vpc_id = aws_vpc. main . id
40+ cidr_block = var. private_subnets [count . index ]
41+ availability_zone = var. azs [count . index ]
42+
43+ tags = merge (var. private_subnet_tags , {
44+ Name = " ${ var . name } -private-${ count . index + 1 } "
5945 })
6046}
6147
62- resource "aws_vpc_endpoint" "ssm" {
63- vpc_id = module. vpc . vpc_id
64- service_name = " com.amazonaws.${ var . region } .ssm"
65- vpc_endpoint_type = " Interface"
66- subnet_ids = module. vpc . private_subnets
67- security_group_ids = [aws_security_group . vpc_endpoints . id ]
68- private_dns_enabled = true
69-
70- tags = merge (var. common_tags , {
71- Name = " hpc-${ var . environment } -ssm-endpoint"
72- Type = " VPC-Endpoint"
48+ # Database Subnets
49+ resource "aws_subnet" "database" {
50+ count = length (var. database_subnets )
51+
52+ vpc_id = aws_vpc. main . id
53+ cidr_block = var. database_subnets [count . index ]
54+ availability_zone = var. azs [count . index ]
55+
56+ tags = merge (var. database_subnet_tags , {
57+ Name = " ${ var . name } -database-${ count . index + 1 } "
7358 })
7459}
7560
76- resource "aws_vpc_endpoint" "ssmmessages" {
77- vpc_id = module. vpc . vpc_id
78- service_name = " com.amazonaws.${ var . region } .ssmmessages"
79- vpc_endpoint_type = " Interface"
80- subnet_ids = module. vpc . private_subnets
81- security_group_ids = [aws_security_group . vpc_endpoints . id ]
82- private_dns_enabled = true
83-
84- tags = merge (var. common_tags , {
85- Name = " hpc-${ var . environment } -ssmmessages-endpoint"
86- Type = " VPC-Endpoint"
61+ # Compute Subnets
62+ resource "aws_subnet" "compute" {
63+ count = length (var. compute_subnets )
64+
65+ vpc_id = aws_vpc. main . id
66+ cidr_block = var. compute_subnets [count . index ]
67+ availability_zone = var. azs [count . index ]
68+
69+ tags = merge (var. compute_subnet_tags , {
70+ Name = " ${ var . name } -compute-${ count . index + 1 } "
8771 })
8872}
8973
90- resource "aws_vpc_endpoint" "cloudwatch" {
91- vpc_id = module . vpc . vpc_id
92- service_name = " com.amazonaws. ${ var . region } .monitoring "
93- vpc_endpoint_type = " Interface "
94- subnet_ids = module . vpc . private_subnets
95- security_group_ids = [ aws_security_group . vpc_endpoints . id ]
96- private_dns_enabled = true
97-
98- tags = merge (var . common_tags , {
99- Name = " hpc- ${ var . environment } -cloudwatch-endpoint "
100- Type = " VPC-Endpoint "
74+ # Route Table for Public Subnets
75+ resource "aws_route_table" "public" {
76+ vpc_id = aws_vpc . main . id
77+
78+ route {
79+ cidr_block = " 0.0.0.0/0 "
80+ gateway_id = aws_internet_gateway . main . id
81+ }
82+
83+ tags = merge ( var. tags , {
84+ Name = " ${ var . name } -public-rt "
10185 })
10286}
10387
104- resource "aws_vpc_endpoint" "cloudwatchlogs" {
105- vpc_id = module. vpc . vpc_id
106- service_name = " com.amazonaws.${ var . region } .logs"
107- vpc_endpoint_type = " Interface"
108- subnet_ids = module. vpc . private_subnets
109- security_group_ids = [aws_security_group . vpc_endpoints . id ]
110- private_dns_enabled = true
111-
112- tags = merge (var. common_tags , {
113- Name = " hpc-${ var . environment } -cloudwatchlogs-endpoint"
114- Type = " VPC-Endpoint"
88+ # Route Table Associations for Public Subnets
89+ resource "aws_route_table_association" "public" {
90+ count = length (aws_subnet. public )
91+
92+ subnet_id = aws_subnet. public [count . index ]. id
93+ route_table_id = aws_route_table. public . id
94+ }
95+
96+ # NAT Gateway (single for dev)
97+ resource "aws_eip" "nat" {
98+ count = var. enable_nat_gateway ? 1 : 0
99+
100+ domain = " vpc"
101+
102+ tags = merge (var. tags , {
103+ Name = " ${ var . name } -nat-eip"
115104 })
116105}
106+
107+ resource "aws_nat_gateway" "main" {
108+ count = var. enable_nat_gateway ? 1 : 0
109+
110+ allocation_id = aws_eip. nat [0 ]. id
111+ subnet_id = aws_subnet. public [0 ]. id
112+
113+ tags = merge (var. tags , {
114+ Name = " ${ var . name } -nat-gateway"
115+ })
116+
117+ depends_on = [aws_internet_gateway . main ]
118+ }
119+
120+ # Route Table for Private Subnets
121+ resource "aws_route_table" "private" {
122+ count = var. enable_nat_gateway ? 1 : 0
123+
124+ vpc_id = aws_vpc. main . id
125+
126+ route {
127+ cidr_block = " 0.0.0.0/0"
128+ nat_gateway_id = aws_nat_gateway. main [0 ]. id
129+ }
130+
131+ tags = merge (var. tags , {
132+ Name = " ${ var . name } -private-rt"
133+ })
134+ }
135+
136+ # Route Table Associations for Private Subnets
137+ resource "aws_route_table_association" "private" {
138+ count = var. enable_nat_gateway ? length (aws_subnet. private ) : 0
139+
140+ subnet_id = aws_subnet. private [count . index ]. id
141+ route_table_id = aws_route_table. private [0 ]. id
142+ }
143+
144+ # Security Group for VPC Endpoints
145+ resource "aws_security_group" "vpc_endpoints" {
146+ name_prefix = " ${ var . name } -vpc-endpoints-"
147+ vpc_id = aws_vpc. main . id
148+ description = " Security group for VPC endpoints"
149+
150+ ingress {
151+ from_port = 443
152+ to_port = 443
153+ protocol = " tcp"
154+ cidr_blocks = [var . cidr ]
155+ description = " HTTPS from VPC"
156+ }
157+
158+ egress {
159+ from_port = 0
160+ to_port = 0
161+ protocol = " -1"
162+ cidr_blocks = [" 0.0.0.0/0" ]
163+ description = " All outbound traffic"
164+ }
165+
166+ tags = merge (var. tags , {
167+ Name = " ${ var . name } -vpc-endpoints-sg"
168+ })
169+
170+ lifecycle {
171+ create_before_destroy = true
172+ }
173+ }
0 commit comments