Skip to content

Commit f60cb2a

Browse files
committed
Created module for aws vpc terraform
1 parent add3c36 commit f60cb2a

File tree

17 files changed

+346
-1
lines changed

17 files changed

+346
-1
lines changed

modules/network/vpc/main.tf

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
resource "aws_vpc" "vpc" {
2+
cidr_block = var.vpc_cidr
3+
enable_dns_support = var.vpc_enable_dns_support
4+
enable_dns_hostnames = var.vpc_enable_dns_hostnames
5+
tags = merge(var.vpc_tags, tomap({"Name" = var.short_env}))
6+
}
7+
8+
# Internet-gateway
9+
resource "aws_internet_gateway" "igw" {
10+
vpc_id = aws_vpc.vpc.id
11+
tags = merge(var.vpc_tags, tomap({"Name" = var.short_env}))
12+
}
13+
14+
#EIP for Nat attaching
15+
resource "aws_eip" "nat" {
16+
domain = "vpc"
17+
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-nat"}))
18+
}
19+
20+
#Nat gateway setup
21+
resource "aws_nat_gateway" "nat-gw" {
22+
allocation_id = aws_eip.nat.id
23+
subnet_id = aws_subnet.public[0].id
24+
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}"}))
25+
}
26+
27+
resource "aws_subnet" "public" {
28+
count = length(var.public_subnets)
29+
cidr_block = var.public_subnets[count.index].CIDR_BLOCK
30+
vpc_id = aws_vpc.vpc.id
31+
availability_zone = var.public_subnets[count.index].AVAILABILITY_ZONE
32+
map_public_ip_on_launch = true
33+
tags = merge(var.vpc_tags, tomap({"Name" = var.public_subnets[count.index].NAME}))
34+
}
35+
36+
resource "aws_subnet" "private" {
37+
count = length(var.private_subnets)
38+
cidr_block = var.private_subnets[count.index].CIDR_BLOCK
39+
vpc_id = aws_vpc.vpc.id
40+
availability_zone = var.private_subnets[count.index].AVAILABILITY_ZONE
41+
tags = merge(var.vpc_tags, tomap({"Name" = var.private_subnets[count.index].NAME}))
42+
}
43+
44+
resource "aws_security_group" "default" {
45+
name = "${var.short_env}-default"
46+
description = "Default SG to alllow traffic from the VPC"
47+
vpc_id = aws_vpc.vpc.id
48+
depends_on = [
49+
aws_vpc.vpc
50+
]
51+
52+
ingress {
53+
from_port = "0"
54+
to_port = "0"
55+
protocol = "-1"
56+
self = true
57+
}
58+
59+
egress {
60+
from_port = "0"
61+
to_port = "0"
62+
protocol = "-1"
63+
cidr_blocks = ["0.0.0.0/0"]
64+
}
65+
66+
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-default"}))
67+
}

modules/network/vpc/outputs.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "id" { value = aws_vpc.vpc.id }
2+
output "cidr_block" { value = aws_vpc.vpc.cidr_block }
3+
output "igw" { value = aws_internet_gateway.igw.id }
4+
output "nat-gw" { value = aws_nat_gateway.nat-gw.id }
5+
output "public_ids" { value = aws_subnet.public.*.id }
6+
output "private_ids" { value = aws_subnet.private.*.id }
7+
output "public-route-table-id" { value = aws_route_table.public-routes.id }
8+
output "private-route-table-id" { value = aws_route_table.private-routes.id }

modules/network/vpc/route_table.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "aws_route_table" "public-routes" {
2+
vpc_id = aws_vpc.vpc.id
3+
route {
4+
cidr_block = "0.0.0.0/0"
5+
gateway_id = aws_internet_gateway.igw.id
6+
}
7+
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-public"}))
8+
}
9+
10+
resource "aws_route_table" "private-routes" {
11+
vpc_id = aws_vpc.vpc.id
12+
route {
13+
cidr_block = "0.0.0.0/0"
14+
nat_gateway_id = aws_nat_gateway.nat-gw.id
15+
}
16+
tags = merge(var.vpc_tags, tomap({"Name" = "${var.short_env}-private"}))
17+
}
18+
19+
resource "aws_route_table_association" "public-route-assoc" {
20+
count = length(aws_subnet.public.*.id)
21+
subnet_id = element(aws_subnet.public.*.id[*], count.index)
22+
23+
route_table_id = aws_route_table.public-routes.id
24+
}
25+
26+
resource "aws_route_table_association" "private-route-assoc" {
27+
count = length(aws_subnet.private.*.id)
28+
subnet_id = element(aws_subnet.private.*.id[*], count.index)
29+
route_table_id = aws_route_table.private-routes.id
30+
}
31+

