> For the complete documentation index, see [llms.txt](https://aws-gcr-wwso-security.gitbook.io/an-quan-zui-jia-shi-jian/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aws-gcr-wwso-security.gitbook.io/an-quan-zui-jia-shi-jian/an-quan-ti-xi-jian-she/cong-devops-dao-devsecops/iac-an-quan-jian-cha/cfn-lint.md).

# CFN Lint

Open Source tool (CFN Lint)\[[https://github.com/aws-cloudformation/cfn-lint ](https://github.com/aws-cloudformation/cfn-lint)]. CFN Lint 是一款基于 Python 的 CLI，用于扫描 CloudFormation 模板。

CFN Lint 配置了各种预设检查和测试。

## 安装CFN Lint

如果是macOS系统可以使用以下命令安装：

```
brew install cfn-lint
```

检查是否可以在终端上运行 cfn-lint 的 cli。

```
$ cfn-lint
```

## 扫描模板

CFN Lint 可与 JSON 和 YAML CloudFormation 模板配合使用，包括检查有效值、资源属性和最佳实践。 CFN Lint 还允许用户定义规则。

* CFN Lint 主要用于检查模板结构，并没有很多用于安全扫描的高级规则。

假设创建YAML文件名为cfn\_link\_example\_template.yaml，内容如下：

```
AWSTemplateFormatVersion: "2010-09-09"
Resources:
        myInstance:
                Type: AWS::EC2::Instance
                Properties:
                        InstanceType: m4.16xlarge
                        ImageId: ami-asdfef
```

创建检查规则custom\_rule.txt，内容如下：

{% code overflow="wrap" %}

```
AWS::EC2::Instance InstanceType NOT_EQUALS "m4.16xlarge" WARN "This is an expensive instance type, don't use it"
```

{% endcode %}

命令行工具执行时与文件在同一个路径下：

```
cfn-lint cfn_link_example_template.yaml -z custom_rule.txt 
```

返回以下提示信息：

<mark style="background-color:yellow;">W9001 This is an expensive instance type, don't use it</mark>

<mark style="background-color:yellow;">cfn\_link\_example\_template.yaml:6:25</mark>

参考资料

预置的规则：<https://github.com/aws-cloudformation/cfn-lint/blob/main/docs/rules.md>
