

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

# 开发自定义 CloudFormation Hook 的先决条件
<a name="hooks-prerequisites"></a>

你可以用 Java 或 Python 开发一个自定义 Hook。以下是开发自定义 Hook 的先决条件：

**Java 先决条件**
+ [Apache Maven](https://maven.apache.org/install.html)
+ [JDK 17](https://www.oracle.com/java/technologies/downloads/#java17)
**注意**  
如果您打算使用[CloudFormation 命令行界面 (CLI)](https://docs.aws.amazon.com/cloudformation-cli/latest/userguide/what-is-cloudformation-cli.html) 启动适用于 Java 的 Hooks 项目，则还必须安装 Python 3.8 或更高版本。 CloudFormation CLI 的 Java 插件可以通过`pip`（Python 的包管理器）安装，该插件由 Python 分发。

要为您的 Java Hooks 项目实现 Hook 处理程序，您可以下载 [Java Hook 处理程序示例文件](samples/java-handlers.zip)。

**Python 先决条件**
+ [Python 版本 3.8](https://www.python.org/downloads/) 或更高版本。

要为你的 Python Hooks 项目实现钩子处理程序，你可以下载 [Python Hook 处理程序示例文件](samples/python-handlers.zip)。

## 开发 Hook 的权限
<a name="hooks-development-permissions"></a>

除了 CloudFormation `Create``Update`、和`Delete`堆栈权限外，您还需要访问以下 AWS CloudFormation 操作。对这些操作的访问权限通过您的 IAM 角色的 CloudFormation 策略进行管理。
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/register-type.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/register-type.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-types.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-types.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deregister-type.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deregister-type.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/set-type-configuration.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/set-type-configuration.html)

有关更多信息，请参阅 [为 CloudFormation Hook 授予 IAM 权限](grant-iam-permissions-for-hooks.md)。

## 为 Hooks 设置开发环境
<a name="hooks-environment"></a>

要开发 Hook，你应该熟悉[CloudFormation 模板](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-guide.html)以及 Python 或 Java。

 

**要安装 CloudFormation CLI 和相关插件，请执行以下操作：**

1. 使用 Python 包管理器安装 CloudFormation CLI。`pip`

   ```
   pip3 install cloudformation-cli
   ```

1. 安装用于 CloudFormation CLI 的 Python 或 Java 插件。

------
#### [ Python ]

   ```
   pip3 install cloudformation-cli-python-plugin
   ```

------
#### [ Java ]

   ```
   pip3 install cloudformation-cli-java-plugin
   ```

------

要升级 CloudFormation CLI 和插件，您可以使用升级选项。

------
#### [ Python ]

```
pip3 install --upgrade cloudformation-cli cloudformation-cli-python-plugin
```

------
#### [ Java ]

```
pip3 install --upgrade cloudformation-cli cloudformation-cli-java-plugin
```

------