Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 1051] Fix CIDR overlap #1093

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions infra/modules/dms-networking/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
locals {
our_target_cidr_block = "172.31.0.0/16" # our [Nava] cidr block, where the target database for the DMS is located
their_source_cidr_block = "10.220.0.0/16" # their [MicroHealth] cidr block, where the origin database for the DMS is located
our_target_cidr_block = var.dms_target_cidr_block # our [Nava] cidr block, where the target database for the DMS is located
their_source_cidr_block = var.dms_source_cidr_block # their [MicroHealth] cidr block, where the origin database for the DMS is located
}

# docs: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ssm_parameter
Expand Down
8 changes: 8 additions & 0 deletions infra/modules/dms-networking/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
variable "vpc_id" {
type = string
}

variable "dms_target_cidr_block" {
type = string
}

variable "dms_source_cidr_block" {
type = string
}
8 changes: 4 additions & 4 deletions infra/modules/network/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
data "aws_availability_zones" "available" {}

locals {
vpc_cidr = "10.0.0.0/20"
vpc_cidr = "10.${var.second_octet}.0.0/20"
num_availability_zones = 3
availability_zones = slice(data.aws_availability_zones.available.names, 0, local.num_availability_zones)
}
Expand All @@ -14,9 +14,9 @@ module "aws_vpc" {
azs = local.availability_zones
cidr = local.vpc_cidr

public_subnets = ["10.0.10.0/24", "10.0.11.0/24", "10.0.12.0/24"]
private_subnets = ["10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24"]
database_subnets = ["10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24"]
public_subnets = ["10.${var.second_octet}.10.0/24", "10.${var.second_octet}.11.0/24", "10.${var.second_octet}.12.0/24"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the cidrsubnet function might be another option here if you prefer, instead of string interpolation. https://developer.hashicorp.com/terraform/language/functions/cidrsubnet

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesbursa I looked into that! But unfortunately I didn't really parse the math TBH.

private_subnets = ["10.${var.second_octet}.0.0/24", "10.${var.second_octet}.1.0/24", "10.${var.second_octet}.2.0/24"]
database_subnets = ["10.${var.second_octet}.5.0/24", "10.${var.second_octet}.6.0/24", "10.${var.second_octet}.7.0/24"]
public_subnet_tags = { subnet_type = "public" }
private_subnet_tags = { subnet_type = "private" }
database_subnet_tags = { subnet_type = "database" }
Expand Down
4 changes: 4 additions & 0 deletions infra/modules/network/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
output "vpc_id" {
value = module.aws_vpc.vpc_id
}

output "vpc_cidr" {
value = local.vpc_cidr
}
5 changes: 5 additions & 0 deletions infra/modules/network/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ variable "name" {
description = "Name to give the VPC. Will be added to the VPC under the 'network_name' tag."
}

variable "second_octet" {
type = number
description = "Second octet of the VPC CIDR block. Must be between 0 and 255."
}

variable "aws_services_security_group_name_prefix" {
type = string
description = "Prefix for the name of the security group attached to VPC endpoints"
Expand Down
19 changes: 19 additions & 0 deletions infra/networks/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions infra/networks/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ module "app_config" {
source = "../api/app-config"
}

module "dms_networking" {
source = "../modules/dms-networking"
vpc_id = module.network.vpc_id
}

data "aws_vpc" "default" {
default = true
}
Expand All @@ -58,4 +53,12 @@ module "network" {
name = var.environment_name
database_subnet_group_name = var.environment_name
aws_services_security_group_name_prefix = var.environment_name
second_octet = module.project_config.network_configs[var.environment_name].second_octet
}

module "dms_networking" {
source = "../modules/dms-networking"
vpc_id = module.network.vpc_id
dms_target_cidr_block = module.network.vpc_cidr
dms_source_cidr_block = module.project_config.network_configs[var.environment_name].dms_source_cidr_block
}
18 changes: 15 additions & 3 deletions infra/project-config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,20 @@ locals {

network_configs = {
# TODO(https://github.com/HHS/simpler-grants-gov/issues/1051) deploy to a non-default VPC in every environment
dev = { vpc_name = "default" }
staging = { vpc_name = "staging" }
prod = { vpc_name = "default" }
dev = {
vpc_name = "default"
second_octet = 0 # The second octet our the VPC CIDR block
dms_source_cidr_block = "10.220.0.0/16" # MicroHealth cidr block, where the origin database for the DMS is located
}
staging = {
vpc_name = "staging"
second_octet = 1 # The second octet our the VPC CIDR block
dms_source_cidr_block = "10.220.0.0/16" # MicroHealth cidr block, where the origin database for the DMS is located
}
prod = {
vpc_name = "default"
second_octet = 3 # The second octet our the VPC CIDR block
dms_source_cidr_block = "10.220.0.0/16" # !PLACEHOLDER! We haven't been provided with this yet
}
}
}
Loading