Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问许可证需确认审计异常

terraform-state-managementTerraform state management 搜索

Agent Skill

用于辅助云资源、部署、容器、基础设施和运维自动化任务。它适合让 Agent 检查配置、整理部署步骤、分析资源状态、生成排障思路或辅助云服务接入。使用时需要明确目标环境、账号权限、区域和资源组,区分本地测试与生产操作;涉及删除资源、重启服务、修改网络或权限配置时,应先确认影响范围。

总安装

214

周安装

9

GitHub Stars

1

下载量

75
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:terraform-state-management(Terraform state management 搜索)
来源仓库:https://github.com/dawiddutoit/custom-claude
仓库路径:skills/terraform-state-management
安装命令:
npx skills add https://github.com/dawiddutoit/custom-claude --skill terraform-state-management
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/dawiddutoit/custom-claude --skill terraform-state-management

简介

支持 state 文件的版本控制、锁定和多用户协作机制。

  • 适用于团队协作部署、避免并发冲突和审计追踪。
  • 通过 GitHub 安装后,在 Codex、Claude、Cursor、Gemini CLI 中调用命令管理 remote backend 配置。
  • 需确保 backend 存储具备足够的读写权限和高可用性。
  • terraform-state-management 属于研究检索类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Terraform State Management Skill

Table of Contents

Quick StartWhat Is This | When to Use | Simple Example

How to ImplementStep-by-Step | Examples

HelpRequirements | See Also

Purpose

Master Terraform state management including remote backends, state locking, drift detection, migrations, and safe recovery procedures. Critical for team collaboration and production safety.

When to Use

Use this skill when you need to:

  • Set up remote state - Configure GCS backend for team collaboration
  • Migrate between backends - Move state from local to remote or between buckets
  • Handle state locks - Resolve lock timeouts and stale locks
  • Prevent state drift - Detect when infrastructure differs from state
  • Back up state - Create and restore state file backups
  • Understand state operations - View, modify, import, or remove resources from state
  • Recover from state issues - Fix corrupted or out-of-sync state

Critical For:

  • Team projects (multiple developers)
  • Production environments
  • CI/CD pipelines
  • State safety and auditability

Trigger Phrases:

  • "Set up remote state with GCS"
  • "Migrate Terraform state to new backend"
  • "Fix state lock timeout"
  • "Detect state drift"
  • "Import existing resource into state"
  • "Backup and restore Terraform state"

Quick Start

Set up remote state with GCS in 3 steps:

# 1. Create GCS bucket for state
gsutil mb gs://terraform-state-prod
gsutil versioning set on gs://terraform-state-prod

# 2. Configure backend in main.tf
terraform {
  backend "gcs" {
    bucket = "terraform-state-prod"
    prefix = "supplier-charges-hub"
  }
}

# 3. Initialize Terraform
terraform init
# Terraform creates lock file automatically

Instructions

Step 1: Understand Terraform State

What is State?

Terraform's "memory" - a JSON file tracking:

  • What resources exist
  • Their current configuration
  • Metadata and dependencies
  • Sensitive data (passwords, keys)

Why It Matters:

  • State is the source of truth (Terraform compares desired state in.tf files vs current state)
  • State file contains secrets (never commit to Git!)
  • State determines what Terraform will create/update/delete

State Example:

{
  "resources": [
    {
      "type": "google_pubsub_topic",
      "name": "incoming",
      "instances": [
        {
          "attributes": {
            "name": "supplier-charges-hub-incoming",
            "id": "projects/ecp-wtr-supplier-charges-prod/topics/..."
          }
        }
      ]
    }
  ]
}

Step 2: Configure Remote State (Critical!)

Always use remote state for team projects:

# main.tf
terraform {
  backend "gcs" {
    bucket = "terraform-state-prod"  # Must exist
    prefix = "supplier-charges-hub"   # Organizes state
  }
}

Why Remote State?

  • ✅ Team collaboration (single source of truth)
  • ✅ Automatic state locking (prevents concurrent modifies)
  • ✅ Backup and versioning
  • ✅ Secrets not in Git
  • ✅ Auditable (who changed what, when)

Local State (Only for local development):

# Default: stores in terraform.tfstate (NOT for team projects!)
terraform init -backend=false

Step 3: Set Up State Locking

State locking prevents concurrent modifications:

# main.tf - GCS automatically locks on apply/destroy
terraform {
  backend "gcs" {
    bucket = "terraform-state-prod"
    prefix = "supplier-charges-hub"
    # Lock is created automatically in gs://bucket/prefix/default.tflock
  }
}

How Locking Works:

User A runs "terraform apply"
├─ GCS creates lock file (.tflock)
├─ User A makes changes
└─ GCS deletes lock file

