关于 tfc-run-logs
tfc-run-logs 是用于按已知运行 ID 获取 Terraform Cloud 计划与应用日志的技能,帮助基础设施团队查看易读输出,并在排障或协作时留存日志。
主要功能
- 01获取计划与应用日志
- 02单独提取计划或应用输出
- 03清除 ANSI 颜色代码
- 04支持自定义 Terraform Cloud 地址
适用场景
- 01排查失败的计划或应用
- 02审阅基础设施运行输出
- 03在问题单或团队协作中分享日志
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ laurigates/ claude-plugins --skill tfc-run-logs安装后在 Claude Code 中输入/tfc-run-logs确认已加载。
详细介绍
Terraform Cloud 运行日志
直接在终端中检索并显示 Terraform Cloud 运行的计划和/或应用日志。
何时使用此技能
| 在以下情况下使用此技能…… | 在以下情况下改用同类技能…… |
|---|---|
| 读取已知 TFC 运行 ID 的计划或应用输出文本 | 将资源变更作为结构化 JSON 进行分析(tfc-plan-json) |
| 通过检查日志行来排查计划或应用失败 | 只检查通过/失败状态,不查看日志内容(tfc-run-status) |
| 以便于阅读的形式审查基础设施变更 | 在历史记录中查找正确的运行 ID(tfc-list-runs) |
| 捕获日志以便分享给团队成员或粘贴到问题中 | 通过简称浏览 FVH 工作区的最新运行(tfc-workspace-runs) |
先决条件
# Required environment variables
export TFE_TOKEN="your-api-token" # User or team token (not organization token)
export TFE_ADDRESS="app.terraform.io" # Optional, defaults to app.terraform.io
核心工作流
获取计划和应用日志
#!/bin/bash
set -euo pipefail
TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
RUN_ID="${1:?Usage: $0 <run-id>}"
# Get run with plan and apply relationships
RUN_DATA=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/runs/$RUN_ID?include=plan,apply")
# Extract IDs
PLAN_ID=$(echo "$RUN_DATA" | jq -r '.data.relationships.plan.data.id')
APPLY_ID=$(echo "$RUN_DATA" | jq -r '.data.relationships.apply.data.id // empty')
# Get and display plan logs
PLAN_LOG_URL=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/plans/$PLAN_ID" | jq -r '.data.attributes."log-read-url"')
echo "=== PLAN OUTPUT ==="
curl -sf "$PLAN_LOG_URL" | sed 's/\x1b\[[0-9;]*m//g' # Strip ANSI codes
# Get apply logs if exists
if [ -n "$APPLY_ID" ]; then
APPLY_LOG_URL=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/applies/$APPLY_ID" | jq -r '.data.attributes."log-read-url"')
echo ""
echo "=== APPLY OUTPUT ==="
curl -sf "$APPLY_LOG_URL" | sed 's/\x1b\[[0-9;]*m//g'
fi
仅获取计划日志
TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
RUN_ID="run-abc123"
# Get plan ID from run
PLAN_ID=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/runs/$RUN_ID" | jq -r '.data.relationships.plan.data.id')
# Get log URL and fetch logs
PLAN_LOG_URL=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/plans/$PLAN_ID" | jq -r '.data.attributes."log-read-url"')
curl -sf "$PLAN_LOG_URL"
仅获取应用日志
TOKEN="${TFE_TOKEN:?TFE_TOKEN not set}"
BASE_URL="https://${TFE_ADDRESS:-app.terraform.io}/api/v2"
RUN_ID="run-abc123"
# Get apply ID from run
APPLY_ID=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/runs/$RUN_ID" | jq -r '.data.relationships.apply.data.id')
if [ -n "$APPLY_ID" ] && [ "$APPLY_ID" != "null" ]; then
# Get log URL and fetch logs
APPLY_LOG_URL=$(curl -sf --header "Authorization: Bearer $TOKEN" \
"$BASE_URL/applies/$APPLY_ID" | jq -r '.data.attributes."log-read-url"')
curl -sf "$APPLY_LOG_URL"
else
echo "No apply for this run"
fi
快速单行命令
计划日志(带 ANSI 颜色)
curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
"https://app.terraform.io/api/v2/runs/run-abc123?include=plan" | \
jq -r '.included[0].attributes."log-read-url"' | xargs curl -sf
计划日志(纯文本)
curl -sf -H "Authorization: Bearer $TFE_TOKEN" \
"https://app.terraform.io/api/v2/runs/run-abc123?include=plan" | \
jq -r '.included[0].attributes."log-read-url"' | \
xargs curl -sf | sed 's/\x1b\[[0-9;]*m//g'
重要说明
- 日志 URL 属于机密信息:Archivist URL 中包含嵌入式身份验证信息,请勿记录
- URL 会过期:日志 URL 的有效期为 25 小时
- 获取日志无需身份验证:获取 Archivist URL 后,无需 bearer token
- ANSI 代码:日志包含颜色代码;使用
sed清除这些代码,以获得纯净输出 - 速率限制:
/runs端点限制为每分钟 30 次请求
常见错误
404 Not Found
- 运行 ID 不存在,或者你没有访问权限
- TFC 对这两种情况都返回 404(安全措施)
401 Unauthorized
- Token 无效或已过期
- 组织 Token 无法访问运行数据,请使用用户/团队 Token
没有应用日志
- 运行可能仅包含计划、尚未应用或已丢弃
- 请先检查运行状态
另请参阅
tfc-run-status:快速检查运行状态tfc-list-runs:列出工作区中的近期运行tfc-plan-json:获取结构化计划 JSON 输出
评论
支持 Markdown