

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 驗證範本 `cfn-lint`
<a name="cfn-lint"></a>

CloudFormation Linter (`cfn-lint`) 會根據 [CloudFormation 資源提供者結構描述和其他規則檢查 JSON ](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/resource-type-schemas.html)和 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>

`cfn-lint` 從 Python 套件索引 (PyPI) 安裝：

```
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 上專案文件中的 [Config 檔案](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 動作、預先遞交組態、自訂規則等範例，請參閱 GitHub 上的 [cfn-lint 專案文件](https://github.com/aws-cloudformation/cfn-lint)。