Skip to content

Commit ef9aeb2

Browse files
Refactored terraform config
1 parent b31619a commit ef9aeb2

File tree

13 files changed

+67
-116
lines changed

13 files changed

+67
-116
lines changed

README.md

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Hashicorp Terraform AWS VPC Module
22
Terraform AWS VPC Module by Source4Learn(An Opensource Community to learn and share knowledge)
3-
![s4l](s4l.png "Source4Learn")
3+
![Source4Learn](https://github.com/opensource4learn/terraform-aws-vpc/blob/main/s4l.png?raw=true)
44

5-
## AWS VPC Module Usage
6-
This AWS VPC Module will creates 1/2/3 tier resources as per user inputs:
5+
## AWS VPC Module
6+
This AWS VPC Module will create following resources:
77
- Subnets ["Public", "Private", "Storage"]
88
- Route Tables ["Public", "Private", "Storage"]
99
- Security Gruoups
@@ -13,65 +13,17 @@ This AWS VPC Module will creates 1/2/3 tier resources as per user inputs:
1313
- Network ACLs
1414
- VPC Endpoints
1515

16-
Example: Single tier AWS VPC architecture having only Public Subnet with required resources.
16+
## Usage
1717

1818
```terraform
19-
resource "aws_vpc" "vpc" {
20-
cidr_block = "10.0.0.0/20"
21-
22-
tags = {
23-
Name = "my-vpc"
24-
Environment = "my-environment"
25-
}
26-
}
27-
28-
resource "aws_internet_gateway" "igw" {
29-
vpc_id = aws_vpc.vpc.id
30-
31-
tags = {
32-
Name = "my-igw"
33-
Environment = "my-environment"
34-
}
35-
}
36-
37-
module "public_subnet" {
38-
source = "./modules/subnets"
39-
vpc_id = aws_vpc.vpc.id
40-
aws_internet_gateway_id = aws_internet_gateway.igw.id
41-
cidr = "10.0.0.0/20"
42-
subnet_bits = "4"
43-
prefix = "my-subnet"
44-
environment = "my-environment"
45-
subnet_type = ["public"]
46-
}
47-
```
48-
49-
In adadition to above example, users can provision 2/3 tier AWS VPC architecture.
50-
51-
```terraform
52-
module "nat_gateway" {
53-
source = "./modules/nat-gateways"
54-
prefix = "my-nat-gateway"
55-
environment = "my-environment"
56-
public_subnet_ids = module.public_subnet.public_subnet_ids
57-
}
58-
59-
module "private_subnet" {
60-
source = "./modules/subnets"
61-
vpc_id = aws_vpc.vpc.id
62-
aws_nat_gateway_id = module.nat_gateway.nat_gateway_ids
63-
cidr = "10.0.0.0/20"
64-
subnet_bits = "4"
65-
prefix = "my-subnet"
66-
environment = "my-environment"
67-
subnet_type = ["private", "storage"]
68-
}
69-
70-
module "security_group" {
71-
source = "./modules/security-groups"
72-
vpc_id = aws_vpc.vpc.id
73-
prefix = "my-security-group"
74-
environment = "my-environment"
19+
module "vpc" {
20+
source = "opensource4learn/vpc/aws"
21+
version = "0.1.0-alpha"
22+
aws_region = "ap-south-1"
23+
cluster_prefix = "source4learn"
24+
cluster_environment = "production"
25+
cidr = "10.0.0.0/20"
26+
subnet_bits = "4"
7527
}
7628
```
7729

@@ -109,8 +61,8 @@ module "security_group" {
10961
|------|-------------|------|---------|:--------:|
11062
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS Default Region | `string` | n/a | yes |
11163
| <a name="input_cidr"></a> [cidr](#input\_cidr) | CIDR block value to define the size of the AWS VPC | `string` | `"10.0.0.0/20"` | no |
112-
| <a name="input_environment"></a> [environment](#input\_environment) | To apply generic environment to AWS VPC Resources | `string` | n/a | yes |
113-
| <a name="input_prefix"></a> [prefix](#input\_prefix) | To apply generic naming to AWS VPC Resources | `string` | n/a | yes |
64+
| <a name="input_cluster_environment"></a> [cluster_environment](#input\_cluster_environment) | To apply generic cluster_environment to AWS VPC Resources | `string` | n/a | yes |
65+
| <a name="input_cluster_prefix"></a> [cluster_prefix](#input\_cluster_prefix) | To apply generic naming to AWS VPC Resources | `string` | n/a | yes |
11466
| <a name="input_subnet_bits"></a> [subnet\_bits](#input\_subnet\_bits) | Subnet bits for cidrsubnet interpolation or Size we need to define for the Subnet (cidr of VPC + Subnet bits) | `string` | n/a | yes |
11567

11668
## Outputs

main.tf

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ resource "aws_vpc" "vpc" {
88
cidr_block = var.cidr
99

1010
tags = {
11-
Name = "${var.prefix}-${var.environment}"
12-
Environment = var.environment
11+
Name = "${var.cluster_prefix}-${var.cluster_environment}"
12+
Environment = var.cluster_environment
1313
}
1414
}
1515

@@ -18,8 +18,8 @@ resource "aws_internet_gateway" "igw" {
1818
vpc_id = aws_vpc.vpc.id
1919

2020
tags = {
21-
Name = "${var.prefix}-${var.environment}"
22-
Environment = var.environment
21+
Name = "${var.cluster_prefix}-${var.cluster_environment}"
22+
Environment = var.cluster_environment
2323
}
2424
}
2525

@@ -30,16 +30,16 @@ module "public_subnet" {
3030
aws_internet_gateway_id = aws_internet_gateway.igw.id
3131
subnet_bits = var.subnet_bits
3232
cidr = var.cidr
33-
prefix = var.prefix
34-
environment = var.environment
33+
cluster_prefix = var.cluster_prefix
34+
cluster_environment = var.cluster_environment
3535
subnet_type = ["public"]
3636
}
3737

3838
# AWS NAT Gateway Module
3939
module "nat_gateway" {
4040
source = "./modules/nat-gateways"
41-
prefix = var.prefix
42-
environment = var.environment
41+
cluster_prefix = var.cluster_prefix
42+
cluster_environment = var.cluster_environment
4343
public_subnet_ids = module.public_subnet.public_subnet_ids
4444
}
4545

@@ -49,8 +49,8 @@ module "private_subnet" {
4949
vpc_id = aws_vpc.vpc.id
5050
aws_nat_gateway_id = module.nat_gateway.nat_gateway_ids
5151
cidr = var.cidr
52-
prefix = var.prefix
53-
environment = var.environment
52+
cluster_prefix = var.cluster_prefix
53+
cluster_environment = var.cluster_environment
5454
subnet_bits = var.subnet_bits
5555
subnet_type = ["private", "storage"]
5656
}
@@ -59,6 +59,6 @@ module "private_subnet" {
5959
module "security_group" {
6060
source = "./modules/security-groups"
6161
vpc_id = aws_vpc.vpc.id
62-
prefix = var.prefix
63-
environment = var.environment
62+
cluster_prefix = var.cluster_prefix
63+
cluster_environment = var.cluster_environment
6464
}

modules/nat-gateways/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ resource "aws_eip" "eip" {
77
count = length(data.aws_availability_zones.available_zones.names)
88

99
tags = {
10-
Name = "${var.prefix}-${count.index + 1}"
11-
Environment = var.environment
10+
Name = "${var.cluster_prefix}-${count.index + 1}"
11+
Environment = var.cluster_environment
1212
}
1313
}
1414

@@ -19,7 +19,7 @@ resource "aws_nat_gateway" "nat_gateway" {
1919
count = length(data.aws_availability_zones.available_zones.names)
2020

2121
tags = {
22-
Name = "${var.prefix}-${count.index + 1}"
23-
Environment = var.environment
22+
Name = "${var.cluster_prefix}-${count.index + 1}"
23+
Environment = var.cluster_environment
2424
}
2525
}

modules/nat-gateways/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
variable "prefix" {
1+
variable "cluster_prefix" {
22
description = "generic naming resources"
33
type = string
44
}
55

6-
variable "environment" {
7-
description = "To apply generic environment to AWS VPC Resources"
6+
variable "cluster_environment" {
7+
description = "To apply generic cluster_environment to AWS VPC Resources"
88
type = string
99
}
1010

modules/security-groups/main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
module "public_security_group" {
33
source = "./resources"
44
vpc_id = var.vpc_id
5-
prefix = var.prefix
6-
environment = var.environment
5+
cluster_prefix = var.cluster_prefix
6+
cluster_environment = var.cluster_environment
77
sg_type = "public"
88
sg_description = "Allow connections from internet"
99
}
@@ -31,8 +31,8 @@ resource "aws_security_group_rule" "allow_https_inbound_public" {
3131
module "private_security_group" {
3232
source = "./resources"
3333
vpc_id = var.vpc_id
34-
prefix = var.prefix
35-
environment = var.environment
34+
cluster_prefix = var.cluster_prefix
35+
cluster_environment = var.cluster_environment
3636
sg_type = "private"
3737
sg_description = "The private security group to allows inbound traffic from public group"
3838
}
@@ -51,8 +51,8 @@ resource "aws_security_group_rule" "allow_inbound_private" {
5151
module "storage_security_group" {
5252
source = "./resources"
5353
vpc_id = var.vpc_id
54-
prefix = var.prefix
55-
environment = var.environment
54+
cluster_prefix = var.cluster_prefix
55+
cluster_environment = var.cluster_environment
5656
sg_type = "storage"
5757
sg_description = "The storage security group to allows inbound traffic from private group"
5858
}

modules/security-groups/resources/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# AWS Security Group
22
resource "aws_security_group" "security_group" {
3-
name = "${var.prefix}-${var.sg_type}"
3+
name = "${var.cluster_prefix}-${var.sg_type}"
44
description = var.sg_description
55
vpc_id = var.vpc_id
66
revoke_rules_on_delete = true
77

88
tags = {
9-
Name = "${var.prefix}-${var.sg_type}"
9+
Name = "${var.cluster_prefix}-${var.sg_type}"
10+
Environment = var.cluster_environment
1011
Type = var.sg_type
11-
Environment = var.environment
1212
}
1313
}
1414

modules/security-groups/resources/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ variable "vpc_id" {
33
type = string
44
}
55

6-
variable "prefix" {
6+
variable "cluster_prefix" {
77
description = "generic naming resources"
88
type = string
99
}
1010

11-
variable "environment" {
12-
description = "To apply generic environment to AWS VPC Resources"
11+
variable "cluster_environment" {
12+
description = "To apply generic cluster_environment to AWS VPC Resources"
1313
type = string
1414
}
1515

modules/security-groups/variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ variable "vpc_id" {
33
type = string
44
}
55

6-
variable "prefix" {
6+
variable "cluster_prefix" {
77
description = "generic naming resources"
88
type = string
99
}
1010

11-
variable "environment" {
12-
description = "To apply generic environment to AWS VPC Resources"
11+
variable "cluster_environment" {
12+
description = "To apply generic cluster_environment to AWS VPC Resources"
1313
type = string
1414
}
1515

modules/subnets/main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ data "aws_availability_zones" "available_zones" {}
55
module "aws_public_subnet" {
66
source = "./resources"
77
create = contains(var.subnet_type, "public") ? 1 : 0
8-
prefix = var.prefix
9-
environment = var.environment
8+
cluster_prefix = var.cluster_prefix
9+
cluster_environment = var.cluster_environment
1010
vpc_id = var.vpc_id
1111
cidr = var.cidr
1212
subnet_bits = var.subnet_bits
@@ -17,8 +17,8 @@ module "aws_public_subnet" {
1717
module "aws_private_subnet" {
1818
source = "./resources"
1919
create = contains(var.subnet_type, "private") ? 1 : 0
20-
prefix = var.prefix
21-
environment = var.environment
20+
cluster_prefix = var.cluster_prefix
21+
cluster_environment = var.cluster_environment
2222
vpc_id = var.vpc_id
2323
cidr = var.cidr
2424
offset = length(data.aws_availability_zones.available_zones.names)
@@ -30,8 +30,8 @@ module "aws_private_subnet" {
3030
module "aws_storage_subnet" {
3131
source = "./resources"
3232
create = contains(var.subnet_type, "storage") ? 1 : 0
33-
prefix = var.prefix
34-
environment = var.environment
33+
cluster_prefix = var.cluster_prefix
34+
cluster_environment = var.cluster_environment
3535
cidr = var.cidr
3636
vpc_id = var.vpc_id
3737
offset = 2 * length(data.aws_availability_zones.available_zones.names)

modules/subnets/resources/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ resource "aws_subnet" "subnets" {
99
availability_zone = data.aws_availability_zones.available_zones.names[count.index]
1010

1111
tags = {
12-
Name = "${var.prefix}-${var.subnet_type}-${count.index + 1}"
13-
Environment = var.environment
12+
Name = "${var.cluster_prefix}-${var.subnet_type}-${count.index + 1}"
13+
Environment = var.cluster_environment
1414
Type = var.subnet_type
1515
}
1616
}
@@ -21,8 +21,8 @@ resource "aws_route_table" "route_table" {
2121
count = var.create > 0 ? length(data.aws_availability_zones.available_zones.names) : 0
2222

2323
tags = {
24-
Name = "${var.prefix}-${var.subnet_type}-${count.index + 1}"
25-
Environment = var.environment
24+
Name = "${var.cluster_prefix}-${var.subnet_type}-${count.index + 1}"
25+
Environment = var.cluster_environment
2626
Type = var.subnet_type
2727
}
2828
}

0 commit comments

Comments
 (0)