Closed13

tflintを試す

ara_ta3ara_ta3

awsは別途書く必要がある

.tflint.hcl

plugin "aws" {
    enabled = true
    version = "0.28.0"
    source  = "github.com/terraform-linters/tflint-ruleset-aws"
}
ara_ta3ara_ta3

initしたらBad Credentialsって怒られた

Installing `aws` plugin...
Failed to install a plugin; Failed to fetch GitHub releases: GET https://api.github.com/repos/terraform-linters/tflint-ruleset-aws/releases/tags/v0.28.0: 401 Bad credentials []

ちなみにtokenは存在した

env |grep GITHUB_TOKEN
GITHUB_TOKEN={見せられないよ}

https://github.com/terraform-linters/tflint/issues/1268

Specifically I had a GITHUB_TOKEN set that (apparently) was not working to access GitHub. Doing unset GITHUB_TOKEN fixed the issue. Totally my fault of course but probably took longer to resolve than it could have.

Thanks for taking it seriously though!

自分のtoken、もう使えないやつが入ってる説があるw
unset GITHUB_TOKENしたら通った

ara_ta3ara_ta3

CI

github actionsはここ https://github.com/terraform-linters/setup-tflint

circle ciのorbはここ https://github.com/terraform-linters/tflint-orb?tab=readme-ov-file
なんだけど、これはなくなってそうw
自前でインストールか、Dockerで起動するのが無難かなぁ

つまりこう

curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash

# or

docker run --rm -v $(pwd):/data -t ghcr.io/terraform-linters/tflint
ara_ta3ara_ta3

ruleのignore

2通りありそう

.tflint.hclへの記述で全体への反映

rule  "terraform_unused_declarations" {
  enabled = false
}

tfファイルに # tflint-ignore: の記述

例えばtflintでこう表示されていた時

  on iam.tf line 24:
  24: data "aws_iam_policy_document" "xxxxx" {

data "aws_iam_policy_document" "xxxxx" の上に記述する
resourceの内側の記述にwarningやerrorが表示されている時、resourceの上の行に書いても反映はされない

# tflint-ignore: terraform_unused_declarations
data "aws_iam_policy_document" "xxxxx" 
ara_ta3ara_ta3

awsを使ってる場合の .tflint.hcl がとりあえずこんな感じかな

plugin "terraform" {
  enabled = true
  preset  = "recommended"
}

plugin "aws" {
    enabled = true
    version = "0.28.0"
    source  = "github.com/terraform-linters/tflint-ruleset-aws"
}
ara_ta3ara_ta3

Warningがあるときもexit codeは0じゃなくなるが、--minimum-failure-severityでerrorのみを対象に出来る

tflint  --minimum-failure-severity=error
ara_ta3ara_ta3

dockerでやる場合、--initをどこかで走らせる必要があるのでdockerfileにしたほうが良いか

FROM ghcr.io/terraform-linters/tflint
COPY . /data
RUN tflint --init
ara_ta3ara_ta3

凝ったことせずMakefile書いとくとこんなんかなー

tflint/docker:
	docker build -t tflint .
	docker run -t tflint

tflint:
	tflint --init
	tflint
ara_ta3ara_ta3

導入に当たってやると良さそうなこと

コマンドの用意

tflint/docker:
	docker build -t tflint .
	docker run -t tflint

# brew install tflint
tflint:
	tflint --init
	tflint --minimum-failure-severity=error

warningと向き合うのが難しいなら --minimum-failure-severity=error を入れておく

初期設定

.tflint.hcl

plugin "terraform" {
  enabled = true
  preset  = "recommended"
}

plugin "aws" {
    enabled = true
    version = "0.28.0"
    source  = "github.com/terraform-linters/tflint-ruleset-aws"
}

修正

以下を駆使したり向き合ったりして解消する

  • tflint --fix
  • # tflint-ignore: terraform_unused_declarations
rule  "terraform_unused_declarations" {
  enabled = false
}

CI

circleci

        - run:
            name: install tflint
            command: curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
        - run:
            name: run tflint
            command: make tflint

tflintのバージョン管理

renovateが対応しているのでそれを入れる
https://docs.renovatebot.com/modules/manager/
https://docs.renovatebot.com/modules/manager/tflint-plugin/

{
  "enabledManagers": [ "tflint-plugin" ] 
}
このスクラップは4ヶ月前にクローズされました