|
@@ -0,0 +1,92 @@
|
|
|
|
+## VPC
|
|
|
|
+resource "aws_vpc" "infra_vpc" {
|
|
|
|
+ cidr_block = var.cidr_block
|
|
|
|
+ enable_dns_support = var.enable_dns_support
|
|
|
|
+ enable_dns_hostnames = var.enable_dns_hostnames
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Public Subnet for ALB
|
|
|
|
+resource "aws_subnet" "infra_public_subnet" {
|
|
|
|
+ count = length(var.public_subnet_az)
|
|
|
|
+
|
|
|
|
+ vpc_id = aws_vpc.infra_vpc.id
|
|
|
|
+ availability_zone = var.public_subnet_az[count.index]
|
|
|
|
+ cidr_block = var.public_subnet_cidr[count.index]
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Private Subnet for EC2
|
|
|
|
+resource "aws_subnet" "infra_private_subnet" {
|
|
|
|
+ vpc_id = aws_vpc.infra_vpc.id
|
|
|
|
+ availability_zone = var.private_subnet_az
|
|
|
|
+ cidr_block = var.private_subnet_cidr
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Internet Gateway
|
|
|
|
+resource "aws_internet_gateway" "infra_internet_gateway" {
|
|
|
|
+ vpc_id = aws_vpc.infra_vpc.id
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Route Table for Public Subnet and attach Internet Gateway to it
|
|
|
|
+resource "aws_route_table" "infra_public_rt" {
|
|
|
|
+ vpc_id = aws_vpc.infra_vpc.id
|
|
|
|
+
|
|
|
|
+ route {
|
|
|
|
+ cidr_block = "0.0.0.0/0"
|
|
|
|
+ gateway_id = aws_internet_gateway.infra_internet_gateway.id
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Route Table Public Subnet Association
|
|
|
|
+resource "aws_route_table_association" "infra_public_subnet_rt_association" {
|
|
|
|
+ count = length(var.public_subnet_az)
|
|
|
|
+
|
|
|
|
+ subnet_id = aws_subnet.infra_public_subnet[count.index].id
|
|
|
|
+ route_table_id = aws_route_table.infra_public_rt.id
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Elastic IP for NAT Gateway
|
|
|
|
+resource "aws_eip" "infra_nat_gateway_eip" {
|
|
|
|
+ domain = "vpc"
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## NAT Gateway
|
|
|
|
+resource "aws_nat_gateway" "infra_nat_gateway" {
|
|
|
|
+ allocation_id = aws_eip.infra_nat_gateway_eip.id
|
|
|
|
+ subnet_id = aws_subnet.infra_public_subnet[0].id
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+
|
|
|
|
+ depends_on = [
|
|
|
|
+ aws_internet_gateway.infra_internet_gateway
|
|
|
|
+ ]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Route Table for Private Subnet and attach NAT Gateway to it
|
|
|
|
+resource "aws_route_table" "infra_private_rt" {
|
|
|
|
+ vpc_id = aws_vpc.infra_vpc.id
|
|
|
|
+
|
|
|
|
+ route {
|
|
|
|
+ cidr_block = "0.0.0.0/0"
|
|
|
|
+ nat_gateway_id = aws_nat_gateway.infra_nat_gateway.id
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ tags = var.tags
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+## Route Table Private Subnet Association
|
|
|
|
+resource "aws_route_table_association" "infra_private_subnet_rt_association" {
|
|
|
|
+ subnet_id = aws_subnet.infra_private_subnet.id
|
|
|
|
+ route_table_id = aws_route_table.infra_private_rt.id
|
|
|
|
+}
|