modules/network/vpc/variables.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
variable "vpc_cidr" {}
3+
variable "vpc_enable_dns_support" {}
4+
variable "vpc_enable_dns_hostnames" {}
5+
variable "vpc_tags" {}
6+
variable "short_env" {}
7+
variable "public_subnets" {
8+
type = list(object({
9+
CIDR_BLOCK = string
10+
AVAILABILITY_ZONE = string
11+
NAME = string
12+
}))
13+
}
14+
15+
variable "private_subnets" {
16+
type = list(object({
17+
CIDR_BLOCK = string
18+
AVAILABILITY_ZONE = string
19+
NAME = string
20+
}))
21+
}

modules/vpc/main.tf

Lines changed: 0 additions & 1 deletion
This file was deleted.

templates/vpc copy/locals.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
locals {
4+
common_tags = {
5+
"App" = var.APPLICATION
6+
"Environment" = var.LONG_ENV
7+
"Env" = var.SHORT_ENV
8+
"BU" = var.BUSINESS_UNIT
9+
"BUSubcategory" = var.BUSINESS_UNIT_SUBCATEGORY
10+
"BUEmail" = var.BUSINESS_UNIT_EMAIL
11+
"CC" = var.COST_CENTRE
12+
"AwsAccountShort" = var.AWS_ACCOUNT_SHORT
13+
"AwsAccount" = var.AWS_ACCOUNT
14+
"AwsAccountId" = data.aws_caller_identity.current.account_id
15+
"ManagedBy" = "terraform"
16+
}
17+
}

templates/vpc copy/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module "vpc" {
2+
source = "../../../..//modules/vpc/vpc_new"
3+
for_each = var.VPC_LIST
4+
vpc_cidr = each.value.VPC_CIDR
5+
vpc_enable_dns_support = each.value.VPC_ENABLE_DNS_SUPPORT
6+
vpc_enable_dns_hostnames = each.value.VPC_ENABLE_DNS_HOSTNAMES
7+
vpc_tags = local.common_tags
8+
short_env = var.SHORT_ENV
9+
public_subnets = each.value.PUBLIC_SUBNETS
10+
private_subnets = each.value.PRIVATE_SUBNETS
11+
}

templates/vpc copy/outputs.tf

Whitespace-only changes.

templates/vpc copy/variables.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
variable "LONG_ENV" {}
2+
variable "SHORT_ENV" {}
3+
variable "APPLICATION" {}
4+
variable "BUSINESS_UNIT" {}
5+
variable "BUSINESS_UNIT_SUBCATEGORY" {}
6+
variable "BUSINESS_UNIT_EMAIL" {}
7+
variable "COST_CENTRE" {}
8+
variable "AWS_ACCOUNT_SHORT" {}
9+
variable "AWS_ACCOUNT" {}
10+
11+
variable "VPC_LIST" {
12+
type = map(object({
13+
VPC_CIDR = string
14+
VPC_ENABLE_DNS_SUPPORT = optional(bool, false)
15+
VPC_ENABLE_DNS_HOSTNAMES = optional(bool, false)
16+
PUBLIC_SUBNETS = optional(list(object({
17+
CIDR_BLOCK = string
18+
AVAILABILITY_ZONE = string
19+
NAME = string
20+
})), [])
21+
PRIVATE_SUBNETS = optional(list(object({
22+
CIDR_BLOCK = string
23+
AVAILABILITY_ZONE = string
24+
NAME = string
25+
})), [])
26+
}))
27+
}

templates/vpc/locals.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
locals {
4+
common_tags = {
5+
"App" = var.APPLICATION
6+
"Environment" = var.LONG_ENV
7+
"Env" = var.SHORT_ENV
8+
"BU" = var.BUSINESS_UNIT
9+
"BUSubcategory" = var.BUSINESS_UNIT_SUBCATEGORY
10+
"BUEmail" = var.BUSINESS_UNIT_EMAIL
11+
"CC" = var.COST_CENTRE
12+
"AwsAccountShort" = var.AWS_ACCOUNT_SHORT
13+
"AwsAccount" = var.AWS_ACCOUNT
14+
"AwsAccountId" = data.aws_caller_identity.current.account_id
15+
"ManagedBy" = "terraform"
16+
"Name" = var.VPC_NAME
17+
}
18+
}

0 commit comments

Comments
 (0)