Files
nexus/wiki/concepts/Infrastructure as Code.md
2026-05-03 05:42:12 +08:00

58 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Infrastructure as Code"
type: concept
tags: [DevOps, Cloud, Automation]
sources: [engineering-devops-automator]
last_updated: 2026-05-01
---
# Infrastructure as Code
## 定义
基础设施即代码Infrastructure as CodeIaC是一种通过代码定义和管理云基础设施的方法替代传统手动配置实现基础设施的可重复性、可版本控制和可测试性。
## 核心原则
1. **声明式配置**:描述期望的最终状态,而非执行的具体步骤
2. **幂等性**:多次执行产生相同结果
3. **版本控制**:所有配置存储在 Git 等版本控制系统
4. **可重复性**:同一代码在不同环境生成相同基础设施
5. **自动化测试**:基础设施变更经过测试验证
## 核心工具
- **Terraform**声明式、多云支持AWS/GCP/Azure
- **AWS CloudFormation**AWS 原生声明式服务
- **AWS CDK**使用编程语言TypeScript/Python定义云资源
- **Ansible**:配置管理 + 基础设施编排
- **Pulumi**:使用通用编程语言定义基础设施
## 在 DevOps Automator 中的应用
- 使用 Terraform 定义 AWS Auto-scaling groups、ALB、CloudWatch alarms
- 通过 `terraform plan` 预览变更,通过 `terraform apply` 自动部署
- 支持多环境管理dev/staging/prod
## 相关概念
- [[CI/CD Pipeline]]IaC 变更通过 CI/CD 流水线自动化部署
- [[Terraform]]DevOps Automator 默认使用的 IaC 工具
- [[GitOps]]:基于 Git 的 IaC 运维模式
## 最佳实践
- 状态文件存储在远程后端S3 + DynamoDB
- 使用 workspaces 或目录分离多环境
- 模块化设计,复用公共组件
- 敏感信息使用 Vault 或 AWS Secrets Manager
## 与传统运维的对比
| 维度 | 传统运维 | IaC |
|------|----------|-----|
| 速度 | 手动配置耗时 | 分钟级自动部署 |
| 一致性 | 人为差异 | 完全一致 |
| 版本控制 | 无 | 完整历史记录 |
| 回滚 | 困难 | 一键回滚 |
| 审计 | 日志分散 | 集中可追溯 |
## Aliases
- IaC
- Infrastructure as Code
- 基础设施即代码
- Configuration as Code