Terraform Azure基础设施部署

使用Terraform模块化配置部署Azure基础设施的完整解决方案,包括高可用性、安全性和可扩展性。

  • by udayrealm
  • HCL
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 udayrealm/Azure-Terraform-MCP-Server

使用说明

使用Terraform构建Azure基础设施 - MCP服务器演示

使用Terraform MCP服务器部署Azure基础设施的完整模块化Terraform配置。

🎯 概述

此项目展示了使用Terraform以模块化、可重用的设计方式部署Azure基础设施的最佳实践。它创建了一个完整的Web应用程序基础设施,包括:

  • 高可用性具有多个后端虚拟机的应用程序网关
  • 安全网络安全组、私有子网、NAT网关
  • 可扩展性模块化设计,易于水平扩展
  • 管理用于安全访问和远程状态存储的跳板机

🏗️ 建筑学

┌─────────────────────────────────────────────────────────────────┐
│                          Internet                               │
└────────────┬────────────────┬────────────────┬─────────────────┘
             │                │                │
     ┌───────▼─────┐  ┌──────▼──────┐  ┌─────▼──────┐
     │  App Gateway│  │   Jumpbox   │  │    NAT     │
     │  Public IP  │  │  Public IP  │  │  Gateway   │
     │  (HTTP/S)   │  │    (SSH)    │  │  Public IP │
     └───────┬─────┘  └──────┬──────┘  └─────┬──────┘
             │                │                │
┌────────────┴────────────────┴────────────────┴────────────────┐
│                    Virtual Network (10.10.0.0/16)             │
├───────────────────────────────────────────────────────────────┤
│  ┌──────────────────┐  ┌──────────────┐  ┌─────────────────┐ │
│  │ AppGW Subnet     │  │ Public       │  │ Private Subnet  │ │
│  │ (10.10.3.0/24)   │  │ Subnet       │  │ (10.10.2.0/24)  │ │
│  │                  │  │(10.10.1.0/24)│  │                 │ │
│  │ • App Gateway    │  │              │  │ • Web VM 0      │ │
│  │                  │  │ • Jumpbox VM │  │ • Web VM 1      │ │
│  │                  │  │              │  │ (Nginx)         │ │
│  └──────────────────┘  └──────────────┘  └─────────────────┘ │
│                                                                │
│  Network Security Groups:                                     │
│  • AppGW NSG: HTTP/HTTPS from Internet                       │
│  • Jumpbox NSG: SSH from specified CIDR                      │
│  • Web NSG: HTTP from AppGW, SSH from VNet                   │
└────────────────────────────────────────────────────────────────┘

📋 组件

步骤1:资源组 & 后端

  • 资源组: rg-uday-demo 在美国中部
  • 用于 Terraform 远程状态的存储账户
  • 状态文件存储容器

步骤2:网络基础设施

  • 虚拟网络 (10.10.0.0/16)
  • 3个子网(公共子网、私有子网、应用网关子网)
  • 带有公网IP的NAT网关
  • 3个公共IP(应用网关、跳板机、NAT)
  • 自定义路由的路由表

步骤3:安全

  • 3 网络安全组
  • 细粒度的安全规则
  • 子网关联

步骤4:计算 - Web虚拟机

  • 2台Ubuntu Linux虚拟机(Standard_B1s配置)
  • 私有子网(无公共IP)
  • 使用 Nginx 的 Cloud-init
  • 显示虚拟机名称、区域和私有IP地址

第五步:计算 - 跳板服务器

  • 1台Ubuntu Linux虚拟机(标准B1s型)
  • 带有静态IP的公共子网
  • 通过SSH访问私有资源的接入点

步骤6:应用网关

  • Standard_v2 SKU
  • 端口80上的HTTP监听器
  • 带有Web虚拟机的后端池
  • 健康探测器
  • 基本路由规则

🚀 快速入门

太长,读不下去了登录到Azure → 配置SSH密钥 → 运行 terraform apply

看见 QUICK_START.md 翻译为中文是:快速入门指南.md 这是快速上手的最快方式。

📖 详细文档

📁 项目结构

.
├── README.md                          # This file
├── architecture.md                    # Architecture documentation
├── infra/                             # Terraform infrastructure code
│   ├── main.tf                        # Root module
│   ├── variables.tf                   # Root variables
│   ├── outputs.tf                     # Root outputs
│   ├── backend.tf                     # Backend configuration
│   ├── terraform.tfvars.example       # Example variables
│   ├── .gitignore                     # Git ignore rules
│   ├── README.md                      # Detailed architecture docs
│   ├── QUICK_START.md                 # Quick start guide
│   ├── DEPLOYMENT_GUIDE.md            # Detailed deployment guide
│   └── modules/                       # Reusable modules
│       ├── resource_group/            # Resource group + storage
│       ├── vnet/                      # Virtual network
│       ├── nsg/                       # Network security groups
│       ├── vm/                        # Web VMs
│       ├── jumpbox/                   # Jumpbox VM
│       └── application_gateway/       # Application Gateway
└── docs/                              # Additional documentation

