Sync: add infrastructure as code notes

This commit is contained in:
2026-04-24 19:58:02 +08:00
parent cc23df1883
commit e4f6f463cb
29 changed files with 2344 additions and 155 deletions

View File

@@ -1,37 +1,49 @@
---
title: "Infrastructure as Code"
type: concept
tags: [DevOps, 自动化, 配置管理]
sources: [ctp-topic-70-eks-deployment-using-iac, learning-sessions-ecs-deployment-using-iac-20230808-183322-meeting-recording, ctp-topic-16-cross-account-terraform-modules, ctp-topic-12-using-ses-smtp-service-terraform-module, learning-sessions-cloud-transformation-programme-deploying-rds-via-terraform]
last_updated: 2026-04-24
tags: [DevOps, AWS, Terraform, Automation]
sources: [ctp-topic-3-deploy-and-maintain-infrastructure, ctp-topic-9-ci-cd-with-gruntwork, ctp-topic-48-terraform-vs-terragrunt, ctp-topic-32-using-atlantis-cicd-for-infrastructure-deployments, ctp-topic-33-an-introduction-to-gitops, ctp-topic-56-automated-infrastructure-testing, learning-sessions-ecs-deployment-using-iac-20230808-183322-meeting-recording]
last_updated: 2026-05-05
---
# Infrastructure as Code
## Overview
Infrastructure as Code (IaC) 是一种通过代码定义和管理基础设施的方法,实现基础设施的标准化、可审计和可重复部署
## Definition
基础设施即代码(Infrastructure as Code, IaC是一种通过机器可读的定义文件(而非物理硬件配置或交互式配置工具)管理和配置计算基础设施的方法
## Core Principles
- **声明式配置**定义期望的状态,而非执行的具体步骤
- **版本控制**:所有基础设施配置纳入 Git 版本控制
- **自动化部署**:通过 CI/CD 流水线自动化执行部署
- **幂等性**重复执行相同配置不产生副作用
- **声明式配置**描述期望的最终状态,而非执行的具体步骤
- **版本控制**:所有基础设施定义文件存储在 Git
- **幂等性**:多次执行产生相同结果
- **可重复性**同一模板可在不同环境快速部署
- **自动化**:与 CI/CD 流水线集成
## Key Tools
- **Terraform**HashiCorp 的基础设施编排工具,支持多云
- **AWS CloudFormation**AWS 原生的 IaC 服务
- **AWS Service Catalog**AWS 的服务目录,封装标准化产品组合
- **Pulumi**使用编程语言Python, TypeScript 等)定义基础设施
- **Terraform**HashiCorp 出品,云厂商无关的 IaC 工具,通过状态文件管理资源
- **Terragrunt**Terraform 轻量封装,贯彻 DRY 原则
- **AWS CloudFormation**AWS 原生 IaC 服务JSON/YAML 模板
- **Pulumi**:编程语言驱动的 IaC 平台
- **Ansible**:配置管理和应用部署工具
## Key Concepts
- **HCL (HashiCorp Configuration Language)**Terraform 的配置语言
- **State Management**Terraform 使用 state 文件追踪资源
- **Modules**可重用的基础设施组件
- **Remote State**:远程状态存储,支持团队协作
## Terraform Ecosystem
- **Gruntwork**:预建 Terraform 模块库,生产级参考架构
- **Atlantis**Git 集成 Terraform 部署PR 评论式协作
- **Terratest**Terraform 代码的 Go 测试框架
- **tfsec**Terraform 静态安全分析工具
- **TFLint**Terraform 代码规范检查
## IaC in CTP Context
CTPCloud Transformation Programme使用 Terraform/Terragrunt 构建 AWS Landing Zone
- [[ctp-topic-3-deploy-and-maintain-infrastructure]]Terragrunt HCL 文件与模块化管理
- [[ctp-topic-9-ci-cd-with-gruntwork]]Gruntwork CI/CD 流水线实践
- [[ctp-topic-48-terraform-vs-terragrunt]]Terraform 与 Terragrunt 对比选型
- [[ctp-topic-32-using-atlantis-cicd-for-infrastructure-deployments]]Atlantis 替代 Jenkins
- [[ctp-topic-56-automated-infrastructure-testing]]TerraTest 自动化测试
- [[learning-sessions-ecs-deployment-using-iac-20230808-183322-meeting-recording]]ECS IaC 部署实践
## Related Concepts
- [[Terraform]]:最流行的 IaC 工具之一
- [[AWS Service Catalog]]AWS IaC 产品目录
- [[GitOps]]:基于 Git 的运维方法论
- [[CI/CD Pipeline]]:自动化部署流水线
- [[DevOps Culture]]IaC 是 DevOps 实践的核心组成
- [[GitOps]]Git 作为 IaC 的单一真相来源
- [[CI/CD Pipeline]]IaC 与 CI/CD 流水线的集成
- [[Policy-as-Code]]IaC 扩展至安全合规策略
- [[Canary-Deployment]]:基于 IaC 的金丝雀部署策略
- [[Atlantis]]GitOps 驱动的 Terraform 协作工具