AWS NAT Gateway & VPC Endpoints Guard
Eliminates $0.045–$0.059/GB Managed NAT Gateway processing charges on S3, DynamoDB, and ECR container image pulls by binding zero-cost Gateway Endpoints to every private route table and adding bounded VPC Flow Logs (14-day default TTL) to surface residual top talkers.
data "aws_vpc" "selected" {
count = var.enable_ecr_interface_endpoints && var.vpc_cidr_block == null ? 1 : 0
id = var.vpc_id
}
locals {
resolved_vpc_cidr = var.enable_ecr_interface_endpoints ? (
var.vpc_cidr_block != null ? var.vpc_cidr_block : data.aws_vpc.selected[0].cidr_block
) : null
common_tags = merge(
{
ManagedBy = "Terraform"
Module = "vortex-aws-nat-gateway-endpoints-guard"
},
var.tags
)
}
# 1. Zero-Cost ($0/GB) S3 Gateway VPC Endpoint across ALL private route tables
resource "aws_vpc_endpoint" "s3_gateway" {
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.region}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = var.private_route_table_ids
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-s3-gateway-endpoint"
})
}
# 2. Zero-Cost ($0/GB) DynamoDB Gateway VPC Endpoint
resource "aws_vpc_endpoint" "dynamodb_gateway" {
count = var.enable_dynamodb_endpoint ? 1 : 0
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.region}.dynamodb"
vpc_endpoint_type = "Gateway"
route_table_ids = var.private_route_table_ids
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-dynamodb-gateway-endpoint"
})
}
# 3. Optional ECR Interface Endpoints (eliminates NAT Gateway tax on EKS container pulls)
resource "aws_security_group" "ecr_endpoints" {
count = var.enable_ecr_interface_endpoints ? 1 : 0
name = "${var.name_prefix}-ecr-endpoints-sg"
description = "Allow HTTPS from private VPC CIDR to ECR PrivateLink Interface Endpoints"
vpc_id = var.vpc_id
ingress {
description = "HTTPS from VPC CIDR"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = [local.resolved_vpc_cidr]
}
egress {
description = "Allow outbound responses"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-ecr-endpoints-sg"
})
}
resource "aws_vpc_endpoint" "ecr_api" {
count = var.enable_ecr_interface_endpoints ? 1 : 0
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.region}.ecr.api"
vpc_endpoint_type = "Interface"
subnet_ids = var.private_subnet_ids
security_group_ids = [aws_security_group.ecr_endpoints[0].id]
private_dns_enabled = true
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-ecr-api-endpoint"
})
lifecycle {
precondition {
condition = length(var.private_subnet_ids) > 0
error_message = "private_subnet_ids must contain at least one private subnet ID when enable_ecr_interface_endpoints is true."
}
}
}
resource "aws_vpc_endpoint" "ecr_dkr" {
count = var.enable_ecr_interface_endpoints ? 1 : 0
vpc_id = var.vpc_id
service_name = "com.amazonaws.${var.region}.ecr.dkr"
vpc_endpoint_type = "Interface"
subnet_ids = var.private_subnet_ids
security_group_ids = [aws_security_group.ecr_endpoints[0].id]
private_dns_enabled = true
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-ecr-dkr-endpoint"
})
}
# 4. Bounded VPC Flow Logs to diagnose residual NAT Gateway & Cross-AZ data transfer
resource "aws_cloudwatch_log_group" "nat_flow_logs" {
count = var.enable_nat_flow_logs ? 1 : 0
name = "/aws/vpc/flow-logs/${var.name_prefix}"
retention_in_days = var.flow_log_retention_days
tags = local.common_tags
}
resource "aws_iam_role" "flow_logs_role" {
count = var.enable_nat_flow_logs ? 1 : 0
name = "${var.name_prefix}-vpc-flow-logs-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "vpc-flow-logs.amazonaws.com" }
Action = "sts:AssumeRole"
}]
})
tags = local.common_tags
}
resource "aws_iam_role_policy" "flow_logs_policy" {
count = var.enable_nat_flow_logs ? 1 : 0
name = "${var.name_prefix}-vpc-flow-logs-write"
role = aws_iam_role.flow_logs_role[0].id
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Action = [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
]
Resource = "${aws_cloudwatch_log_group.nat_flow_logs[0].arn}:*"
}]
})
}
resource "aws_flow_log" "nat_audit" {
count = var.enable_nat_flow_logs ? 1 : 0
vpc_id = var.vpc_id
traffic_type = "ACCEPT"
log_destination_type = "cloud-watch-logs"
log_destination = aws_cloudwatch_log_group.nat_flow_logs[0].arn
iam_role_arn = aws_iam_role.flow_logs_role[0].arn
max_aggregation_interval = 600
tags = merge(local.common_tags, {
Name = "${var.name_prefix}-nat-audit-flow-log"
})
}
variable "vpc_id" {
type = string
description = "Target AWS VPC ID where private workloads run."
}
variable "region" {
type = string
description = "AWS region (e.g., us-east-1, eu-central-2, eu-west-2)."
}
variable "private_route_table_ids" {
type = list(string)
description = "ALL private subnet route table IDs across every Availability Zone. Omitting even one AZ route table causes silent NAT Gateway bleed."
validation {
condition = length(var.private_route_table_ids) > 0
error_message = "At least one private route table ID must be provided."
}
}
variable "enable_dynamodb_endpoint" {
type = bool
description = "Deploy a zero-cost ($0/GB) DynamoDB Gateway VPC Endpoint."
default = true
}
variable "enable_ecr_interface_endpoints" {
type = bool
description = "Deploy PrivateLink Interface Endpoints for ecr.api and ecr.dkr to keep container image pulls off Managed NAT Gateways."
default = false
}
variable "private_subnet_ids" {
type = list(string)
description = "Private subnet IDs (one per AZ) required when enable_ecr_interface_endpoints is true."
default = []
}
variable "vpc_cidr_block" {
type = string
description = "Optional explicit VPC CIDR block permitted to reach ECR Interface Endpoints on TCP 443. When null, resolved dynamically from data.aws_vpc.selected."
default = null
validation {
condition = var.vpc_cidr_block == null || can(cidrhost(var.vpc_cidr_block, 0))
error_message = "vpc_cidr_block must be a valid IPv4 CIDR block (e.g., 10.20.0.0/16) or null."
}
}
variable "enable_nat_flow_logs" {
type = bool
description = "Enable VPC Flow Logs to CloudWatch with bounded retention to audit residual NAT Gateway and Cross-AZ data transfer."
default = true
}
variable "flow_log_retention_days" {
type = number
description = "CloudWatch Logs retention in days for VPC Flow Logs (bounded to prevent log storage drift)."
default = 14
validation {
condition = contains([1, 3, 5, 7, 14, 30, 60, 90], var.flow_log_retention_days)
error_message = "flow_log_retention_days must be a bounded AWS CloudWatch retention value (1 to 90 days)."
}
}
variable "name_prefix" {
type = string
description = "Prefix applied to security groups, IAM roles, and log groups."
default = "vortex-nat-guard"
}
variable "tags" {
type = map(string)
description = "Additional tags applied to all created resources."
default = {}
}
output "s3_gateway_endpoint_id" {
value = aws_vpc_endpoint.s3_gateway.id
description = "ID of the zero-cost S3 Gateway VPC Endpoint."
}
output "dynamodb_gateway_endpoint_id" {
value = try(aws_vpc_endpoint.dynamodb_gateway[0].id, null)
description = "ID of the zero-cost DynamoDB Gateway VPC Endpoint (if enabled)."
}
output "ecr_interface_endpoint_ids" {
value = var.enable_ecr_interface_endpoints ? {
ecr_api = aws_vpc_endpoint.ecr_api[0].id
ecr_dkr = aws_vpc_endpoint.ecr_dkr[0].id
} : {}
description = "Map of ECR PrivateLink Interface Endpoint IDs (if enabled)."
}
output "flow_log_group_name" {
value = try(aws_cloudwatch_log_group.nat_flow_logs[0].name, null)
description = "CloudWatch Log Group name capturing bounded VPC Flow Logs for NAT/Cross-AZ diagnostics."
}
terraform {
required_version = ">= 1.5.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.0"
}
}
}
module "nat_gateway_guard" {
source = "./aws-nat-gateway-endpoints-guard"
vpc_id = aws_vpc.prod.id
region = "eu-central-2"
private_route_table_ids = aws_route_table.private[*].id
enable_dynamodb_endpoint = true
enable_ecr_interface_endpoints = true
private_subnet_ids = aws_subnet.private[*].id
vpc_cidr_block = aws_vpc.prod.cidr_block
enable_nat_flow_logs = true
flow_log_retention_days = 14
}