Skip to content

Commit

Permalink
Created module for aws vpc terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
pransh62390 committed Nov 28, 2024
1 parent add3c36 commit f60cb2a
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 1 deletion.
67 changes: 67 additions & 0 deletions modules/network/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = var.vpc_enable_dns_support
enable_dns_hostnames = var.vpc_enable_dns_hostnames
tags = merge(var.vpc_tags, tomap({"Name" = var.short_env}))
}

# Internet-gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id
tags = merge(var.vpc_tags, tomap({"Name" = var.short_env}))
}

#EIP for Nat attaching
resource "aws_eip" "nat" {
domain = "vpc"
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-nat"}))
}

#Nat gateway setup
resource "aws_nat_gateway" "nat-gw" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public[0].id
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}"}))
}

resource "aws_subnet" "public" {
count = length(var.public_subnets)
cidr_block = var.public_subnets[count.index].CIDR_BLOCK
vpc_id = aws_vpc.vpc.id
availability_zone = var.public_subnets[count.index].AVAILABILITY_ZONE
map_public_ip_on_launch = true
tags = merge(var.vpc_tags, tomap({"Name" = var.public_subnets[count.index].NAME}))
}

resource "aws_subnet" "private" {
count = length(var.private_subnets)
cidr_block = var.private_subnets[count.index].CIDR_BLOCK
vpc_id = aws_vpc.vpc.id
availability_zone = var.private_subnets[count.index].AVAILABILITY_ZONE
tags = merge(var.vpc_tags, tomap({"Name" = var.private_subnets[count.index].NAME}))
}

resource "aws_security_group" "default" {
name = "${var.short_env}-default"
description = "Default SG to alllow traffic from the VPC"
vpc_id = aws_vpc.vpc.id
depends_on = [
aws_vpc.vpc
]

ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
self = true
}

egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-default"}))
}
8 changes: 8 additions & 0 deletions modules/network/vpc/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "id" { value = aws_vpc.vpc.id }
output "cidr_block" { value = aws_vpc.vpc.cidr_block }
output "igw" { value = aws_internet_gateway.igw.id }
output "nat-gw" { value = aws_nat_gateway.nat-gw.id }
output "public_ids" { value = aws_subnet.public.*.id }
output "private_ids" { value = aws_subnet.private.*.id }
output "public-route-table-id" { value = aws_route_table.public-routes.id }
output "private-route-table-id" { value = aws_route_table.private-routes.id }
31 changes: 31 additions & 0 deletions modules/network/vpc/route_table.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "aws_route_table" "public-routes" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-public"}))
}

resource "aws_route_table" "private-routes" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat-gw.id
}
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-private"}))
}

resource "aws_route_table_association" "public-route-assoc" {
count = length(aws_subnet.public.*.id)
subnet_id = element(aws_subnet.public.*.id[*], count.index)

route_table_id = aws_route_table.public-routes.id
}

resource "aws_route_table_association" "private-route-assoc" {
count = length(aws_subnet.private.*.id)
subnet_id = element(aws_subnet.private.*.id[*], count.index)
route_table_id = aws_route_table.private-routes.id
}

21 changes: 21 additions & 0 deletions modules/network/vpc/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

variable "vpc_cidr" {}
variable "vpc_enable_dns_support" {}
variable "vpc_enable_dns_hostnames" {}
variable "vpc_tags" {}
variable "short_env" {}
variable "public_subnets" {
type = list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
}))
}

variable "private_subnets" {
type = list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
}))
}
1 change: 0 additions & 1 deletion modules/vpc/main.tf

This file was deleted.

17 changes: 17 additions & 0 deletions templates/vpc copy/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
data "aws_caller_identity" "current" {}

locals {
common_tags = {
"App" = var.APPLICATION
"Environment" = var.LONG_ENV
"Env" = var.SHORT_ENV
"BU" = var.BUSINESS_UNIT
"BUSubcategory" = var.BUSINESS_UNIT_SUBCATEGORY
"BUEmail" = var.BUSINESS_UNIT_EMAIL
"CC" = var.COST_CENTRE
"AwsAccountShort" = var.AWS_ACCOUNT_SHORT
"AwsAccount" = var.AWS_ACCOUNT
"AwsAccountId" = data.aws_caller_identity.current.account_id
"ManagedBy" = "terraform"
}
}
11 changes: 11 additions & 0 deletions templates/vpc copy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "vpc" {
source = "../../../..//modules/vpc/vpc_new"
for_each = var.VPC_LIST
vpc_cidr = each.value.VPC_CIDR
vpc_enable_dns_support = each.value.VPC_ENABLE_DNS_SUPPORT
vpc_enable_dns_hostnames = each.value.VPC_ENABLE_DNS_HOSTNAMES
vpc_tags = local.common_tags
short_env = var.SHORT_ENV
public_subnets = each.value.PUBLIC_SUBNETS
private_subnets = each.value.PRIVATE_SUBNETS
}
Empty file added templates/vpc copy/outputs.tf
Empty file.
27 changes: 27 additions & 0 deletions templates/vpc copy/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
variable "LONG_ENV" {}
variable "SHORT_ENV" {}
variable "APPLICATION" {}
variable "BUSINESS_UNIT" {}
variable "BUSINESS_UNIT_SUBCATEGORY" {}
variable "BUSINESS_UNIT_EMAIL" {}
variable "COST_CENTRE" {}
variable "AWS_ACCOUNT_SHORT" {}
variable "AWS_ACCOUNT" {}

