Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaspy/606b4c30408b4e1bf0322c0f53c1e78b to your computer and use it in GitHub Desktop.
Save chaspy/606b4c30408b4e1bf0322c0f53c1e78b to your computer and use it in GitHub Desktop.
How to add a new lint to tflint-ruleset-aws

How to add a new lint to tflint-ruleset-aws

tflint の aws 向けの rule set は pluggable になっており、新規 rule 追加がしやすくなっています。本発表では DB Instance の engine が valid のテストを追加する PR を元に、Contribution の方法と、その lint の仕組みを解説します。

Who am I

Lead Software Engineer, Site Reliability at Quipper

tflint

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

A Pluggable Terraform Linter

ref: terraform planで消耗しないためにTFLintを作った話

日本人作!かっこいい!@wata727 さん作

aws 用の rule set はこちら terraform-linters/tflint-ruleset-aws

結論: tflint-ruleset-aws に Contribution した

Add lint for db_instance's engine attribute

2021/02/13 追記 Merge されました 🎉

きっかけ

@sogaoh さんのツイート(マジ感謝!)

Screen Shot 2021-02-10 at 19 28 52

眺める

まず aws_db_instance_invalid_type.go を眺める

これは instanceType を lint するもの。まぁこれ engine によって許可する、しないはあると思うので厳密ではないが。少なくとも instance_type field に取りうる値をテストしているようだ。

// AwsDBInstanceInvalidTypeRule checks whether "aws_db_instance" has invalid intance type.
type AwsDBInstanceInvalidTypeRule struct {
	resourceType  string
	attributeName string
	instanceTypes map[string]bool
}

// NewAwsDBInstanceInvalidTypeRule returns new rule with default attributes
func NewAwsDBInstanceInvalidTypeRule() *AwsDBInstanceInvalidTypeRule {
	return &AwsDBInstanceInvalidTypeRule{
		resourceType:  "aws_db_instance",
		attributeName: "instance_class",
		instanceTypes: map[string]bool{
			"db.cr1.8xlarge":   true,
			"db.cv11.18xlarge": true,
			"db.cv11.2xlarge":  true,
			"db.cv11.4xlarge":  true,
      ...
    },
  }
}

まぁなんかいけるやろ(たぶんいける)

でまぁとりあえず "Type" を "Engine" によしなに書き換えて PR 出した。

で、テストいるのでテストも雰囲気で書いた

ざっくりとした仕組み

すごい仕組みで tflint が aws-rulese-aws を読み込み、その中の Ruleset を読み込み、テストを実行している(全然よくわかっていません)

1. pflint が plugin を探す

2. plugin では各 rule を読み込んでテスト

ちゃんと動くのか

動いた!

$ make install
go build
mkdir -p ~/.tflint.d/plugins
mv ./tflint-ruleset-aws ~/.tflint.d/plugins
$ tf init

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/aws from the dependency lock file
- Installing hashicorp/aws v3.27.0...
tflin- Installed hashicorp/aws v3.27.0 (signed by HashiCorp)

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ tflint
1 issue(s) found:

Error: "mysql57" is invalid engine. (aws_db_instance_invalid_engine)

  on main.tf line 4:
   4:   engine               = "mysql57"

Reference: https://github.com/terraform-linters/tflint-ruleset-aws/blob/v0.2.1/docs/rules/aws_db_instance_invalid_engine.md

rule を追加する方法

  1. 既存の Rule を参考に aws_<resource_name>_invalid_<something_you_want_to_test>.go を書く
  2. テストを書く
  3. make install して動かす
  4. document を書く
  5. PR を出す!
  6. merge 🎉

まとめ

  • まだまだ rule は多いとは言えないので、apply 爆死を経験したら lint できないか?って考えてみよう
  • できそうなら Contribution してみよう!

おしまい

@chaspy
Copy link
Author

chaspy commented Feb 10, 2021

  • sogaoh さんのツイート

Screen Shot 2021-02-10 at 19 28 52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment