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

Feature/add dst cidr #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 18 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ locals {
cwagent_param_arn = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? var.cloudwatch_agent_configuration_param_arn : aws_ssm_parameter.cloudwatch_agent_config[0].arn : null
cwagent_param_name = var.use_cloudwatch_agent ? var.cloudwatch_agent_configuration_param_arn != null ? split("/", data.aws_arn.ssm_param[0].resource)[1] : aws_ssm_parameter.cloudwatch_agent_config[0].name : null
security_groups = concat(var.use_default_security_group ? [aws_security_group.main.id] : [], var.additional_security_group_ids)
route_table_ids = var.update_route_table ? var.route_tables_ids : {}

route_entries = flatten([
for rt_id in local.route_table_ids : [
for cidr in var.destination_cidr_blocks : {
route_table_id = rt_id
destination_cidr_block = cidr
}
]
])

}

data "aws_region" "current" {}
Expand Down Expand Up @@ -62,10 +73,12 @@ resource "aws_network_interface" "main" {
}

resource "aws_route" "main" {
for_each = var.update_route_tables || var.update_route_table ? merge(var.route_tables_ids, var.route_table_id != null ? { RESERVED_FKC_NAT = var.route_table_id } : {}) : {}

route_table_id = each.value
destination_cidr_block = "0.0.0.0/0"
for_each = {
for idx, route in local.route_entries :
"${route.route_table_id}_${replace(route.destination_cidr_block, "/", "-")}" => route
}
route_table_id = each.value.route_table_id
destination_cidr_block = each.value.destination_cidr_block
network_interface_id = aws_network_interface.main.id
}

Expand All @@ -80,4 +93,4 @@ resource "aws_ssm_parameter" "cloudwatch_agent_config" {
METRICS_NAMESPACE = var.cloudwatch_agent_configuration.namespace
METRICS_ENDPOINT_OVERRIDE = var.cloudwatch_agent_configuration.endpoint_override
})
}
}
11 changes: 10 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,17 @@ variable "ssh_cidr_blocks" {
}
}

variable "destination_cidr_blocks" {
type = list(string)
default = ["0.0.0.0/0"]
validation {
condition = length([for cidr in var.destination_cidr_blocks : cidr if can(cidrsubnet(cidr, 0, 0))]) == length(var.destination_cidr_blocks)
error_message = "Each item must be a valid CIDR block in CIDR notation (e.g., '192.168.0.0/16')."
}
}

variable "tags" {
description = "Tags to apply to resources created within the module"
type = map(string)
default = {}
}
}