View a markdown version of this page

使用 cfn-lint 验证模板 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

使用 cfn-lint 验证模板

Amazon CloudFormation Linter(cfn-lint)根据 CloudFormation 资源提供程序架构和其他规则检查 JSON 和 YAML 模板。它可以发现无效的资源属性、不支持的属性值、损坏的引用以及常见的最佳实践问题。在将模板发送给 CloudFormation 之前,请使用它来发现这些问题。如果模板使用 Amazon Serverless Application Model(Amazon SAM)转换,则 cfn-lint 会在检查模板之前应用该转换。

有关发布说明、完整的规则列表和所有命令选项,请参阅 GitHub 上的 cfn-lint。

有关本地验证的限制以及部署前需采取的步骤,请参阅了解验证范围。

安装 cfn-lint

从 Python 包索引(PyPI)安装 cfn-lint:

python3 -m pip install cfn-lint

在 macOS 上,您也可以使用 Homebrew 来安装它:

brew install cfn-lint

要确认安装,请输出已安装的版本:

cfn-lint --version

有关 Docker 映像和其他安装方法,请参阅 GitHub 上的安装说明。

运行 cfn-lint

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

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

您还可以使用通配符来检查目录中的所有模板。使用 ** 来包含子目录:

cfn-lint templates/*.yaml cfn-lint templates/**/*.yaml
注意

在 bash 中,在使用 ** 之前先运行 shopt -s globstar,使其与子目录匹配。在 zsh 中,** 默认情况下匹配子目录。

要从标准输入中读取模板,请将 - 作为模板路径传递:

cat template.yaml | cfn-lint -

默认情况下,cfn-lint 会根据 us-east-1 的架构检查模板。要检查其他 Amazon Web Services 区域的模板,请使用 --regions 选项。由于此选项接受多个值,因此请在模板路径之前添加 --,或使用 -t 来标识模板:

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

某些检查取决于参数值。要检查具有特定值的模板,请通过 --parameters 传递它们,或通过 --parameter-files 提供参数文件:

cfn-lint --parameters InstanceType=t3.micro Environment=prod -- template.yaml cfn-lint --parameter-files params.json -- template.yaml

要列出 cfn-lint 可应用的所有规则,请运行以下命令:

cfn-lint --list-rules

了解结果

对于每个调查发现,cfn-lint 会输出规则 ID、一条消息以及问题的文件、行和列。有关规则的完整列表及其各自的检查内容,请参阅 GitHub 上的 cfn-lint 规则。

要更改输出格式,请将 --format 与quiet、parseable、json、junit、pretty 或 sarif 配合使用:

cfn-lint --format json template.yaml

cfn-lint 未发现任何问题时返回退出代码 0,否则返回非零退出代码。要控制哪些调查发现导致非零退出代码,请使用 --non-zero-exit-code。当模板未通过您选择的检查时,您可以使用退出代码停止自动化构建。

配置检查

您可以使用命令行选项、配置文件或模板中的元数据来配置 cfn-lint。当同一设置出现在多处时,命令行选项会覆盖模板元数据,而模板元数据会覆盖配置文件。

选择并配置规则

要跳过规则,请使用 --ignore-checks 并指定一个或多个规则 ID 或规则 ID 前缀。要运行默认关闭的规则,请使用 --include-checks。要在规则与忽略设置匹配的情况下运行该规则,请使用 --mandatory-checks。

cfn-lint --ignore-checks RULE_ID -- template.yaml

某些规则接受设置。使用 --configure-rule 并采用格式 RULE_ID:key=value:

cfn-lint --configure-rule RULE_ID:key=value -- template.yaml

使用配置文件

要保留模板的设置,请在运行 cfn-lint 的目录中创建 .cfnlintrc、.cfnlintrc.yaml 或 .cfnlintrc.yml 文件。您还可以在主目录中创建 ~/.cfnlintrc 文件,以获取适用于所有地方的设置。以下示例选择了要检查的模板,跳过了其中一个模板,设置了区域,并跳过了一条规则:

templates: - templates/**/*.yaml ignore_templates: - templates/legacy.yaml regions: - us-east-1 - us-west-2 ignore_checks: - RULE_ID

当配置文件选择模板时,您可以不带参数地运行 cfn-lint 来检查所有模板。有关所有设置,请参阅 GitHub 上的 Config file。

在模板中配置检查

要为一个模板配置 cfn-lint,请在顶层 Metadata 部分添加一个 cfn-lint 键:

Metadata: cfn-lint: config: regions: - us-east-1 - us-east-2 ignore_checks: - RULE_ID

要跳过单个资源的规则,请在该资源的 Metadata 属性中添加相同的键。以下示例仅跳过一个实例的规则:

Resources: MyInstance: Type: AWS::EC2::Instance Metadata: cfn-lint: config: ignore_checks: - RULE_ID Properties: InstanceType: t3.micro ImageId: ami-abc1234

添加自定义规则

您可以在不编写 Python 的情况下添加自己的检查。自定义规则文件每行包含一条规则,格式如下:

ResourceType Property Operator Value [ErrorLevel] [Message]

例如,当模板使用昂贵的实例类型时,custom_rules.txt 中的以下规则会报告警告:

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

通过 --custom-rules(或 -z)传递文件,或者在配置文件中添加 custom_rules 设置:

cfn-lint --custom-rules custom_rules.txt -- template.yaml

有关支持的运算符,请参阅 GitHub 上的 Custom rules。对于更复杂的检查,您可以用 Python 编写规则并使用 --append-rules 加载它们。要更改 cfn-lint 认为有效的内容,例如要求特定标签,您可以通过 --override-spec 提供一个规范覆盖文件。有关更多信息,请参阅 GitHub 上的 Getting started with rules 和 Customize specifications。

向您的工作流添加 cfn-lint

您可以在以下位置运行 cfn-lint:

  • 在您的编辑器中:CloudFormation 语言服务器使用 cfn-lint 在您键入时显示问题。有关设置,请参阅CloudFormation 语言服务器。其他编辑器(包括 Vim、Emacs 和 Sublime Text)也有相应的插件可用。

  • Git 提交之前:将 cfn-lint 添加到 .pre-commit-config.yaml 文件中,使其在每次提交之前在更改的模板上运行。请将 rev 值替换为要使用的版本。

    repos: - repo: https://github.com/aws-cloudformation/cfn-lint rev: v1.56.0 hooks: - id: cfn-lint files: templates/.*\.(json|yml|yaml)$
  • 在自动化构建中:将 cfn-lint 作为构建步骤运行,并使用退出代码使构建失败。此外,还有一个适用于 cfn-lint 的 GitHub 操作。

  • 在您自己的 Python 代码中:导入 cfnlint.api 并调用 lint 或 lint_all 来检查模板字符串,并在应用程序中处理结果。

    from cfnlint.api import lint, ManualArgs with open("template.yaml") as f: template = f.read() matches = lint(template, config=ManualArgs(regions=["us-east-1"])) for match in matches: print(match)

有关编辑器插件、GitHub 操作和更多集成示例,请参阅 GitHub 上的 cfn-lint。