1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GitHub ActionsでCloudFormationのテンプレートリントチェック

Last updated at Posted at 2020-06-30

IaCもCI/CDする時代になりましたね~ (今更なに言ってんでしょうかw
普段はCloudFormationを使用する場面が多いので、CFnでデプロイする前にテンプレートファイルが正しく書けているかチェックしたくなります。リントチェックもCIしたいのでGitHub Actions上でリントチェックしてみようと思います。

使用ツール/サービス

CloudFormationのリンターでcfn-lintというツールがありますが、cfn-python-lintの使用を推奨しています。今後はcfn-python-lintを使っていきましょう。

Additional features may be added at the discretion of the project maintainers, however, we strongly advise you use cfn-python-lint at https://github.com/awslabs/cfn-python-lint instead of this project.

GitHub Actions

cfn-python-lintのActionsもありましたが、今回はpipでインストールして実行するようなworkflowにしました。

name: CloudFormation Linter

on:
  pull_request:
    paths:
      - 'cfn/*.yml'

jobs:
  lint:
    name: Lint Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - uses: actions/setup-python@v2
        with:
          python-version: '3.8'

      - name: Install cfn-python-lint
        run: pip install --no-cache-dir cfn-lint

      - name: Execute lint check
        run: cfn-lint cfn/*.yml

      - uses: homoluctus/slatify@master
        with:
          type: ${{ job.status }}
          job_name: ':aws_cfn: *CloudFormation Lint*'
          channel: '#alert_ci_cd'
          url: ${{ secrets.SLACK_WEBHOOK }}
          token: ${{ secrets.GITHUB_TOKEN }}

これでCFnを実行する前にエラーを発見できますね。

工夫点

cfnディレクトリにCFnテンプレートファイルを作成するようにして、プルリクの変更ファイルにcfnディレクトリ以下の*.ymlが含まれていたらCIを実行するようにしました。
もっと厳密にcfnディレクトリ以下を分けるような運用をしている場合は、cfn/**.ymlをトリガーにするといいでしょう。*では/にマッチしませんが、**なら/を含む任意の文字にマッチします。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?