11locals {
2- subnet_config = [
2+ public_subnet_config = [
33 {
4+ name = " imms-${ var . environment } -fhir-api-public-subnet-a"
45 cidr_block = " 172.31.16.0/20"
56 availability_zone = " eu-west-2a"
67 },
78 {
9+ name = " imms-${ var . environment } -fhir-api-public-subnet-b"
810 cidr_block = " 172.31.32.0/20"
911 availability_zone = " eu-west-2b"
1012 },
1113 {
14+ name = " imms-${ var . environment } -fhir-api-public-subnet-c"
1215 cidr_block = " 172.31.0.0/20"
1316 availability_zone = " eu-west-2c"
1417 }
1518 ]
19+ private_subnet_config = [
20+ {
21+ name = " imms-${ var . environment } -fhir-api-private-subnet-a"
22+ cidr_block = " 172.31.48.0/20"
23+ availability_zone = " eu-west-2a"
24+ },
25+ {
26+ name = " imms-${ var . environment } -fhir-api-private-subnet-b"
27+ cidr_block = " 172.31.64.0/20"
28+ availability_zone = " eu-west-2b"
29+ },
30+ {
31+ name = " imms-${ var . environment } -fhir-api-private-subnet-c"
32+ cidr_block = " 172.31.80.0/20"
33+ availability_zone = " eu-west-2c"
34+ }
35+ ]
1636}
1737
1838resource "aws_vpc" "default" {
1939 cidr_block = " 172.31.0.0/16"
2040 enable_dns_support = true
2141 enable_dns_hostnames = true
42+
2243 tags = {
2344 Name = " imms-${ var . environment } -fhir-api-vpc"
2445 }
2546}
2647
27- resource "aws_subnet" "default_subnets" {
28- for_each = { for idx , subnet in local . subnet_config : idx => subnet }
29- vpc_id = aws_vpc. default . id
30- cidr_block = each. value . cidr_block
31- availability_zone = each. value . availability_zone
32- map_public_ip_on_launch = true
48+ resource "aws_subnet" "public" {
49+ for_each = { for idx , subnet in local . public_subnet_config : idx => subnet }
50+
51+ vpc_id = aws_vpc. default . id
52+ cidr_block = each. value . cidr_block
53+ availability_zone = each. value . availability_zone
54+
55+ tags = {
56+ Name = each.value.name
57+ }
3358}
3459
3560resource "aws_internet_gateway" "default" {
3661 vpc_id = aws_vpc. default . id
62+
3763 tags = {
3864 Name = " imms-${ var . environment } -fhir-api-igw"
3965 }
4066}
4167
42- resource "aws_route_table" "default " {
68+ resource "aws_route_table" "public " {
4369 vpc_id = aws_vpc. default . id
70+
4471 tags = {
45- Name = " imms-${ var . environment } -fhir-api-rtb"
72+ Name = " imms-${ var . environment } -fhir-api-public- rtb"
4673 }
4774}
4875
49- resource "aws_route_table_association" "subnet_associations" {
50- for_each = aws_subnet. default_subnets
76+ resource "aws_route_table_association" "public_subnets" {
77+ for_each = aws_subnet. public
78+
5179 subnet_id = each. value . id
52- route_table_id = aws_route_table. default . id
80+ route_table_id = aws_route_table. public . id
5381}
5482
55-
56- resource "aws_route" "igw_route" {
57- route_table_id = aws_route_table. default . id
58- destination_cidr_block = " 0.0.0.0/16"
83+ resource "aws_route" "igw" {
84+ route_table_id = aws_route_table. public . id
85+ destination_cidr_block = " 0.0.0.0/0"
5986 gateway_id = aws_internet_gateway. default . id
6087}
6188
89+ resource "aws_subnet" "private" {
90+ for_each = { for idx , subnet in local . private_subnet_config : idx => subnet }
91+
92+ vpc_id = aws_vpc. default . id
93+ cidr_block = each. value . cidr_block
94+ availability_zone = each. value . availability_zone
95+
96+ tags = {
97+ Name = each.value.name
98+ }
99+ }
100+
101+ resource "aws_eip" "nat" {
102+ domain = " vpc"
103+
104+ depends_on = [aws_internet_gateway . default ]
105+ }
106+
107+ resource "aws_nat_gateway" "default" {
108+ allocation_id = aws_eip. nat . id
109+ subnet_id = aws_subnet. public [0 ]. id
110+
111+ tags = {
112+ Name = " imms-${ var . environment } -fhir-api-nat"
113+ }
114+ }
115+
116+ resource "aws_route_table" "private" {
117+ vpc_id = aws_vpc. default . id
118+
119+ tags = {
120+ Name = " imms-${ var . environment } -fhir-api-private-rtb"
121+ }
122+ }
123+
124+ resource "aws_route_table_association" "private_subnets" {
125+ for_each = aws_subnet. private
126+
127+ subnet_id = each. value . id
128+ route_table_id = aws_route_table. private . id
129+ }
130+
131+ resource "aws_route" "nat" {
132+ route_table_id = aws_route_table. private . id
133+ destination_cidr_block = " 0.0.0.0/0"
134+ nat_gateway_id = aws_nat_gateway. default . id
135+ }
136+
62137resource "aws_route53_zone" "parent_hosted_zone" {
63138 name = var. parent_route53_zone_name
64139}
@@ -74,3 +149,17 @@ resource "aws_route53_record" "imms_ns" {
74149 ttl = 172800
75150 records = [for ns in aws_route53_zone . child_hosted_zone . name_servers : " ${ ns } ." ]
76151}
152+
153+ # TODO - remove once state has been updated
154+ moved {
155+ from = aws_subnet. default_subnets
156+ to = aws_subnet. public
157+ }
158+ moved {
159+ from = aws_route_table. default
160+ to = aws_route_table. public
161+ }
162+ moved {
163+ from = aws_route. igw_route
164+ to = aws_route. igw
165+ }
0 commit comments