User B tries to run "terraform apply" (during User A's operation)
├─ Terraform detects lock file
├─ Waits for timeout (default 10 minutes)
└─ Fails with "Error acquiring state lock"

Lock Configuration:

# Increase lock timeout (default 10m)
terraform apply -lock-timeout=15m

# Disable locking (dangerous - only for testing!)
terraform apply -lock=false

Step 4: Work with State Files

View State:

# List all resources
terraform state list

# Show specific resource
terraform state show google_pubsub_topic.incoming
# Output: Displays current config from state

# Show as JSON
terraform state show -json google_pubsub_topic.incoming | jq

Modify State (use with caution!):

# Rename resource (update references in .tf files too!)
terraform state mv google_pubsub_topic.old google_pubsub_topic.new

# Remove resource from state (don't delete it in GCP!)
terraform state rm google_pubsub_topic.incoming
# Use when Terraform should stop managing a resource

# Import existing resource into state
terraform import google_pubsub_topic.incoming \
  projects/ecp-wtr-supplier-charges-prod/topics/existing-topic

Pull/Push State (rarely needed):

# Download state locally
terraform state pull > backup.tfstate

# Upload state (careful!)
terraform state push backup.tfstate

# Back up state
gsutil cp gs://terraform-state-prod/supplier-charges-hub/default.tfstate ./backup.tfstate

Step 5: Detect and Fix State Drift

State Drift: When actual infrastructure differs from Terraform state.

Causes:

  • Manual changes in GCP console
  • Deletion outside Terraform
  • Failed Terraform run
  • Provider bugs

Detection:

# Refresh state (read actual infrastructure)
terraform refresh
# Updates state to match reality, shows changes

# Plan shows drift
terraform plan
# If plan shows changes you didn't make, you have drift

Resolution:

# Option 1: Accept reality (update state)
terraform refresh
terraform apply
# Applies any missing resource definitions

# Option 2: Revert infrastructure to match state
terraform destroy
terraform apply
# Deletes and recreates everything

# Option 3: Selective fix
terraform import google_pubsub_topic.incoming \
  projects/ecp-wtr-supplier-charges-prod/topics/my-topic
# Imports actual resource back into state

Step 6: Migrate State Between Backends

Scenario: Moving from local state to GCS backend.

# 1. Add backend configuration
# main.tf
terraform {
  backend "gcs" {
    bucket = "terraform-state-prod"
    prefix = "supplier-charges-hub"
  }
}

# 2. Initialize with state migration
terraform init -migrate-state

# Terraform prompts:
# Do you want to copy existing state to the new backend?
# > yes

# 3. Verify migration
terraform state list
# Should show all your resources

# 4. Delete local state (optional)
rm -f terraform.tfstate*

Migrating Between GCS Buckets:

# 1. Update backend config
terraform {
  backend "gcs" {
    bucket = "terraform-state-new-bucket"
    prefix = "supplier-charges-hub"
  }
}

# 2. Migrate
terraform init -migrate-state

# 3. Verify
terraform state list

Step 7: Backup and Restore State

Regular Backups:

# Automated backup script
#!/bin/bash
BACKUP_DIR="./state-backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR
gsutil cp gs://terraform-state-prod/supplier-charges-hub/default.tfstate \
  $BACKUP_DIR/tfstate_$TIMESTAMP.json

# Keep only last 30 days
find $BACKUP_DIR -mtime +30 -delete

Restore from Backup:

# 1. Check available backups
ls -lh state-backups/

# 2. Restore specific backup
gsutil cp ./state-backups/tfstate_20251114_100000.json \
  gs://terraform-state-prod/supplier-charges-hub/default.tfstate

# 3. Verify
terraform state list
terraform plan

Examples

Example 1: Setting Up GCS Backend from Scratch

# 1. Create bucket
gsutil mb gs://terraform-state-production
gsutil versioning set on gs://terraform-state-production

# 2. Enable access logging
gsutil logging set on -b gs://terraform-logs gs://terraform-state-production

# 3. Add backend config to main.tf
cat >> main.tf << 'EOF'
terraform {
  backend "gcs" {
    bucket = "terraform-state-production"
    prefix = "supplier-charges-hub/prod"
  }
}
EOF

# 4. Initialize
rm -rf .terraform/  # Clean local state
terraform init

# Output:
# Initializing the backend...
# bucket: terraform-state-production
# prefix: supplier-charges-hub/prod
# Successfully configured the backend "gcs"!

Example 2: Recovering from Stale Lock

# Error occurs
# Error: Error acquiring the state lock
# Lock Info:
#   ID: 1234567890
#   Created: 2025-11-14 10:00:00 UTC

# 1. Check if operation is actually running
gcloud compute operations list --filter="status:RUNNING"
# No output = no operation running

# 2. Force unlock
terraform force-unlock 1234567890

# 3. Re-plan to verify state
terraform refresh
terraform plan
# Should succeed

Example 3: Detecting and Fixing State Drift

# Someone manually created a Pub/Sub topic in GCP console

# 1. Detect drift
terraform plan
# Output shows: google_pubsub_topic.incoming created outside terraform

# 2. Option A: Import into state
terraform import google_pubsub_topic.incoming \
  projects/ecp-wtr-supplier-charges-prod/topics/manual-topic

terraform state show google_pubsub_topic.incoming
# Now Terraform knows about it

# 3. Option B: Delete from state (if should be managed elsewhere)
terraform state rm google_pubsub_topic.incoming

Example 4: State Migration Timeline

# Project grows, want centralized state management

# Current: Local state
terraform state list
# Stored in ./terraform.tfstate

# Step 1: Create centralized backend
gsutil mb gs://company-terraform-state
gsutil versioning set on gs://company-terraform-state

# Step 2: Configure in main.tf
terraform {
  backend "gcs" {
    bucket = "company-terraform-state"
    prefix = "supplier-charges-hub"
  }
}

# Step 3: Migrate
terraform init -migrate-state
# Terraform: "Do you want to copy existing state to the new backend?"
# Yes

# Step 4: Verify
terraform state list
# All resources present

# Step 5: Remove local state (now safe)
rm -f .gitignore  # Remove terraform.tfstate* from ignore
rm -f terraform.tfstate terraform.tfstate.backup
git add .gitignore
git commit -m "migrate: move state to centralized GCS backend"

Requirements

  • Terraform 1.x+
  • GCP credentials (gcloud auth or service account)
  • GCS bucket (for remote state)
  • gsutil CLI tool installed
  • Git (for version control)

See Also

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

37.44%
按下载量换算28

Claude

30.18%
按下载量换算23

Cursor

18.46%
按下载量换算14

Gemini CLI

9.26%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

未通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。来源安全扫描存在 warning/failed 结果,不能写成本站确认安全。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills