chrisluiz16

Untitled

May 15th, 2023
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.84 KB | None | 0 0
  1. provider "aws" {
  2.   region     = "ap-south-1"
  3.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  4.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  5.   default_tags {
  6.     tags = {
  7.  
  8.       "Env"     = var.project_env,
  9.       "Project" = var.project_name
  10.     }
  11.   }
  12. }
  13.  
  14.  
  15. # Should you choose to have a nat gateway to allow internet connectivity on private subnet
  16.  
  17. variable "nat" {
  18.   type    = bool
  19.   default = "false"
  20.  
  21. }
  22.  
  23.  
  24. # Fetaching the details of the availablity zones
  25.  
  26. data "aws_availability_zones" "az" {
  27.   state = "available"
  28. }
  29.  
  30. locals {
  31.   az = length(data.aws_availability_zones.az.names)
  32.  
  33. }
  34.  
  35. # creating VPC
  36.  
  37. resource "aws_vpc" "main" {
  38.   cidr_block           = var.vpc_cidr
  39.   instance_tenancy     = "default"
  40.   enable_dns_support   = true
  41.   enable_dns_hostnames = true
  42.   tags = {
  43.     Name = "${var.project_name}-${var.project_env}"
  44.   }
  45. }
  46.  
  47. #Creating IGW
  48.  
  49. resource "aws_internet_gateway" "igw" {
  50.   vpc_id = aws_vpc.main.id
  51.   tags = {
  52.     Name = "${var.project_name}-${var.project_env}"
  53.   }
  54. }
  55. # Creating subnets
  56.  
  57. resource "aws_subnet" "public" {
  58.   count                   = local.az
  59.   vpc_id                  = aws_vpc.main.id
  60.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index)
  61.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  62.   map_public_ip_on_launch = true
  63.   tags = {
  64.     "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
  65.  
  66.   }
  67. }
  68.  
  69. resource "aws_subnet" "private" {
  70.   count                   = local.az
  71.   vpc_id                  = aws_vpc.main.id
  72.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
  73.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  74.   map_public_ip_on_launch = false
  75.   tags = {
  76.     "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
  77.  
  78.   }
  79. }
  80.  
  81. # creating elastic IP for NAT G/W if var =true
  82.  
  83. resource "aws_eip" "ip_true" {
  84.  
  85.   count = var.nat == true ? 1 : 0
  86.   vpc   = true
  87.   tags = {
  88.     "Name" = "${var.project_name}-${var.project_env}-Nat"
  89.   }
  90. }
  91.  
  92.  
  93.  
  94. resource "aws_nat_gateway" "nat_true" {
  95.   count         = var.nat == true ? 1 : 0
  96.   allocation_id = aws_eip.ip_true[0].id
  97.   subnet_id     = aws_subnet.public[0].id
  98.   tags = {
  99.     "Name" = "${var.project_name}-${var.project_env}"
  100.   }
  101.  
  102.   depends_on = [aws_internet_gateway.igw]
  103.  
  104. }
  105.  
  106.  
  107. # Creating Route table
  108.  
  109. resource "aws_route_table" "public" {
  110.   vpc_id = aws_vpc.main.id
  111.  
  112.   route {
  113.     cidr_block = "0.0.0.0/0"
  114.     gateway_id = aws_internet_gateway.igw.id
  115.   }
  116.   tags = {
  117.     "Name" = "${var.project_name}-${var.project_env}-public"
  118.   }
  119. }
  120.  
  121. # Creating route table if var = true
  122.  
  123. resource "aws_route_table" "private_true" {
  124.   count  = var.nat == true ? 1 : 0
  125.   vpc_id = aws_vpc.main.id
  126.  
  127.   route {
  128.     cidr_block     = "0.0.0.0/0"
  129.     nat_gateway_id = aws_nat_gateway.nat_true[0].id
  130.   }
  131.   tags = {
  132.     "Name" = "${var.project_name}-${var.project_env}-private"
  133.   }
  134. }
  135.  
  136.  
  137. # Creating route table if var = false
  138.  
  139. resource "aws_route_table" "private_false" {
  140.   count  = var.nat == false ? 1 : 0
  141.   vpc_id = aws_vpc.main.id
  142.   tags = {
  143.     "Name" = "${var.project_name}-${var.project_env}-private"
  144.   }
  145. }
  146.  
  147.  
  148.  
  149. #Association with the route table
  150.  
  151. resource "aws_route_table_association" "public" {
  152.   count          = local.az
  153.   subnet_id      = aws_subnet.public["${count.index}"].id
  154.   route_table_id = aws_route_table.public.id
  155. }
  156.  
  157. resource "aws_route_table_association" "nat_enabled" {
  158.   count          = var.nat == true ? local.az : 0
  159.   subnet_id      = aws_subnet.private["${count.index}"].id
  160.   route_table_id = aws_route_table.private_true[0].id
  161. }
  162.  
  163. resource "aws_route_table_association" "nat_disabled" {
  164.   count          = var.nat == false ? local.az : 0
  165.   subnet_id      = aws_subnet.private["${count.index}"].id
  166.   route_table_id = aws_route_table.private_false[0].id
  167. }
Advertisement
Add Comment
Please, Sign In to add comment