islamelkadi/terraform-aws-vpc
GitHub: islamelkadi/terraform-aws-vpc
一个生产就绪的 AWS VPC Terraform 模块,内置多框架合规支持和环境自适应安全控制。
Stars: 0 | Forks: 0
# Terraform AWS VPC 模块
[](https://github.com/islamelkadi/terraform-aws-vpc/actions/workflows/terraform-security.yaml)
[](https://github.com/islamelkadi/terraform-aws-vpc/actions/workflows/terraform-lint.yaml)
[](https://github.com/islamelkadi/terraform-aws-vpc/actions/workflows/terraform-docs.yaml)
一个全面的 Terraform 模块,用于 AWS 网络基础设施,包括 VPC、VPC Endpoints、Network ACLs 和 Security Groups。
## 目录
- [前置条件](#prerequisites)
- [安全性](#security)
- [功能](#features)
- [用法](#usage)
- [要求](#requirements)
- [MCP Servers](#mcp-servers)
## 前置条件
本模块专为 macOS 设计。您的机器上必须已安装以下软件:
- Python 3 和 pip
- [Kiro](https://kiro.dev) 和 Kiro CLI
- [Homebrew](https://brew.sh)
要安装其余的开发工具,请运行:
```
make bootstrap
```
这将安装/升级:tfenv、Terraform (通过 tfenv)、tflint、terraform-docs、checkov 和 pre-commit。
## 安全性
### 安全控制
本模块实施了符合以下标准的安全控制:
- AWS Foundational Security Best Practices (FSBP)
- CIS AWS Foundations Benchmark
- NIST 800-53 Rev 5
- NIST 800-171 Rev 2
- PCI DSS v4.0
### 已实施的控制
- [x] **Flow Logs**: 通过 CloudWatch 启用,用于网络流量监控
- [x] **Private Subnets**: 用于计算资源隔离
- [x] **NAT Gateway**: 高可用性选项,用于私有子网互联网访问
- [x] **Security Groups**: 具有最小权限规则的有状态防火墙
- [x] **Network ACLs**: 可选的无状态防火墙,用于纵深防御
- [x] **Security Control Overrides**: 可扩展的覆盖系统,带有审计理由
- [ ] **VPC Endpoints**: 用于 AWS 服务(可选,成本与安全的权衡)
有关完整的安全标准和实施细节,请参阅 [AWS Security Standards](../../../.kiro/steering/aws/aws-security-standards.md)。
### 基于环境的安全控制
安全控制通过 [terraform-aws-metadata](https://github.com/islamelkadi/terraform-aws-metadata?tab=readme-ov-file#security-profiles) 模块的安全配置文件根据环境自动应用:
| Control | Dev | Staging | Prod |
|---------|-----|---------|------|
| VPC Flow Logs | Optional | Required | Required |
| Private subnets | Recommended | Required | Required |
| NAT Gateway HA | Single AZ | Multi-AZ | Multi-AZ |
| Security groups | Enforced | Enforced | Enforced |
| Network ACLs | Optional | Recommended | Required |
| VPC Endpoints | Optional | Recommended | Required |
有关安全配置文件以及控制如何随环境变化的完整详细信息,请参阅 [Security Profiles](https://github.com/islamelkadi/terraform-aws-metadata?tab=readme-ov-file#security-profiles) 文档。
### 安全扫描抑制
本模块抑制了某些 Checkov 安全检查,这些检查要么不适用于示例/演示代码,要么代表了有意的设计决策。以下检查在 `.checkov.yaml` 中被抑制:
**模块源版本控制 (CKV_TF_1, CKV_TF_2)**
- 被抑制是因为我们使用语义版本标签 (`?ref=v1.0.0`) 而不是提交哈希,以获得更好的可维护性和可读性
- 语义版本控制是稳定版本的有效且广泛接受的版本控制策略
**VPC 安全控制**
- **Security Group Egress (CKV_AWS_382)**: VPC endpoint 安全组需要广泛的出站规则以进行 AWS 服务访问;用户应根据使用的特定服务进行限制
- **NACL All Ports (CKV_AWS_352)**: NACL 默认拒绝规则有意阻止所有端口;这是安全最佳实践
- **CloudWatch Log Group KMS Encryption (CKV_AWS_158)**: VPC Flow Logs 的 KMS 加密会增加成本;请根据安全要求启用
- **Security Group Attachment (CKV2_AWS_5)**: 示例代码中的安全组可能不会立即附加;用户会将它们附加到自己的资源上
- **Default Security Group (CKV2_AWS_12)**: 默认安全组限制应由用户在创建 VPC 后根据其要求进行管理
- **NACL Attachment (CKV2_AWS_1)**: NACL 到子网的附加是单独演示的;用户应根据其网络分段要求进行附加
## 子模块
本模块包含五个用于完整 VPC 网络的子模块:
### VPC (`modules/vpc`)
创建包含子网、NAT gateways、internet gateways 和 flow logs 的 AWS VPC。
**功能:**
- 跨多个可用区的公有和私有子网
- 用于私有子网互联网访问的 NAT Gateway
- 用于公有子网访问的 Internet Gateway
- VPC Flow Logs 到 CloudWatch
- 路由表和关联
- 默认安全(私有子网、启用 flow logs)
**用法:**
```
module "vpc" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/vpc?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_cidr = "10.0.0.0/16"
availability_zones = ["ca-central-1a", "ca-central-1b"]
public_subnet_cidrs = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnet_cidrs = ["10.0.10.0/24", "10.0.11.0/24"]
enable_nat_gateway = true
enable_flow_logs = true
kms_key_arn = aws_kms_key.main.arn
}
```
### NAT Gateway (`modules/nat-gateway`)
创建带有 Internet Gateway 的 NAT Gateway,用于私有子网互联网访问。
**功能:**
- 公有子网中的 NAT Gateway
- 自动分配 Elastic IP
- Internet Gateway 创建和管理
- 高可用性支持(跨 AZ 的多个 NAT Gateway)
- 安全控制集成
- 成本优化选项(单 AZ vs 多 AZ)
**用法:**
```
module "nat_gateway" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/nat-gateway?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
# Multiple subnets across AZs for HA
public_subnet_ids = [
module.vpc.public_subnet_ids[0], # ca-central-1a
module.vpc.public_subnet_ids[1], # ca-central-1b
]
tags = {
Project = "CorporateActions"
}
}
```
### VPC Endpoints (`modules/endpoints`)
为 AWS 服务创建 VPC endpoints 以将流量保留在 VPC 内。
**功能:**
- Interface endpoints (PrivateLink)
- Gateway endpoints (S3, DynamoDB)
- 安全组管理
- 启用 Private DNS
- 在可用的情况下使用具成本效益的 gateway endpoints
**用法:**
```
module "vpc_endpoints" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/endpoints?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
security_group_ids = [module.security_group.id]
# Gateway endpoints (free)
enable_s3_endpoint = true
enable_dynamodb_endpoint = true
# Interface endpoints (charged)
enable_lambda_endpoint = true
enable_secretsmanager_endpoint = true
enable_bedrock_endpoint = true
}
```
### Network ACLs (`modules/nacl`)
创建用于子网级安全(纵深防御)的 Network ACLs。
**功能:**
- 子网层的无状态防火墙规则
- 入站和出站规则
- 规则编号管理
- 支持 ICMP、TCP、UDP 协议
- 补充安全组以进行纵深防御
**用法:**
```
module "nacl" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/nacl?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "private"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
ingress_rules = [
{
rule_number = 100
protocol = "tcp"
rule_action = "allow"
cidr_block = "10.0.0.0/16"
from_port = 443
to_port = 443
}
]
egress_rules = [
{
rule_number = 100
protocol = "-1"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
]
}
```
### Security Groups (`modules/security-group`)
创建用于实例级安全(有状态防火墙)的 Security Groups。
**功能:**
- 实例级的有状态防火墙规则
- 入站和出站规则
- 支持 CIDR 块和源安全组
- 特定协议规则(TCP、UDP、ICMP、所有)
- EC2、Lambda、RDS 等的主要安全控制
**用法:**
```
module "security_group" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/security-group?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "lambda"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
description = "Security group for Lambda functions"
ingress_rules = [
{
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
description = "HTTPS from VPC"
}
]
egress_rules = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound"
}
]
}
```
## 完整示例
以下是如何同时使用所有五个子模块:
```
# 创建包含 subnets 的 VPC
module "vpc" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/vpc?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_cidr = "10.0.0.0/16"
availability_zones = ["ca-central-1a", "ca-central-1b"]
public_subnet_cidrs = ["10.0.1.0/24", "10.0.2.0/24"]
private_subnet_cidrs = ["10.0.10.0/24", "10.0.11.0/24"]
enable_nat_gateway = true
enable_flow_logs = true
kms_key_arn = aws_kms_key.main.arn
}
# 创建 NAT Gateway (可选 - 如果不使用 VPC 模块内置的 NAT)
module "nat_gateway" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/nat-gateway?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
# Multiple subnets across AZs for HA
public_subnet_ids = module.vpc.public_subnet_ids
tags = {
Project = "CorporateActions"
}
}
# 创建 Security Groups
module "lambda_sg" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/security-group?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "lambda"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
description = "Security group for Lambda functions"
egress_rules = [
{
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "HTTPS to internet"
}
]
}
# 创建 VPC Endpoints
module "vpc_endpoints" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/endpoints?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "main"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
security_group_ids = [module.lambda_sg.id]
enable_s3_endpoint = true
enable_dynamodb_endpoint = true
enable_lambda_endpoint = true
}
# 创建 Network ACLs
module "private_nacl" {
source = "github.com/islamelkadi/terraform-aws-vpc//modules/nacl?ref=v1.0.0"
namespace = "example"
environment = "prod"
name = "private"
region = "us-east-1"
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnet_ids
ingress_rules = [
{
rule_number = 100
protocol = "tcp"
rule_action = "allow"
cidr_block = "10.0.0.0/16"
from_port = 443
to_port = 443
}
]
egress_rules = [
{
rule_number = 100
protocol = "-1"
rule_action = "allow"
cidr_block = "0.0.0.0/0"
from_port = 0
to_port = 0
}
]
}
```
## 模块结构
```
terraform-aws-vpc/
├── modules/
│ ├── vpc/ # VPC with subnets, NAT, IGW
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ ├── data.tf
│ │ ├── metadata.tf
│ │ ├── provider.tf
│ │ ├── security-validations.tf
│ │ ├── example/
│ │ └── README.md
│ ├── nat-gateway/ # NAT Gateway with IGW
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ ├── locals.tf
│ │ ├── security-validations.tf
│ │ ├── versions.tf
│ │ ├── example/
│ │ └── README.md
│ ├── endpoints/ # VPC Endpoints
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ ├── locals.tf
│ │ ├── metadata.tf
│ │ ├── provider.tf
│ │ ├── example/
│ │ └── README.md
│ ├── nacl/ # Network ACLs
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ │ ├── locals.tf
│ │ ├── metadata.tf
│ │ ├── provider.tf
│ │ ├── example/
│ │ └── README.md
│ └── security-group/ # Security Groups
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
│ ├── locals.tf
│ ├── metadata.tf
│ ├── provider.tf
│ ├── example/
│ └── README.md
└── README.md
```
## 为什么有五个子模块?
**关注点分离:**
- VPC 是基础(始终需要)
- NAT Gateway 提供私有子网互联网访问(可选,可由 VPC 模块管理)
- Security Groups 是主要防火墙(几乎总是需要)
- Endpoints 是可选的(成本与安全的权衡)
- NACLs 是可选的(用于合规的额外安全层)
**灵活性:**
- 仅使用 VPC 进行简单设置
- 将 NAT Gateway 作为独立模块添加,以更好地控制 HA 和成本
- 添加 security groups 用于实例级安全
- 添加 endpoints 用于私有 AWS 服务访问
- 添加 NACLs 以满足合规要求(纵深防御)
**独立版本控制:**
- 每个子模块可以独立演进
- 重大变更仅限于特定子模块
## 安全功能
所有子模块都实施了安全最佳实践:
- **VPC**: 默认私有子网、启用 flow logs、KMS 加密
- **Security Groups**: 有状态防火墙、最小权限规则
- **Endpoints**: 启用 Private DNS、安全组限制
- **NACLs**: 无状态防火墙规则、显式允许/拒绝
## Security Groups 与 NACLs
**Security Groups (有状态):**
- 实例级安全
- 有状态(返回流量自动允许)
- 仅允许规则(隐式拒绝)
- 在决定之前评估所有规则
- 主要安全控制
**Network ACLs (无状态):**
- 子网级安全
- 无状态(必须显式允许返回流量)
- 允许和拒绝规则
- 按规则编号顺序处理规则
- 纵深防御 / 合规要求
**最佳实践:** 使用 Security Groups 作为主要控制,在需要时添加 NACLs 作为额外层。
## 示例
请参阅每个子模块的目录以获取具体示例:
- [VPC Example](modules/vpc/example/)
- [NAT Gateway Example](modules/nat-gateway/example/)
- [Security Group Example](modules/security-group/example/)
- [Endpoints Example](modules/endpoints/example/)
- [NACL Example](modules/nacl/example/)
## MCP Servers
本模块包含两个在 `.kiro/settings/mcp.json` 中配置的 [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) 服务器,用于 Kiro:
| Server | Package | Description |
|--------|---------|-------------|
| `aws-docs` | `awslabs.aws-documentation-mcp-server@latest` | 提供对 AWS 文档的访问,用于服务功能、API 参考和最佳实践的上下文查找。 |
| `terraform` | `awslabs.terraform-mcp-server@latest` | 直接从 IDE 启用 Terraform 操作(init、validate、plan、fmt、tflint),并为常见工作流提供自动批准的命令。 |
这两个服务器都通过 `uvx` 运行,除了 [bootstrap](#prerequisites) 步骤外,不需要额外安装。
## Requirements
No requirements.
## Providers
No providers.
## Modules
No modules.
## Resources
No resources.
## Inputs
No inputs.
## Outputs
No outputs.
标签:AWS, CIS, CloudWatch, DPI, EC2, ECS, IaC, NAT网关, NIST, PCI-DSS, Terraform, VPC, 亚马逊云, 人工智能安全, 企业级, 合规性, 子网, 安全组, 模块, 流日志, 流量监控, 特权提升, 生产就绪, 网络ACL, 网络安全, 自动化部署, 逆向工具, 防御深度, 隐私保护