

# 使用 `cfn-lint` 验证模板
<a name="cfn-lint"></a>

CloudFormation Linter（`cfn-lint`）根据 [CloudFormation 资源提供程序架构](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/resource-type-schemas.html)和其他规则检查 JSON 和 YAML 模板。在您将模板发送到 CloudFormation 之前，它可以查找无效的资源属性、不支持的值和常见问题。

源代码、发行说明、设置和完整的规则列表可在 [GitHub 上的 cfn-lint 存储库](https://github.com/aws-cloudformation/cfn-lint)中找到。

**Topics**
+ [安装 `cfn-lint`](#cfn-lint-install)
+ [运行 `cfn-lint`](#cfn-lint-run)
+ [配置检查](#cfn-lint-configure)
+ [向您的工作流添加 `cfn-lint`](#cfn-lint-integrate)

## 安装 `cfn-lint`
<a name="cfn-lint-install"></a>

从 Python 包索引（PyPI）安装 `cfn-lint`：

```
python3 -m pip install cfn-lint
```

在 macOS 上，您也可以使用 Homebrew 来安装它：

```
brew install cfn-lint
```

有关支持的 Python 版本和其他安装方法，请参阅 GitHub 上的[安装说明](https://github.com/aws-cloudformation/cfn-lint#install)。

## 运行 `cfn-lint`
<a name="cfn-lint-run"></a>

向命令传递一个或多个模板路径：

```
cfn-lint template.yaml
cfn-lint template-one.yaml template-two.json
```

要检查多个 AWS 区域 的模板，请使用 `--regions` 选项：

```
cfn-lint --regions us-east-1 us-west-2 -- template.yaml
```

当发现问题时，该命令会返回非零退出代码。当模板未通过您选择的检查时，您可以使用此行为停止自动构建。

## 配置检查
<a name="cfn-lint-configure"></a>

使用 `.cfnlintrc`、`.cfnlintrc.yaml` 或 `.cfnlintrc.yml` 文件将项目设置与模板一同保存。您可以使用此文件来选择模板、包括或忽略检查、选择 AWS 区域 以及配置规则行为。有关所有设置，请参阅 GitHub 上项目文档中的[配置文件](https://github.com/aws-cloudformation/cfn-lint#config-file)。

## 向您的工作流添加 `cfn-lint`
<a name="cfn-lint-integrate"></a>

您可以从受支持的编辑器运行 `cfn-lint`，也可以在执行 Git 提交前自动运行，或者将其作为自动构建的一部分来运行。CloudFormation 语言服务器还使用 `cfn-lint` 在您键入时显示问题。有关语言服务器设置，请参阅[CloudFormation 语言服务器](ide-extension.md)。

有关编辑器插件、GitHub Actions、预提交配置、自定义规则和更多示例，请参阅 GitHub 上的 [cfn-lint 项目文档](https://github.com/aws-cloudformation/cfn-lint)。