chrisluiz16

MAIN.tf

May 11th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.42 KB | None | 0 0
  1. ec2-user@DESKTOP-Q259SHJ ~/count/vpc-module (default)$ cat main.tf
  2. # Fetaching the details of the availablity zones
  3.  
  4. data "aws_availability_zones" "az" {
  5.   state = "available"
  6. }
  7.  
  8. # creating VPC
  9.  
  10. resource "aws_vpc" "main" {
  11.   cidr_block           = var.vpc_cidr
  12.   instance_tenancy     = "default"
  13.   enable_dns_support   = true
  14.   enable_dns_hostnames = true
  15.   tags = {
  16.     "Name" = "${var.project_name}-${var.project_enviornment}"
  17.   }
  18. }
  19.  
  20. #Creating IGW
  21.  
  22. resource "aws_internet_gateway" "igw" {
  23.   vpc_id = aws_vpc.main.id
  24.   tags = {
  25.     "Name" = "${var.project_name}-${var.project_enviornment}-vpc"
  26.   }
  27. }
  28.  
  29. #Creating public subnet
  30. resource "aws_subnet" "public" {
  31.   count                   = 3
  32.   cidr_block              = cidrsubnet(var.vpc_cidr, 3, count.index)
  33.   vpc_id                  = aws_vpc.main.id
  34.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  35.   map_public_ip_on_launch = true
  36.   tags = {
  37.     "Name" = "${var.project_name}-${var.project_enviornment}-public-${count.index}"
  38.   }
  39. }
  40.  
  41. #Creating private subnets
  42.  
  43. resource "aws_subnet" "private" {
  44.  
  45.   count             = 3
  46.   vpc_id            = aws_vpc.main.id
  47.   cidr_block        = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
  48.   availability_zone = data.aws_availability_zones.az.names["${count.index}"]
  49.   tags = {
  50.     "Name" = "${var.project_name}-${var.project_enviornment}-private-${count.index}"
  51.   }
  52. }
  53.  
  54.  
  55. # Creating EIP allocation
  56.  
  57. resource "aws_eip" "ip" {
  58.  
  59.   count = var.natgw == true ? 1 : 0
  60.   vpc   = true
  61.   tags = {
  62.     "Name" = "${var.project_name}-${var.project_enviornment}-natip"
  63.   }
  64. }
  65.  
  66. # Creating NAT G/W
  67.  
  68.  
  69. resource "aws_nat_gateway" "example" {
  70.  
  71.   count         = var.natgw == true ? 1 : 0
  72.   allocation_id = aws_eip.ip[0].id
  73.   subnet_id     = aws_subnet.public[1].id
  74.  
  75.   tags = {
  76.     "Name" = "${var.project_name}-${var.project_enviornment}-natgw"
  77.   }
  78.   depends_on = [aws_internet_gateway.igw]
  79. }
  80.  
  81.  
  82. # Creating Public route table
  83.  
  84.  
  85. resource "aws_route_table" "public" {
  86.   vpc_id = aws_vpc.main.id
  87.  
  88.   route {
  89.     cidr_block = "0.0.0.0/0"
  90.     gateway_id = aws_internet_gateway.igw.id
  91.   }
  92.  
  93.   tags = {
  94.     "Name" = "${var.project_name}-${var.project_enviornment}-public"
  95.   }
  96.  
  97. }
  98.  
  99.  
  100. # Creating private route table
  101.  
  102. resource "aws_route_table" "private_true" {
  103.  
  104.   count  = var.natgw == true ? 1 : 0
  105.   vpc_id = aws_vpc.main.id
  106.  
  107.   route {
  108.     cidr_block     = "0.0.0.0/0"
  109.     nat_gateway_id = aws_nat_gateway.example[0].id
  110.   }
  111.  
  112.   tags = {
  113.     "Name" = "${var.project_name}-${var.project_enviornment}-private"
  114.   }
  115. }
  116.  
  117. resource "aws_route_table" "private_false" {
  118.  
  119.   count  = var.natgw == false ? 1 : 0
  120.   vpc_id = aws_vpc.main.id
  121.   tags = {
  122.     "Name" = "${var.project_name}-${var.project_enviornment}-private"
  123.   }
  124. }
  125.  
  126.  
  127. # public route table associations
  128.  
  129. resource "aws_route_table_association" "public" {
  130.   count          = 3
  131.   subnet_id      = aws_subnet.public[count.index].id
  132.   route_table_id = aws_route_table.public.id
  133. }
  134.  
  135.  
  136. # Private route table associations
  137.  
  138. resource "aws_route_table_association" "private_true" {
  139.  
  140.   count    = var.natgw == true ? 3 : 0
  141.   subnet_id      = aws_subnet.private[count.index].id
  142.   route_table_id = aws_route_table.private_true[0].id
  143. }
  144.  
  145.  
  146. resource "aws_route_table_association" "private_false" {
  147.  
  148.   count = var.natgw == false ? 3: 0
  149.   subnet_id      = aws_subnet.private[count.index].id
  150.   route_table_id = aws_route_table.private_false[0].id
  151. }
Advertisement
Add Comment
Please, Sign In to add comment