Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- provider "aws" {
- region = "ap-south-1"
- access_key = "AKIA5YKGNPEXMXI3AEUM"
- secret_key = "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"
- default_tags {
- tags = {
- "Env" = var.project_env,
- "Project" = var.project_name
- }
- }
- }
- # Should you choose to have a nat gateway to allow internet connectivity on private subnet
- variable "nat" {
- type = bool
- default = "false"
- }
- # Fetaching the details of the availablity zones
- data "aws_availability_zones" "az" {
- state = "available"
- }
- locals {
- az = length(data.aws_availability_zones.az.names)
- }
- # creating VPC
- resource "aws_vpc" "main" {
- cidr_block = var.vpc_cidr
- instance_tenancy = "default"
- enable_dns_support = true
- enable_dns_hostnames = true
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- #Creating IGW
- resource "aws_internet_gateway" "igw" {
- vpc_id = aws_vpc.main.id
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- # Creating subnets
- resource "aws_subnet" "public" {
- count = local.az
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
- }
- }
- resource "aws_subnet" "private" {
- count = local.az
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = false
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
- }
- }
- # creating elastic IP for NAT G/W if var =true
- resource "aws_eip" "ip_true" {
- count = var.nat == true ? 1 : 0
- vpc = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-Nat"
- }
- }
- resource "aws_nat_gateway" "nat_true" {
- count = var.nat == true ? 1 : 0
- allocation_id = aws_eip.ip_true[0].id
- subnet_id = aws_subnet.public[0].id
- tags = {
- "Name" = "${var.project_name}-${var.project_env}"
- }
- depends_on = [aws_internet_gateway.igw]
- }
- # Creating Route table
- resource "aws_route_table" "public" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- gateway_id = aws_internet_gateway.igw.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public"
- }
- }
- # Creating route table if var = true
- resource "aws_route_table" "private_true" {
- count = var.nat == true ? 1 : 0
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- nat_gateway_id = aws_nat_gateway.nat_true[0].id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private"
- }
- }
- # Creating route table if var = false
- resource "aws_route_table" "private_false" {
- count = var.nat == false ? 1 : 0
- vpc_id = aws_vpc.main.id
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private"
- }
- }
- #Association with the route table
- resource "aws_route_table_association" "public" {
- count = local.az
- subnet_id = aws_subnet.public["${count.index}"].id
- route_table_id = aws_route_table.public.id
- }
- resource "aws_route_table_association" "nat_enabled" {
- count = var.nat == true ? local.az : 0
- subnet_id = aws_subnet.private["${count.index}"].id
- route_table_id = aws_route_table.private_true[0].id
- }
- resource "aws_route_table_association" "nat_disabled" {
- count = var.nat == false ? local.az : 0
- subnet_id = aws_subnet.private["${count.index}"].id
- route_table_id = aws_route_table.private_false[0].id
- }
Advertisement
Add Comment
Please, Sign In to add comment