Skip to content

Commit

Permalink
Add custom vpc_cidr_index variable to avoid VPC CIDR collisions with …
Browse files Browse the repository at this point in the history
…multiple environments
  • Loading branch information
Aaron Carlucci committed Nov 22, 2023
1 parent 6632d43 commit 7ceefa1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion terraform/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ locals {
Application = "aws-swan-demo"
Environment = var.environment
}
namespace = "aws-swan-demo-${var.environment}"
namespace = "aws-swan-demo-${var.environment}"
}
12 changes: 9 additions & 3 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
variable "aws_region" {
default = "us-east-1"
description = "The AWS region name in which the main infrastructure should be deployed"
description = "The AWS region name in which the main infrastructure should be deployed."
type = string
}

variable "aws_replication_region" {
default = "us-west-2"
description = "The AWS replication region where resources are provisioned for failover"
description = "The AWS replication region where resources are provisioned for failover."
type = string
}

variable "vpc_cidr_index" {
default = 0
description = "The number of the second CIDR IP address segment to act as an index for multiple environment support. The default CIDR range is 10.0.0.0/16, so setting this to 1 would initialize the VPC to a CIDR range of 10.1.0.0/16. This is a negotiated stopgap solution to allow for the provisioning of multiple instances of the application in one region and avoid CIDR collisions."
type = number
}

variable "environment" {
description = "Name of the provisioned environment for namespacing purposes"
description = "Name of the provisioned environment for namespacing purposes."
type = string
}
10 changes: 5 additions & 5 deletions terraform/vpc.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
cidr_block = "10.${var.vpc_cidr_index}.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true

Expand All @@ -11,7 +11,7 @@ resource "aws_vpc" "vpc" {
# Public subnets
resource "aws_subnet" "public_1" {
availability_zone = "${var.aws_region}a"
cidr_block = "10.0.1.0/24"
cidr_block = "10.${var.vpc_cidr_index}.1.0/24"
map_public_ip_on_launch = true
vpc_id = aws_vpc.vpc.id

Expand All @@ -22,7 +22,7 @@ resource "aws_subnet" "public_1" {

resource "aws_subnet" "public_2" {
availability_zone = "${var.aws_region}b"
cidr_block = "10.0.2.0/24"
cidr_block = "10.${var.vpc_cidr_index}.2.0/24"
map_public_ip_on_launch = true
vpc_id = aws_vpc.vpc.id

Expand All @@ -34,7 +34,7 @@ resource "aws_subnet" "public_2" {
# Private subnets
resource "aws_subnet" "private_1" {
availability_zone = "${var.aws_region}a"
cidr_block = "10.0.3.0/24"
cidr_block = "10.${var.vpc_cidr_index}.3.0/24"
vpc_id = aws_vpc.vpc.id

tags = {
Expand All @@ -44,7 +44,7 @@ resource "aws_subnet" "private_1" {

resource "aws_subnet" "private_2" {
availability_zone = "${var.aws_region}b"
cidr_block = "10.0.4.0/24"
cidr_block = "10.${var.vpc_cidr_index}.4.0/24"
vpc_id = aws_vpc.vpc.id

tags = {
Expand Down

0 comments on commit 7ceefa1

Please sign in to comment.