Skip to content

Commit 4f55c0c

Browse files
authored
Merge pull request #21 from Aswin-Vijayan/master
[TEC-56][Add]- Added conditions to nat-gateway
2 parents 9c0fe72 + 5d547a1 commit 4f55c0c

File tree

11 files changed

+333
-119
lines changed

11 files changed

+333
-119
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ terraform destroy \
2222
-backend-config="dynamodb_table=terraform-state-lock"
2323
```
2424

25+
#### VPC Provisioning
26+
27+
cd into the `environments/dev/vpc` directory and run the following commands:
28+
29+
1. Init Terraform in the directory `environments/dev/vpc`
30+
31+
```
32+
terraform init
33+
```
34+
2. To preview the changes in code
35+
36+
```
37+
terraform plan -var-file=../../../vars/dev/vpc.tfvars
38+
```
39+
3. To apply the changes
40+
41+
```
42+
terraform apply -var-file=../../../vars/dev/vpc.tfvars
43+
```
44+
4. To destroy the resources created using the code
45+
46+
```
47+
terraform destroy -var-file=../../../vars/dev/vpc.tfvars
48+
49+
2550
#### RDS Provisioning
2651
2752
cd into the `environments/dev/rds` directory and run the following commands:

environments/dev/vpc/main.tf

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ provider "aws" {
77

88
module "vpc" {
99
source = "../../../modules/vpc"
10-
name = "eks-vpc"
11-
region = "us-west-2"
12-
project = "EKS Demo"
13-
environment = "dev"
14-
vpc_cidr_block = "10.0.0.0/16"
15-
public_subnet_cidr_blocks = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
16-
app_subnet_cidr_blocks = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
17-
db_subnet_cidr_blocks = ["10.0.7.0/24", "10.0.8.0/24", "10.0.9.0/24"]
18-
management_subnet_cidr_blocks = ["10.0.10.0/24", "10.0.11.0/24", "10.0.12.0/24"]
19-
availability_zones = ["us-west-2a", "us-west-2b", "us-west-2c"]
10+
region = var.region
11+
vpc_cidr_block = var.vpc_cidr_block
12+
instance_tenancy = var.instance_tenancy
13+
enable_dns_support = var.enable_dns_support
14+
enable_dns_hostnames = var.enable_dns_hostnames
15+
domain = var.domain
16+
create_nat_gateway = var.create_nat_gateway
17+
destination_cidr_block = var.destination_cidr_block
18+
map_public_ip_on_launch = var.map_public_ip_on_launch
19+
public_subnet_cidr_blocks = var.public_subnet_cidr_blocks
20+
app_subnet_cidr_blocks = var.app_subnet_cidr_blocks
21+
db_subnet_cidr_blocks = var.db_subnet_cidr_blocks
22+
management_subnet_cidr_blocks = var.management_subnet_cidr_blocks
23+
availability_zones = var.availability_zones
24+
owner = var.owner
25+
environment = var.environment
26+
cost_center = var.cost_center
27+
application = var.application
2028
}

environments/dev/vpc/variables.tf

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Common Variables
2+
3+
variable "tags" {
4+
default = {}
5+
type = map(string)
6+
description = "Extra tags to attach to the VPC resources"
7+
}
8+
9+
variable "region" {
10+
type = string
11+
description = "Region of the VPC"
12+
}
13+
14+
# VPC Variables
15+
16+
variable "vpc_cidr_block" {
17+
type = string
18+
description = "CIDR block for the VPC"
19+
}
20+
21+
# Subnet Varaibles
22+
23+
variable "public_subnet_cidr_blocks" {
24+
type = list(any)
25+
description = "List of public subnet CIDR blocks"
26+
}
27+
28+
variable "app_subnet_cidr_blocks" {
29+
type = list(any)
30+
description = "List of application subnet CIDR blocks"
31+
}
32+
33+
variable "db_subnet_cidr_blocks" {
34+
type = list(any)
35+
description = "List of Database subnet CIDR blocks"
36+
}
37+
38+
variable "management_subnet_cidr_blocks" {
39+
type = list(any)
40+
description = "List of management subnet CIDR blocks"
41+
}
42+
43+
variable "availability_zones" {
44+
type = list(any)
45+
description = "List of availability zones"
46+
}
47+
48+
variable "create_nat_gateway" {
49+
type = bool
50+
description = "whether to create a NAT gateway or not"
51+
}
52+
53+
variable "owner" {
54+
type = string
55+
description = "Name of owner"
56+
}
57+
58+
variable "environment" {
59+
type = string
60+
description = "The environment name for the resources."
61+
}
62+
63+
variable "cost_center" {
64+
type = string
65+
description = "Name of cost-center for this alb-asg"
66+
}
67+
68+
variable "application" {
69+
type = string
70+
description = "Name of the application"
71+
}
72+
73+
variable "instance_tenancy" {
74+
type = string
75+
description = "Set instance-tenancy"
76+
}
77+
78+
variable "enable_dns_support" {
79+
type = bool
80+
description = "whether to enable DNS support or not"
81+
}
82+
83+
variable "enable_dns_hostnames" {
84+
type = bool
85+
description = "whether to enable DNS hostnames or not"
86+
}
87+
88+
variable "domain" {
89+
type = string
90+
description = "Set the domain of eip"
91+
}
92+
93+
variable "destination_cidr_block" {
94+
type = string
95+
description = "Set the destination cidr block"
96+
}
97+
98+
variable "map_public_ip_on_launch" {
99+
type = bool
100+
description = "whether to map public ip on launch or not"
101+
}
102+
103+

modules/vpc/Untitled-1

Whitespace-only changes.

modules/vpc/internet-gateway.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ resource "aws_internet_gateway" "main" {
33

44
tags = merge(
55
{
6-
Name = "InternetGateway",
7-
Project = var.project,
8-
Environment = var.environment
6+
Name = "${var.environment}-${var.application}-internet-gateway",
7+
Environment = var.environment,
8+
Owner = var.owner,
9+
CostCenter = var.cost_center,
10+
Application = var.application
911
},
1012
var.tags
1113
)

modules/vpc/nat-gateway.tf

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
resource "aws_eip" "main" {
2-
count = 3
3-
domain = "vpc"
2+
count = var.create_nat_gateway ? 1 : 0
3+
domain = var.domain
44
}
55

66
resource "aws_nat_gateway" "main" {
7-
count = length(var.subnet_ids)
8-
allocation_id = aws_eip.main[count.index].id
7+
count = var.create_nat_gateway ? 1 : 0
8+
allocation_id = aws_eip.main[0].id
99
subnet_id = aws_subnet.public[0].id
10+
depends_on = [aws_internet_gateway.main]
1011

11-
tags = merge(
12+
tags = merge(
1213
{
13-
Name = "NATGateway",
14-
Project = var.project,
15-
Environment = var.environment
14+
Name = "${var.environment}-${var.application}-nat-gateway",
15+
Environment = var.environment,
16+
Owner = var.owner,
17+
CostCenter = var.cost_center,
18+
Application = var.application
1619
},
1720
var.tags
1821
)
19-
}
22+
}

modules/vpc/route-tables.tf

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,85 @@ resource "aws_route_table" "public" {
33

44
tags = merge(
55
{
6-
Name = "PublicRouteTable",
7-
Project = var.project,
8-
Environment = var.environment
6+
Name = "${var.environment}-${var.application}-public-route-table",
7+
Environment = var.environment,
8+
Owner = var.owner,
9+
CostCenter = var.cost_center,
10+
Application = var.application
911
},
1012
var.tags
1113
)
1214
}
1315

1416
resource "aws_route" "public" {
1517
route_table_id = aws_route_table.public.id
16-
destination_cidr_block = "0.0.0.0/0"
18+
destination_cidr_block = var.destination_cidr_block
1719
gateway_id = aws_internet_gateway.main.id
1820
}
1921

20-
21-
resource "aws_route_table_association" "public" {
22-
count = length(var.public_subnet_cidr_blocks)
23-
subnet_id = aws_subnet.public[count.index].id
24-
route_table_id = aws_route_table.public.id
25-
}
26-
2722
resource "aws_route_table" "app" {
2823
vpc_id = aws_vpc.main.id
2924

3025
tags = merge(
3126
{
32-
Name = "AppRouteTable",
33-
Project = var.project,
34-
Environment = var.environment
27+
Name = "${var.environment}-${var.application}-app-route-table",
28+
Environment = var.environment,
29+
Owner = var.owner,
30+
CostCenter = var.cost_center,
31+
Application = var.application
3532
},
3633
var.tags
3734
)
3835
}
3936

4037
resource "aws_route" "app" {
41-
count = length(aws_nat_gateway.main)
42-
route_table_id = aws_route_table.app.id
43-
destination_cidr_block = "0.0.0.0/0"
44-
gateway_id = aws_nat_gateway.main[count.index].id
38+
count = var.create_nat_gateway ? 1 : 0
39+
route_table_id = aws_route_table.app.id
40+
destination_cidr_block = var.destination_cidr_block
41+
nat_gateway_id = aws_nat_gateway.main[count.index].id
4542
}
4643

47-
resource "aws_route_table_association" "app" {
48-
count = length(var.app_subnet_cidr_blocks)
49-
subnet_id = aws_subnet.app[count.index].id
50-
route_table_id = aws_route_table.app.id
51-
}
5244

53-
#
5445
resource "aws_route_table" "db" {
5546
vpc_id = aws_vpc.main.id
5647

5748
tags = merge(
5849
{
59-
Name = "DbRouteTable",
60-
Project = var.project,
61-
Environment = var.environment
50+
Name = "${var.environment}-${var.application}-db-route-table",
51+
Environment = var.environment,
52+
Owner = var.owner,
53+
CostCenter = var.cost_center,
54+
Application = var.application
6255
},
6356
var.tags
6457
)
6558
}
6659

6760
resource "aws_route" "db" {
68-
count = length(aws_nat_gateway.main)
69-
route_table_id = aws_route_table.db.id
70-
destination_cidr_block = "0.0.0.0/0"
71-
gateway_id = aws_nat_gateway.main[count.index].id
72-
}
73-
74-
resource "aws_route_table_association" "db" {
75-
count = length(var.db_subnet_cidr_blocks)
76-
subnet_id = aws_subnet.db[count.index].id
77-
route_table_id = aws_route_table.db.id
61+
count = var.create_nat_gateway ? 1 : 0
62+
route_table_id = aws_route_table.db.id
63+
destination_cidr_block = var.destination_cidr_block
64+
nat_gateway_id = aws_nat_gateway.main[count.index].id
7865
}
7966

8067
resource "aws_route_table" "management" {
8168
vpc_id = aws_vpc.main.id
8269

8370
tags = merge(
8471
{
85-
Name = "ManagementRouteTable",
86-
Project = var.project,
87-
Environment = var.environment
72+
Name = "${var.environment}-${var.application}-management-route-table",
73+
Environment = var.environment,
74+
Owner = var.owner,
75+
CostCenter = var.cost_center,
76+
Application = var.application
8877
},
8978
var.tags
9079
)
9180
}
9281

9382
resource "aws_route" "management" {
94-
count = length(aws_nat_gateway.main)
95-
route_table_id = aws_route_table.management.id
96-
destination_cidr_block = "0.0.0.0/0"
97-
gateway_id = aws_nat_gateway.main[count.index].id
98-
}
99-
100-
resource "aws_route_table_association" "management" {
101-
count = length(var.management_subnet_cidr_blocks)
102-
subnet_id = aws_subnet.management[count.index].id
103-
route_table_id = aws_route_table.management.id
83+
count = var.create_nat_gateway ? 1 : 0
84+
route_table_id = aws_route_table.management.id
85+
destination_cidr_block = var.destination_cidr_block
86+
nat_gateway_id = aws_nat_gateway.main[count.index].id
10487
}

0 commit comments

Comments
 (0)