✨ 特点

实施的最佳实践

✅ 模块化设计每种资源类型都在其自身的可重用模块中
✅ 明确依赖关系与……的显式依赖关系 depends_on
✅ 变量定义文档齐全,且默认设置合理
✅ 全面标注所有资源中的一致标签
✅ 有意义的输出轻松验证与集成
✅ 安全第一私有虚拟机、网络安全组(NSG)规则、SSH认证
✅ 高可用性NAT 网关,应用程序网关 v2
✅ 远程状态Azure Blob Storage 后端支持

Terraform 功能

  • 模块可组合、可重用的组件
  • 本地价值观计算值和常用标签
  • 输出值模块间通信
  • 变量参数化配置
  • 远程状态团队协作支持
  • 依赖项适当的资源排序

🔧 前提条件

  • 具有贡献者访问权限的Azure订阅
  • Azure CLI 已安装并配置
  • Terraform >= 1.0(译文:Terraform 1.0或更高版本)
  • SSH密钥对(uday-azure)

📦 部署

最少步骤

# 1. Login to Azure
az login

# 2. Generate SSH key
ssh-keygen -t rsa -b 4096 -f ~/.ssh/uday-azure

# 3. Configure
cd infra
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars: add SSH key, unique storage account name

# 4. Deploy
terraform init
terraform apply

# 5. Access
terraform output application_gateway_public_ip
# Open http://<ip> in browser

详细步骤

见 《部署指南.md》 以获取全面的说明。

🔍 验证

部署后:

# View all outputs
terraform output

# Test the web application
curl http://$(terraform output -raw application_gateway_public_ip)

# SSH to jumpbox
ssh -i ~/.ssh/uday-azure azureuser@$(terraform output -raw jumpbox_public_ip)

# View deployment summary
terraform output deployment_summary

🧪 测试

# Load balancing test
for i in {1..10}; do
  curl http://$(terraform output -raw application_gateway_public_ip)
  echo ""
done

# Should show responses from both web-0 and web-1

🗑️ 清理

cd infra
terraform destroy

警告这将删除所有资源,包括存储帐户!

💰 成本估算

2025年左右美国地区的月度大致费用:

资源 数量 月成本
存储账户 1 1-2美元
虚拟网络 1 免费
NAT 网关 1 $33 + 数据流量费用
公共IP 3 11美元
虚拟机(Standard_B1s) 3台 30美元
应用网关(标准版_v2) 1 125美元
总计 大约200美元

节省成本的小贴士:

  • 在不使用虚拟机时将其关闭
  • 使用较小的虚拟机规模进行测试
  • 为应用网关使用自动扩展

🛠️ 定制化

添加更多Web虚拟机

编辑 terraform.tfvars:

web_vm_count = 3  # or more

更改虚拟机大小

vm_size = "Standard_B2s"  # Larger VM

更改地区

location = "eastus"

然后运行:

terraform plan
terraform apply

🔐 安全考量

用于生产:

  1. 限制SSH访问:

    jumpbox_allowed_cidr = "your-office-ip/32"
    
  2. 使用 Azure Bastion 而不是使用Jumpbox的公共IP

  3. 启用 Azure 防火墙 用于高级防护

  4. 使用密钥保管库 用于密钥管理

  5. 启用诊断日志 对于所有资源

  6. 实施 Azure 策略 用于治理

  7. 使用私有终结点 用于存储账户

📚 学习资源

🤝 贡献

这是一个演示项目。请随意:

  • 分叉并根据您的需求进行定制
  • 提交错误报告
  • 提出改进建议

📄 许可证

此项目仅作为演示和学习之用,按原样提供。

🙏 致谢

使用以下技术构建:

  • HashiCorp 的 Terraform
  • 微软的Azure
  • 用于自动化基础设施生成的Terraform MCP服务器

📞 支持

对于问题:

  1. 检查一下 《部署指南.md》 故障排除部分
  2. 检查 Azure 门户中的资源状态
  3. 检查 Terraform 状态: terraform show
  4. 验证配置: terraform validate

准备好部署了吗? 前往 QUICK_START.md 翻译成中文是:快速启动指南.md 或者 《部署指南》.md!

使用Terraform和Azure,满怀热爱地构建而成