

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 状态文件丢失后，安全地清 AWS 理 Terraform (AFT) 资源的 Account Factory
<a name="clean-up-aft-resources-safely-after-state-file-loss"></a>

*Gokendra Malviya，Amazon Web Services*

## Summary
<a name="clean-up-aft-resources-safely-after-state-file-loss-summary"></a>

当你使用 Acco AWS unt Factory for Terraform (AFT) 管理 AWS Control Tower 环境时，AFT 会生成一个 Terraform 状态文件来跟踪 Terraform 创建的资源的状态和配置。丢失 Terraform 状态文件可能会给资源管理和清理带来重大挑战。这种模式提供了一种系统的方法，可以在保持环境完整性的同时，安全地识别和移除 AFT 相关资源。 AWS Control Tower 

此过程旨在确保正确移除所有 AFT 组件，即使没有原始状态文件参考亦是如此。此过程为在您的环境中成功重新建立和重新配置 AFT 提供了明确的途径，有助于确保最大限度地减少对运营的干扰。 AWS Control Tower 

有关 AFT 的更多信息，请参阅 [AWS Control Tower 文档](https://docs.aws.amazon.com/controltower/latest/userguide/taf-account-provisioning.html)。

## 先决条件和限制
<a name="clean-up-aft-resources-safely-after-state-file-loss-prereqs"></a>

**先决条件**
+ 深入了解 [AFT 架构](https://docs.aws.amazon.com/controltower/latest/userguide/aft-architecture.html)。
+ 对以下账户的管理员访问权限：
  + AFT 管理账户
  + AWS Control Tower 管理账户
  + 日志存档账户
  + 审计账户
+ 验证没有任何服务控制策略 (SCPs) 包含会阻止删除 AFT 相关资源的限制或限制。

**限制**
+ 此过程可以有效地清理资源，但无法恢复丢失的状态文件，并且某些资源可能需要手动识别。
+ 清理过程的持续时间取决于环境的复杂性，可能需要几个小时。
+ 此模式已在 AFT 版本 1.12.2 中进行了测试，并删除了以下资源。如果您使用的是其他版本的 AFT，则可能需要删除其他资源。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html)

**重要**  
按照此模式中的步骤删除的资源无法恢复。在执行这些步骤之前，请仔细验证资源名称并确认它们是由 AFT 创建的。

## 架构
<a name="clean-up-aft-resources-safely-after-state-file-loss-architecture"></a>

下图显示了 AFT 组件和高级工作流。AFT 设置了一个 Terraform 管道，可帮助您在 AWS Control Tower中预调配和自定义您的账户。AFT 遵循一种 GitOps 模型来实现账户配置过程的自动化 AWS Control Tower。借助 AFT，您可以创建一个账户请求 Terraform 文件，以获取触发 AFT 账户预调配工作流的输入。账户预调配完成后，AFT 可以自动运行其他自定义步骤。

![\[AFT 组件和高级工作流。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/1342c0a6-4b07-46df-a063-ceab2e2f83c8/images/3e0cae87-20ef-4fcc-aacf-bb450844ac56.png)


在此架构中：
+ **AWS Control Tower 管理账户**是专用于 AWS 账户 该 AWS Control Tower 服务的账户。这通常也称为 *AWS 付款人账户*或 *AWS Organizations 管理账户*。
+ **AFT 管理账户**是专门用 AWS 账户 于 AFT 管理操作的账户。这与企业的管理账户不同。
+ **Vended 账户** AWS 账户 包含您选择的所有基准组件和控件。AFT AWS Control Tower 用来出售一个新账户。

有关此架构的更多信息，请参阅 AWS Control Tower 研讨会中的 [AFT 简介](https://catalog.workshops.aws/control-tower/en-US/customization/aft)。

## 工具
<a name="clean-up-aft-resources-safely-after-state-file-loss-tools"></a>

**AWS 服务**
+ [AWS Control Tower](https://docs.aws.amazon.com/controltower/latest/userguide/what-is-control-tower.html)按照规范性最佳实践，帮助您设置和管理 AWS 多账户环境。
+ AWS A@@ [ccount Factory for Terraform (AFT)](https://docs.aws.amazon.com/controltower/latest/userguide/taf-account-provisioning.html) 设置了 Terraform 管道，以帮助你在中配置和自定义账户和资源。 AWS Control Tower
+ [AWS Organizations](https://docs.aws.amazon.com/organizations/latest/userguide/orgs_introduction.html)随着 AWS 资源的增长和扩展，可以帮助你集中管理和治理环境。通过使用 Organizations，您可以创建账户并分配资源，对账户进行分组以组织工作流，应用策略以满足治理的需要，并通过对所有账户使用统一的付款方式来简化账单。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 通过控制谁经过身份验证并有权使用 AWS 资源，从而帮助您安全地管理对资源的访问权限。此模式需要 IAM 角色和权限。

**其他工具**
+ [Terraform](https://www.terraform.io/) 是一款基础设施即代码 (IaC) 工具 HashiCorp ，可帮助您创建和管理云和本地资源。

## 最佳实践
<a name="clean-up-aft-resources-safely-after-state-file-loss-best-practices"></a>
+ 有关 AWS Control Tower信息，请参阅 AWS Control Tower 文档中的[AWS Control Tower 管理员最佳实践](https://docs.aws.amazon.com/controltower/latest/userguide/best-practices.html)。
+ 有关 IAM 的信息，请参阅 IAM 文档中的[安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 操作说明
<a name="clean-up-aft-resources-safely-after-state-file-loss-epics"></a>

### 删除 AFT 管理账户中的 AFT 资源
<a name="delete-aft-resources-in-the-aft-management-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除由 AFT 标签标识的资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 AWS Backup 备份存储库。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 Amazon CloudWatch 资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 AWS KMS 资源。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 

### 删除日志存档账户中的 AFT 资源
<a name="delete-aft-resources-in-the-log-archive-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除 S3 存储桶。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 

### 删除审计账户中的 AFT 资源
<a name="delete-aft-resources-in-the-audit-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 

### 删除 AWS Control Tower 管理账户中的 AFT 资源
<a name="delete-aft-resources-in-the-ctower-management-account"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 删除 IAM 角色。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 
| 删除 EventBridge 规则。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | AWS 管理员、AWS DevOps、 DevOps 工程师 | 

## 问题排查
<a name="clean-up-aft-resources-safely-after-state-file-loss-troubleshooting"></a>


| 问题 | 解决方案 | 
| --- | --- | 
| 断开互联网网关失败。 | 在删除由 **AFT** 标签标识的资源时，如果在断开或删除互联网网关时遇到此问题，则必须先删除 VPC 端点：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | 
| 您找不到指定的 CloudWatch 查询。 | 如果您找不到 AFT 创建的 CloudWatch 查询，请按照以下步骤操作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/clean-up-aft-resources-safely-after-state-file-loss.html) | 

## 相关资源
<a name="clean-up-aft-resources-safely-after-state-file-loss-resources"></a>
+ AFT：
  + [GitHub 存储库](https://github.com/aws-ia/terraform-aws-control_tower_account_factory)
  + [工作坊](https://catalog.workshops.aws/control-tower/en-US/customization/aft)
  + [文档](https://docs.aws.amazon.com/controltower/latest/userguide/aft-getting-started.html)
+ [AWS Control Tower 文档](https://docs.aws.amazon.com/controltower/latest/userguide/getting-started-with-control-tower.html)

## 附加信息
<a name="clean-up-aft-resources-safely-after-state-file-loss-additional"></a>

要在 L CloudWatch ogs Insights 控制面板上查看 AFT **查询，请从右上角选择已保存和示例查询**图标，如以下屏幕截图所示：

![\[在 “ CloudWatch 日志见解” 控制面板上访问 AFT 查询。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/1342c0a6-4b07-46df-a063-ceab2e2f83c8/images/255d4032-738b-4600-9084-9684d2e9a328.png)