variable "VPC_LIST" {
type = map(object({
VPC_CIDR = string
VPC_ENABLE_DNS_SUPPORT = optional(bool, false)
VPC_ENABLE_DNS_HOSTNAMES = optional(bool, false)
PUBLIC_SUBNETS = optional(list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
})), [])
PRIVATE_SUBNETS = optional(list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
})), [])
}))
}
18 changes: 18 additions & 0 deletions templates/vpc/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
data "aws_caller_identity" "current" {}

locals {
common_tags = {
"App" = var.APPLICATION
"Environment" = var.LONG_ENV
"Env" = var.SHORT_ENV
"BU" = var.BUSINESS_UNIT
"BUSubcategory" = var.BUSINESS_UNIT_SUBCATEGORY
"BUEmail" = var.BUSINESS_UNIT_EMAIL
"CC" = var.COST_CENTRE
"AwsAccountShort" = var.AWS_ACCOUNT_SHORT
"AwsAccount" = var.AWS_ACCOUNT
"AwsAccountId" = data.aws_caller_identity.current.account_id
"ManagedBy" = "terraform"
"Name" = var.VPC_NAME
}
}
10 changes: 10 additions & 0 deletions templates/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module "vpc" {
source = "../../../..//modules/vpc/vpc_new"
vpc_cidr = var.VPC_CIDR
vpc_enable_dns_support = var.VPC_ENABLE_DNS_SUPPORT
vpc_enable_dns_hostnames = var.VPC_ENABLE_DNS_HOSTNAMES
vpc_tags = local.common_tags
short_env = var.SHORT_ENV
public_subnets = var.PUBLIC_SUBNETS
private_subnets = var.PRIVATE_SUBNETS
}
Empty file added templates/vpc/outputs.tf
Empty file.
43 changes: 43 additions & 0 deletions templates/vpc/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
variable "LONG_ENV" {}
variable "SHORT_ENV" {}
variable "APPLICATION" {}
variable "BUSINESS_UNIT" {}
variable "BUSINESS_UNIT_SUBCATEGORY" {}
variable "BUSINESS_UNIT_EMAIL" {}
variable "COST_CENTRE" {}
variable "AWS_ACCOUNT_SHORT" {}
variable "AWS_ACCOUNT" {}
variable "VPC_NAME" {}

variable "VPC_CIDR" {
type = string
}

variable "VPC_ENABLE_DNS_SUPPORT" {
type = bool
default = false
}
variable "VPC_ENABLE_DNS_HOSTNAMES" {
type = bool
default = false
}

variable "PUBLIC_SUBNETS" {
type = list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
}))

default = []
}

variable "PRIVATE_SUBNETS" {
type = list(object({
CIDR_BLOCK = string
AVAILABILITY_ZONE = string
NAME = string
}))

default = []
}
Empty file added test/main.tf
Empty file.
Empty file added test/outputs.tf
Empty file.
Empty file added test/variables.tf
Empty file.
93 changes: 93 additions & 0 deletions tfvars/vpc/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
terraform {
source = "../../../../../..//tftemplates/stage/common/vpc/"
extra_arguments "common_vars" {
commands = ["init","plan", "apply"]
}
}

inputs = {
APPLICATION = "all"
LONG_ENV = "stage"
SHORT_ENV = "stg"
BUSINESS_UNIT = "all"
BUSINESS_UNIT_SUBCATEGORY = ""
BUSINESS_UNIT_EMAIL = ""
COST_CENTRE = ""
AWS_ACCOUNT_SHORT = "ss"
AWS_ACCOUNT = ""

VPC_CIDR = "10.3.0.0/16"
VPC_NAME = "stg"
VPC_ENABLE_DNS_SUPPORT = true
VPC_ENABLE_DNS_HOSTNAMES = true

PUBLIC_SUBNETS = [
{
CIDR_BLOCK = "10.3.0.0/24"
AVAILABILITY_ZONE = "ap-south-1a"
NAME = "stg-public-subnet-1a"
},
{
CIDR_BLOCK = "10.3.1.0/24"
AVAILABILITY_ZONE = "ap-south-1b"
NAME = "stg-public-subnet-1b"
},
{
CIDR_BLOCK = "10.3.4.0/24"
AVAILABILITY_ZONE = "ap-south-1c"
NAME = "stg-public-subnet-1c"
}
]

PRIVATE_SUBNETS = [
{
CIDR_BLOCK = "10.3.2.0/24"
AVAILABILITY_ZONE = "ap-south-1a"
NAME = "stg-private-subnet-1a"
},
{
CIDR_BLOCK = "10.3.3.0/24"
AVAILABILITY_ZONE = "ap-south-1b"
NAME = "stg-private-subnet-1b"
},
{
CIDR_BLOCK = "10.3.5.0/24"
AVAILABILITY_ZONE = "ap-south-1c"
NAME = "stg-private-subnet-1c"
}
]
}

generate "backend" {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
terraform {
backend "s3" {
bucket = "staging-setup-cloud-platform"
key = "stage.terraform.tfstate/ap-south-1/common/vpc/terraform.tfstate"
region = "ap-south-1"
}
}
EOF
}

generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"

contents = <<EOF
terraform {
required_version = ">= 1.4.2"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.21.0"
}
}
}
provider "aws" {
region = "ap-south-1"
}
EOF
}

0 comments on commit f60cb2a

Please sign in to comment.