Actualiser vpc-production.tf
All checks were successful
Terraform Apply / Terraform Apply (push) Successful in 41s
All checks were successful
Terraform Apply / Terraform Apply (push) Successful in 41s
This commit is contained in:
146
vpc-production.tf
Normal file
146
vpc-production.tf
Normal file
@@ -0,0 +1,146 @@
|
||||
variable "aws_region" {
|
||||
default = "eu-north-1"
|
||||
}
|
||||
|
||||
variable "environment" {
|
||||
default = "Production"
|
||||
}
|
||||
|
||||
variable "vpc_cidr" {
|
||||
default = "10.0.0.0/16"
|
||||
description = "CIDR block of the vpc"
|
||||
}
|
||||
|
||||
variable "public_subnets_cidr" {
|
||||
type = list(any)
|
||||
default = ["10.0.0.0/20", "10.0.128.0/20"]
|
||||
description = "CIDR block for Public Subnet"
|
||||
}
|
||||
|
||||
variable "private_subnets_cidr" {
|
||||
type = list(any)
|
||||
default = ["10.0.16.0/20", "10.0.144.0/20"]
|
||||
description = "CIDR block for Private Subnet"
|
||||
}
|
||||
|
||||
locals {
|
||||
availability_zones = ["${var.aws_region}a", "${var.aws_region}b"]
|
||||
}
|
||||
|
||||
# VPC
|
||||
resource "aws_vpc" "default" {
|
||||
cidr_block = "10.0.0.0/16"
|
||||
enable_dns_hostnames = true
|
||||
enable_dns_support = true
|
||||
instance_tenancy = "default"
|
||||
|
||||
tags = {
|
||||
Name = "${var.environment}-vpc"
|
||||
Environment = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
# Public subnet
|
||||
resource "aws_subnet" "public_subnet" {
|
||||
vpc_id = aws_vpc.default.id
|
||||
|
||||
count = length(var.public_subnets_cidr)
|
||||
cidr_block = element(var.public_subnets_cidr, count.index)
|
||||
availability_zone = element(local.availability_zones, count.index)
|
||||
map_public_ip_on_launch = true
|
||||
|
||||
tags = {
|
||||
Name = "${var.environment}-${element(local.availability_zones, count.index)}-public-subnet"
|
||||
Environment = "${var.environment}"
|
||||
}
|
||||
}
|
||||
|
||||
# Private Subnet
|
||||
resource "aws_subnet" "private_subnet" {
|
||||
vpc_id = aws_vpc.default.id
|
||||
|
||||
count = length(var.private_subnets_cidr)
|
||||
cidr_block = element(var.private_subnets_cidr, count.index)
|
||||
availability_zone = element(local.availability_zones, count.index)
|
||||
map_public_ip_on_launch = false
|
||||
|
||||
tags = {
|
||||
Name = "${var.environment}-${element(local.availability_zones, count.index)}-private-subnet"
|
||||
Environment = "${var.environment}"
|
||||
}
|
||||
}
|
||||
|
||||
#Internet gateway
|
||||
resource "aws_internet_gateway" "igw" {
|
||||
vpc_id = aws_vpc.default.id
|
||||
|
||||
tags = {
|
||||
"Name" = "${var.environment}-igw"
|
||||
"Environment" = var.environment
|
||||
}
|
||||
}
|
||||
|
||||
# Elastic-IP (eip) for NAT
|
||||
#resource "aws_eip" "nat_eip" {
|
||||
# vpc = true
|
||||
# depends_on = [aws_internet_gateway.ig]
|
||||
#}
|
||||
|
||||
# NAT Gateway
|
||||
#resource "aws_nat_gateway" "nat" {
|
||||
# allocation_id = aws_eip.nat_eip.id
|
||||
# subnet_id = element(aws_subnet.public_subnet.*.id, 0)
|
||||
# tags = {
|
||||
# Name = "nat-gateway-${var.environment}"
|
||||
# Environment = "${var.environment}"
|
||||
# }
|
||||
#}
|
||||
|
||||
# Routing tables to route traffic for Private Subnet
|
||||
resource "aws_route_table" "private" {
|
||||
vpc_id = aws_vpc.default.id
|
||||
|
||||
tags = {
|
||||
Name = "${var.environment}-private-route-table"
|
||||
Environment = "${var.environment}"
|
||||
}
|
||||
}
|
||||
|
||||
# Routing tables to route traffic for Public Subnet
|
||||
resource "aws_route_table" "public" {
|
||||
vpc_id = aws_vpc.default.id
|
||||
|
||||
tags = {
|
||||
Name = "${var.environment}-public-route-table"
|
||||
Environment = "${var.environment}"
|
||||
}
|
||||
}
|
||||
|
||||
# Route for Internet Gateway
|
||||
resource "aws_route" "public_internet_gateway" {
|
||||
route_table_id = aws_route_table.public.id
|
||||
|
||||
destination_cidr_block = "0.0.0.0/0"
|
||||
gateway_id = aws_internet_gateway.igw.id
|
||||
}
|
||||
|
||||
# Route for NAT Gateway
|
||||
#resource "aws_route" "private_internet_gateway" {
|
||||
# route_table_id = aws_route_table.private.id
|
||||
#
|
||||
# destination_cidr_block = "0.0.0.0/0"
|
||||
# gateway_id = aws_nat_gateway.nat.id
|
||||
#}
|
||||
|
||||
# Route table associations for both Public subnet
|
||||
resource "aws_route_table_association" "public" {
|
||||
count = length(var.public_subnets_cidr)
|
||||
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
|
||||
route_table_id = aws_route_table.public.id
|
||||
}
|
||||
|
||||
resource "aws_route_table_association" "private" {
|
||||
count = length(var.private_subnets_cidr)
|
||||
subnet_id = element(aws_subnet.private_subnet.*.id, count.index)
|
||||
route_table_id = aws_route_table.private.id
|
||||
}
|
Reference in New Issue
Block a user