chrisluiz16

Untitled

May 8th, 2023
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.65 KB | Source Code | 0 0
  1. # Fetaching the details of the availablity zones
  2.  
  3. data "aws_availability_zones" "az" {
  4.   state = "available"
  5. }
  6.  
  7. # creating VPC
  8.  
  9. resource "aws_vpc" "main" {
  10.   cidr_block           = var.vpc_cidr
  11.   instance_tenancy     = "default"
  12.   enable_dns_support   = true
  13.   enable_dns_hostnames = true
  14.   tags = {
  15.     Name = "${var.project_name}-${var.project_env}"
  16.   }
  17. }
  18.  
  19. #Creating IGW
  20.  
  21. resource "aws_internet_gateway" "igw" {
  22.   vpc_id = aws_vpc.main.id
  23.   tags = {
  24.     Name = "${var.project_name}-${var.project_env}"
  25.   }
  26. }
  27. # Creating subnets
  28.  
  29. resource "aws_subnet" "public" {
  30.   count                   = 3
  31.   vpc_id                  = aws_vpc.main.id
  32.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index)
  33.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  34.   map_public_ip_on_launch = true
  35.   tags = {
  36.     "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
  37.  
  38.   }
  39. }
  40.  
  41. resource "aws_subnet" "private" {
  42.   count                   = 3
  43.   vpc_id                  = aws_vpc.main.id
  44.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
  45.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  46.   map_public_ip_on_launch = false
  47.   tags = {
  48.     "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
  49.  
  50.   }
  51. }
  52.  
  53. # creating elastic IP for NAT G/W
  54.  
  55. resource "aws_eip" "ip" {
  56.   vpc = true
  57.   tags = {
  58.     "Name" = "${var.project_name}-${var.project_env}-"
  59.   }
  60. }
  61.  
  62.  
  63. # creating NAT gateway
  64.  
  65. resource "aws_nat_gateway" "nat" {
  66.   allocation_id = aws_eip.ip.id
  67.   subnet_id     = aws_subnet.public[0].id
  68.   tags = {
  69.     "Name" = "${var.project_name}-${var.project_env}"
  70.   }
  71.  
  72.   depends_on = [aws_internet_gateway.igw]
  73.  
  74. }
  75.  
  76. # Creating Route table
  77.  
  78. resource "aws_route_table" "public" {
  79.   vpc_id = aws_vpc.main.id
  80.  
  81.   route {
  82.     cidr_block = "0.0.0.0/0"
  83.     gateway_id = aws_internet_gateway.igw.id
  84.   }
  85.   tags = {
  86.     "Name" = "${var.project_name}-${var.project_env}-public"
  87.   }
  88. }
  89. resource "aws_route_table" "private" {
  90.   vpc_id = aws_vpc.main.id
  91.  
  92.   route {
  93.     cidr_block     = "0.0.0.0/0"
  94.     nat_gateway_id = aws_nat_gateway.nat.id
  95.   }
  96.   tags = {
  97.     "Name" = "${var.project_name}-${var.project_env}-private"
  98.   }
  99. }
  100.  
  101.  
  102. #Association with the route table
  103.  
  104. resource "aws_route_table_association" "public" {
  105.   count          = 3
  106.   subnet_id      = aws_subnet.public["${count.index}"].id
  107.   route_table_id = aws_route_table.public.id
  108. }
  109.  
  110. resource "aws_route_table_association" "private" {
  111.   count          = 3
  112.   subnet_id      = aws_subnet.private["${count.index}"].id
  113.   route_table_id = aws_route_table.private.id
  114. }
Advertisement
Add Comment
Please, Sign In to add comment