Skip to content

Commit 76a6382

Browse files
Add multi tier selection option
1 parent 0abf936 commit 76a6382

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

main.tf

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# AWS VPC
2+
provider "aws" {
3+
region = "ap-south-1"
4+
}
5+
26
terraform {
37
required_version = ">= 0.12.0"
48
}
@@ -33,6 +37,7 @@ module "public_subnet" {
3337
cluster_prefix = var.cluster_prefix
3438
cluster_environment = var.cluster_environment
3539
subnet_type = ["public"]
40+
cluster_architecture = var.cluster_architecture
3641
}
3742

3843
# AWS NAT Gateway Module
@@ -53,12 +58,13 @@ module "private_subnet" {
5358
cluster_environment = var.cluster_environment
5459
subnet_bits = var.subnet_bits
5560
subnet_type = ["private", "storage"]
61+
cluster_architecture = var.cluster_architecture
5662
}
5763

5864
# AWS VPC Security Groups Module
5965
module "security_group" {
60-
source = "./modules/security-groups"
61-
vpc_id = aws_vpc.vpc.id
62-
cluster_prefix = var.cluster_prefix
63-
cluster_environment = var.cluster_environment
66+
source = "./modules/security-groups"
67+
vpc_id = aws_vpc.vpc.id
68+
cluster_prefix = var.cluster_prefix
69+
cluster_environment = var.cluster_environment
6470
}

modules/subnets/main.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,44 @@ data "aws_availability_zones" "available_zones" {}
55
module "aws_public_subnet" {
66
source = "./resources"
77
create = contains(var.subnet_type, "public") ? 1 : 0
8+
tier = var.cluster_architecture == "1-tier" ? 1 : 0
89
cluster_prefix = var.cluster_prefix
910
cluster_environment = var.cluster_environment
1011
vpc_id = var.vpc_id
1112
cidr = var.cidr
1213
subnet_bits = var.subnet_bits
1314
subnet_type = "public"
15+
cluster_architecture = var.cluster_architecture
1416
}
1517

1618
# AWS Private Subnets
1719
module "aws_private_subnet" {
1820
source = "./resources"
1921
create = contains(var.subnet_type, "private") ? 1 : 0
22+
tier = var.cluster_architecture == "2-tier" ? 1 : 0
2023
cluster_prefix = var.cluster_prefix
2124
cluster_environment = var.cluster_environment
2225
vpc_id = var.vpc_id
2326
cidr = var.cidr
2427
offset = length(data.aws_availability_zones.available_zones.names)
2528
subnet_bits = var.subnet_bits
2629
subnet_type = "private"
30+
cluster_architecture = var.cluster_architecture
2731
}
2832

2933
# AWS Storage Subnets
3034
module "aws_storage_subnet" {
3135
source = "./resources"
3236
create = contains(var.subnet_type, "storage") ? 1 : 0
37+
tier = var.cluster_architecture == "3-tier" ? 1 : 0
3338
cluster_prefix = var.cluster_prefix
3439
cluster_environment = var.cluster_environment
3540
cidr = var.cidr
3641
vpc_id = var.vpc_id
3742
offset = 2 * length(data.aws_availability_zones.available_zones.names)
3843
subnet_bits = var.subnet_bits
3944
subnet_type = "storage"
45+
cluster_architecture = var.cluster_architecture
4046
}
4147

4248
# AWS Route Tables - Public Route
@@ -49,7 +55,7 @@ resource "aws_route" "public_route" {
4955

5056
# AWS Route Tables - Private Route
5157
resource "aws_route" "private_route" {
52-
count = contains(var.subnet_type, "private") ? length(data.aws_availability_zones.available_zones.names) : 0
58+
count = var.cluster_architecture == "2-tier" || var.cluster_architecture == "3-tier" && contains(var.subnet_type, "private") ? length(data.aws_availability_zones.available_zones.names) : 0
5359
route_table_id = module.aws_private_subnet.route_table_ids[count.index]
5460
destination_cidr_block = "0.0.0.0/0"
5561
nat_gateway_id = var.aws_nat_gateway_id[count.index]

modules/subnets/resources/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data "aws_availability_zones" "available_zones" {}
44
# AWS Subnets
55
resource "aws_subnet" "subnets" {
66
vpc_id = var.vpc_id
7-
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
7+
count = var.create > 0 && var.tier > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
88
cidr_block = cidrsubnet(var.cidr, var.subnet_bits, var.offset + count.index)
99
availability_zone = data.aws_availability_zones.available_zones.names[count.index]
1010

@@ -18,7 +18,7 @@ resource "aws_subnet" "subnets" {
1818
# AWS Route Tables
1919
resource "aws_route_table" "route_table" {
2020
vpc_id = var.vpc_id
21-
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
21+
count = var.create > 0 && var.tier > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
2222

2323
tags = {
2424
Name = "${var.cluster_prefix}-${var.subnet_type}-${count.index + 1}"
@@ -29,7 +29,7 @@ resource "aws_route_table" "route_table" {
2929

3030
# AWS Route Table - Subnet Association
3131
resource "aws_route_table_association" "subnet_association" {
32-
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
32+
count = var.create > 0 && var.tier > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
3333
subnet_id = element(aws_subnet.subnets.*.id, count.index)
3434
route_table_id = element(aws_route_table.route_table.*.id, count.index)
3535
}

modules/subnets/resources/variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ variable "cluster_environment" {
1919
type = string
2020
}
2121

22+
variable "cluster_architecture" {
23+
description = "To apply generic cluster_architecture to AWS VPC Resources"
24+
type = string
25+
}
26+
2227
variable "cidr" {
2328
description = "CIDR block value is the size of the VPC"
2429
type = string
@@ -29,6 +34,11 @@ variable "create" {
2934
type = string
3035
}
3136

37+
variable "tier" {
38+
description = "tier"
39+
type = string
40+
}
41+
3242
variable "subnet_bits" {
3343
description = "Subnet bits for cidrsubnet interpolation or Size we need to define for the Subnet (cidr of VPC + Subnet bits)"
3444
type = string

modules/subnets/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ variable "cluster_environment" {
2525
type = string
2626
}
2727

28+
variable "cluster_architecture" {
29+
description = "To apply generic cluster_environment to AWS VPC Resources"
30+
type = string
31+
}
32+
2833
variable "subnet_bits" {
2934
description = "Subnet bits for cidrsubnet interpolation or Size we need to define for the Subnet (cidr of VPC + Subnet bits)"
3035
type = string

variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22
variable "cluster_prefix" {
33
description = "To apply generic naming to AWS VPC Resources"
44
type = string
5+
default = "copper"
56
}
67

78
variable "cluster_environment" {
9+
description = "To apply generic environment to AWS VPC Resources"
10+
type = string
11+
default = "devops"
12+
}
13+
14+
variable "cluster_architecture" {
815
description = "To apply generic cluster_environment to AWS VPC Resources"
916
type = string
17+
default = "1-tier"
1018
}
1119

1220
variable "cidr" {
1321
description = "CIDR block value to define the size of the AWS VPC"
1422
type = string
23+
default = "10.0.0.0/20"
1524
}
1625

1726
variable "subnet_bits" {
1827
description = "Subnet bits for cidrsubnet interpolation or Size we need to define for the Subnet (cidr of VPC + Subnet bits)"
1928
type = string
29+
default = "4"
2030
}

0 commit comments

Comments
 (0)