

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

# 使用 Python 在 AMS 中自动创建 RFC
<a name="automatically-create-an-rfc-in-ams-using-python"></a>

*Gnanasekaran Kailasam，Amazon Web Services*

## Summary
<a name="automatically-create-an-rfc-in-ams-using-python-summary"></a>

AWS Managed Services (AMS) 通过持续管理您的 Amazon Web Services (AWS) 基础设施，帮助您更高效、更安全地运营基于云的基础设施。若要对托管环境进行更改，您需要创建并提交新的变更请求 (RFC)，其中包含特定操作或操作的更改类型 (CT) ID。

但是，手动创建 RFC 可能需要大约五分钟，组织中的团队可能需要 RFCs 每天提交多个 RFC。此模式可帮助您自动执行 RFC 创建进程，缩短每个 RFC 的创建时间，并消除手动错误。  

此模式描述了如何使用 Python 代码自动创建 `Stop EC2 instance` RFC，可停止您的 AWS 账户中的 Amazon Elastic Compute Cloud（Amazon EC2）实例。然后，您可以将此模式的方法和 Python 自动应用于其他类型的 RFC 。 

## 先决条件和限制
<a name="automatically-create-an-rfc-in-ams-using-python-prereqs"></a>

**先决条件**
+ AMS 高级账户。有关这方面的更多信息，请参阅 AWS Managed Services 文档中的 [AMS 运营计划](https://docs.aws.amazon.com/managedservices/latest/accelerate-guide/what-is-ams-op-plans.html)。
+ 您的 AMS 账户中至少一个现有 EC2 实例。
+ 了解如何在 AMS RFCs 中创建和提交。
+ 熟悉 Python。

**限制**
+ 您只能 RFCs 用于更改您的 AMS 账户。您的 AWS 账户使用不同过程执行类似更改。

## 架构
<a name="automatically-create-an-rfc-in-ams-using-python-architecture"></a>

**技术堆栈**
+ AMS
+ AWS 命令行界面（AWS CLI）
+ 适用于 Python 的 Amazon SDK (Boto3)
+ Python 及其必需软件包 (JSON 和 Boto3)

**自动化和扩缩**

此模式提供了自动化 `Stop EC2 instance` RFC 的示例代码，但您可以将此模式的示例代码和方法用于其他 RFCs模式。

## 工具
<a name="automatically-create-an-rfc-in-ams-using-python-tools"></a>
+ [AWS Managed Services](https://docs.aws.amazon.com/managedservices/latest/ctexguide/ex-rfc-use-examples.html) – AMS 可帮助您更高效、更安全地运营 AWS 基础设施。
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) – AWS 命令行界面（AWS CLI）是一款统一工具，可用于管理 Amazon Web Services。在 AMS 中，变更管理 API 提供用于创建和管理的操作 RFCs。
+ [适用于 Python 的 Amazon SDK (Boto3)](https://docs.aws.amazon.com/pythonsdk/) – 适用于 Python 的开发工具包可以轻松集成 Python 应用程序、库或脚本与 Amazon Web Services。

**代码**

`AMS Stop EC2 Instance.zip` 文件（附件）包含用于创建 `Stop EC2 instance` RFC 的 Python 代码。您还可以将此代码配置为：为多个 EC2 实例提交单个 RFC。

## 操作说明
<a name="automatically-create-an-rfc-in-ams-using-python-epics"></a>

### 选项 1 – 设置适用于 macOS 或 Linux 的环境
<a name="option-1-ndash-set-up-environment-for-macos-or-linux"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
|  安装并验证 Python。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系统管理员 | 
|  安装 AWS CLI。 | 运行 `pip install awscli --upgrade –user` 命令，以安装 AWS CLI*。 * | AWS 系统管理员 | 
|  安装 Boto3。 | 运行 `pip install boto3` 命令，以安装 Boto3。 | AWS 系统管理员 | 
| 安装 JSON。 | 运行 `pip install json` 命令，以安装 JSON。 | AWS 系统管理员 | 
| 设置 AMS CLI。 | 登录 AWS 管理控制台，打开 AMS 控制台，然后选择**文档**。下载包含 AMS CLI 的 .zip 文件，将其解压缩，然后将其安装至本地计算机。在安装 AMS CLI 后，运行 `aws amscm help` 命令。输出提供了 AMS 变更管理流程相关信息。 | AWS 系统管理员 | 

### 选项 2 - 设置适用于 Windows 的环境
<a name="option-2-ndash-set-up-environment-for-windows"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
|  安装并验证 Python。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系统管理员 | 
| 安装 AWS CLI。 | 运行 `pip install awscli --upgrade –user` 命令，以安装 AWS CLI。 | AWS 系统管理员 | 
|  安装 Boto3。 | 运行 `pip install boto3` 命令，以安装 Boto3。 | AWS 系统管理员 | 
| 安装 JSON。 | 运行 `pip install json` 命令，以安装 JSON。 | AWS 系统管理员 | 
| 设置 AMS CLI。 | 登录 AWS 管理控制台，打开 AMS 控制台，然后选择**文档**。下载包含 AMS CLI 的 .zip 文件，将其解压缩，然后将其安装至本地计算机。在安装 AMS CLI 后，运行 `aws amscm help` 命令。输出提供了 AMS 变更管理流程相关信息 | AWS 系统管理员 | 

### 提取适用于 RFC 的 CT ID 与执行参数
<a name="extract-the-ct-id-and-execution-parameters-for-the-rfc"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 提取适用于 RFC 的 CT ID、版本与执行参数。 | 每个 RFC 都有不同的 CT ID、版本以及执行参数。您可以使用以下选项之一提取此信息：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html)要使此模式的 Python 自动化适应其他模式 RFCs，请将文件（附件）中的 `ams_stop_ec2_instance` Python 代码`AMS Stop EC2 Instance.zip`文件中的 CT 类型和参数值替换为您提取的值。 | AWS 系统管理员 | 

### 运行 Python 自动化
<a name="run-the-python-automation"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 运行 Python 自动化。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/automatically-create-an-rfc-in-ams-using-python.html) | AWS 系统管理员 | 

## 相关资源
<a name="automatically-create-an-rfc-in-ams-using-python-resources"></a>
+ [什么是变更类型？](https://docs.aws.amazon.com/managedservices/latest/ctexguide/understanding-cts.html)
+ [CLI 教程：高可用性双层堆栈 (Linux/RHEL)](https://docs.aws.amazon.com/managedservices/latest/ctexguide/tut-create-ha-stack.html)

## 附件
<a name="attachments-2b6c68fd-a27e-4c8b-934d-caec50c196ed"></a>

要访问与此文档相关联的其他内容，请解压以下文件：[attachment.zip](samples/p-attach/2b6c68fd-a27e-4c8b-934d-caec50c196ed/attachments/attachment.zip)