使用 aws cloudformation describe-stack-drift-detection-status 检查堆栈漂移检测任务状态
【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli
本指南讲解 AWS CLI 中aws cloudformation describe-stack-drift-detection-status命令的完整用法:从detect-stack-drift拿到漂移检测 ID 开始,到轮询该命令确认检测是否完成、判断堆栈是否发生漂移,再到用describe-stack-resource-drifts定位具体漂移资源,形成一条完整的堆栈漂移排查链路。读完本文,你将掌握这条命令的参数、返回字段、状态枚举含义,以及如何用 Shell 脚本自动化"发起检测 → 轮询状态 → 查看漂移明细"的整套流程。
命令概述:为什么需要"查看漂移检测状态"?
AWS CloudFormation 的**堆栈漂移检测(stack drift detection)**用于发现堆栈的"实际配置"与"期望配置"(模板 + 模板参数)之间的差异。这种差异通常由带外修改(out-of-band changes)引起,例如有人通过控制台或直接 API 手动改动了某资源的属性。CloudFormation 认为:只要堆栈中有一个资源发生了漂移,整个堆栈就视为DRIFTED。
漂移检测是异步操作:detect-stack-drift提交任务后立即返回一个StackDriftDetectionId,而真正扫描资源需要时间。describe-stack-drift-detection-status正是用来查询这个异步任务执行进度的命令。
在服务模型文件中可以看到,DescribeStackDriftDetectionStatus是一个 POST 请求操作,其接口文档明确指出:使用DetectStackDrift发起检测并拿到StackDriftDetectionId后,用本操作监控进度;检测完成后,再用DescribeStackResourceDrifts返回堆栈及其资源的漂移详情。这三个命令在 AWS CLI 中的对应实现为:
aws cloudformation detect-stack-drift:发起漂移检测,返回检测 IDaws cloudformation describe-stack-drift-detection-status:查询检测任务状态(本文主角)aws cloudformation describe-stack-resource-drifts:查看已检测资源的具体漂移差异
典型使用流程:从发起检测到确认结果
漂移检测的命令行闭环如下:
发起检测并记录返回的检测 ID:
aws cloudformation detect-stack-drift --stack-name my-stack输出:
{ "StackDriftDetectionId": "1a229160-e4d9-xmpl-ab67-0a4f93df83d4" }这个 ID 是后续所有查询的入参。
轮询检测状态,直到
DetectionStatus变为DETECTION_COMPLETE或DETECTION_FAILED:aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id 1a229160-e4d9-xmpl-ab67-0a4f93df83d4输出:
{ "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "StackDriftDetectionId": "1a229160-e4d9-xmpl-ab67-0a4f93df83d4", "StackDriftStatus": "DRIFTED", "DetectionStatus": "DETECTION_COMPLETE", "DriftedStackResourceCount": 1, "Timestamp": "2019-10-02T05:54:30.902Z" }查看漂移明细(若检测到漂移):
aws cloudformation describe-stack-resource-drifts --stack-name my-stack输出会列出发生漂移的资源、期望属性与实际属性的差异(详见后文"完整链路示例")。
参数详解
根据输入模型定义,该命令只有一个必填参数:
| 参数 | 必填 | 类型 | 说明 |
|---|---|---|---|
--stack-drift-detection-id | 是 | String | 漂移检测操作的 ID,来自detect-stack-drift的返回值。CloudFormation 每次执行检测都会生成新的检测 ID 和新的结果。 |
命令行用法:
aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id <detection-id>对应的底层请求构造由 AWS CLI 的命令加载与参数序列化框架处理,具体实现在 awscli/argparser.py、awscli/argprocess.py 等命令解析模块中;服务端模型(操作定义、输入输出 shape、错误类型)则统一在 service-2.json 中声明。
输出字段详解
根据输出模型定义,返回 JSON 包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
StackId | String | 堆栈的唯一 ID(ARN),用于定位堆栈。 |
StackDriftDetectionId | String | 本次漂移检测操作的 ID。 |
StackDriftStatus | String | 堆栈实际配置与期望配置(模板 + 模板参数)的比较状态,取值见下文。 |
DetectionStatus | String | 漂移检测操作自身的执行状态,取值见下文。 |
DetectionStatusReason | String | 检测操作处于当前状态的原因(仅在检测失败等场景下有意义)。 |
DriftedStackResourceCount | Integer | 发生漂移的堆栈资源总数。在检测操作到达DETECTION_COMPLETE之前为null;若堆栈IN_SYNC,该值为 0。 |
Timestamp | Timestamp | 漂移检测操作发起的时间。 |
其中必返回字段为StackId、StackDriftDetectionId、DetectionStatus、Timestamp四项(见输出模型)。
StackDriftStatus 的四种取值
由 StackDriftStatus 枚举定义:
| 取值 | 含义 |
|---|---|
DRIFTED | 堆栈与期望的模板配置存在差异。只要有一个资源漂移,整个堆栈即视为DRIFTED。 |
IN_SYNC | 堆栈实际配置与期望的模板配置一致。 |
NOT_CHECKED | CloudFormation 尚未检查该堆栈是否与期望模板配置存在差异。 |
UNKNOWN | CloudFormation 无法对堆栈中的某个资源执行漂移检测,需结合DetectionStatusReason查看原因。 |
DetectionStatus 的三种取值
由 StackDriftDetectionStatus 枚举定义:
| 取值 | 含义 |
|---|---|
DETECTION_IN_PROGRESS | 漂移检测操作正在进行中。 |
DETECTION_COMPLETE | 对堆栈中所有支持漂移检测的资源,检测均已成功完成(不支持漂移检测的资源保持未检查状态)。若发起检测时指定了逻辑资源 ID 过滤器,则仅检查这些逻辑 ID 对应的资源。 |
DETECTION_FAILED | 堆栈中至少一个资源的漂移检测失败。CloudFormation 成功完成检测的资源仍会返回结果。 |
关于 "StackDriftStatus 为 UNKNOWN" 的补充
在describe-stack-drift-detection-status的上下文中,当StackDriftStatus为UNKNOWN时,说明 CloudFormation 未能对堆栈中的某些资源运行漂移检测,此时应重点查看DetectionStatusReason字段获取具体原因。同一语义在堆栈级的StackDriftInformation/StackDriftInformationSummary结构(service-2.json)中也有体现,describe-stacks等命令返回的堆栈概要信息同样会携带该状态。
完整链路示例:定位一次真实的资源漂移
下面展示三个命令如何协同工作,完整还原官方示例文档 describe-stack-drift-detection-status.rst、detect-stack-drift.rst 与 describe-stack-resource-drifts.rst 中的场景。
假设堆栈my-stack中的某个 AWS Lambda 函数被人在带外改动了MemorySize和Timeout。依次执行:
# 1. 发起漂移检测 aws cloudformation detect-stack-drift --stack-name my-stack # 2. 查询检测状态(将上一步返回的 ID 填入) aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id 1a229160-e4d9-xmpl-ab67-0a4f93df83d4检测完成后的状态输出显示堆栈已漂移:
{ "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "StackDriftDetectionId": "1a229160-e4d9-xmpl-ab67-0a4f93df83d4", "StackDriftStatus": "DRIFTED", "DetectionStatus": "DETECTION_COMPLETE", "DriftedStackResourceCount": 1, "Timestamp": "2019-10-02T05:54:30.902Z" }# 3. 查看漂移资源的具体差异 aws cloudformation describe-stack-resource-drifts --stack-name my-stack输出中可以看到被修改的 Lambda 函数及其属性级差异:
{ "StackResourceDrifts": [ { "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204", "LogicalResourceId": "function", "PhysicalResourceId": "my-function-SEZV4XMPL4S5", "ResourceType": "AWS::Lambda::Function", "StackResourceDriftStatus": "MODIFIED", "PropertyDifferences": [ { "PropertyPath": "/MemorySize", "ExpectedValue": "128", "ActualValue": "256", "DifferenceType": "NOT_EQUAL" }, { "PropertyPath": "/Timeout", "ExpectedValue": "900", "ActualValue": "22", "DifferenceType": "NOT_EQUAL" } ], "Timestamp": "2019-10-02T05:54:44.064Z" } ] }通过PropertyDifferences中的ExpectedValue(期望值)与ActualValue(实际值)对比,可以精确定位是哪条属性、从什么值被改成了什么值——这正是describe-stack-drift-detection-status最终要导向的"证据"。
实战脚本:轮询直到检测完成
由于检测是异步的,实践中通常需要轮询。下面是一个可直接复制的 Bash 示例:
DETECTION_ID=$(aws cloudformation detect-stack-drift \ --stack-name my-stack \ --query StackDriftDetectionId --output text) STATUS="DETECTION_IN_PROGRESS" while [ "$STATUS" = "DETECTION_IN_PROGRESS" ]; do STATUS=$(aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id "$DETECTION_ID" \ --query DetectionStatus --output text) echo "Detection status: $STATUS" sleep 5 done aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id "$DETECTION_ID"要点说明:
- 通过
--query与--output text提取单个字段,便于脚本比较(该能力由 AWS CLI 的输出格式化与 JMESPath 查询机制提供,相关实现见 awscli/formatter.py)。 - 轮询间隔建议设为 5~10 秒;检测通常在几十秒内完成,但大型堆栈耗时更久。
- 循环结束后再次调用该命令,即可拿到包含
StackDriftStatus与DriftedStackResourceCount的完整 JSON。
若需在 CI/CD 中快速判断堆栈是否漂移并直接失败,可结合DriftedStackResourceCount:
COUNT=$(aws cloudformation describe-stack-drift-detection-status \ --stack-drift-detection-id "$DETECTION_ID" \ --query DriftedStackResourceCount --output text) if [ "$COUNT" != "0" ]; then echo "Stack drifted: $COUNT resource(s) out of sync." exit 1 fi注意事项
- 检测 ID 仅对本次检测有效:每次调用
detect-stack-drift都会产生新的StackDriftDetectionId与新的检测结果(见输入模型文档)。CloudFormation 对每个堆栈保留的漂移结果数量与时长可能不同,不要长期依赖旧 ID。 DriftedStackResourceCount在检测完成前为null,轮询脚本中若直接读取该值需注意空值处理。- 并非所有资源类型都支持漂移检测;不支持的资源不会被检查,也不会出现在
describe-stack-resource-drifts的结果中。 DetectionStatus为DETECTION_FAILED时,部分资源仍可能有可用结果,应先读DetectionStatusReason排查失败原因,再对成功完成检测的资源做分析。- 本仓库内
describe-stack-drift-detection-status的官方示例位于 awscli/examples/cloudformation/describe-stack-drift-detection-status.rst,与之配套的detect-stack-drift、describe-stack-resource-drifts示例分别位于 detect-stack-drift.rst 和 describe-stack-resource-drifts.rst;命令的完整 API 模型与字段枚举定义可在 service-2.json 中按DescribeStackDriftDetectionStatus关键字检索确认。
【免费下载链接】aws-cliUniversal Command Line Interface for Amazon Web Services项目地址: https://gitcode.com/GitHub_Trending/aw/aws-cli
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考