Files
nexus/wiki/concepts/TerraformState.md

31 lines
1.4 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: "Terraform State"
type: concept
tags:
- Terraform
- IaC
- State Management
sources:
- ctp-topic-48-terraform-vs-terragrunt.md
last_updated: 2026-05-13
---
## Definition
Terraform State 是 Terraform 内部维护的 JSON 文件,记录了真实世界基础设施资源与 HCL 代码定义之间的映射关系,是 Terraform 将期望状态与实际运行环境绑定的核心机制。
## Core Mechanism
- **状态锁定State Locking**:防止并发执行导致状态冲突
- **状态后端State Backend**本地存储适用于个人开发企业级使用需配置远程后端S3 + DynamoDB、Terraform Cloud、Consul 等)
- **状态漂移检测Drift Detection**:对比状态文件与实际资源,发现非 Terraform 管理的变更
## State Management Best Practices
- **永远不要手动修改状态文件**——使用 `terraform import` 导入已有资源
- **使用远程后端**:本地状态在团队协作下会导致状态文件冲突和丢失
- **启用状态加密**:敏感信息(密码、密钥)可能存在于状态中
- **版本化存储**:使用支持版本控制的远程后端(如 S3 + DynamoDB
## Connections
- [[Terraform]] ← uses ← [[TerraformState]]
- [[Terragrunt]] ← abstracts_away ← [[TerraformState]](通过模板化管理 remote state 块)
- [[TerraformEnterprise]] ← provides_managed ← [[TerraformState]](远程工作区状态管理)