1
1

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.

インフラをコード化したし、オートレビューするか(tf_cop)

Last updated at Posted at 2018-09-30

tl;dr

何が嬉しいの?

  • わざわざネーミングルールを意識しなくてよくなります
  • コンソール上で名前の表示されないEC2やらが無くなります(tagのつけ忘れ防止)
  • descriptionのないterraformのコードが無くなります(descriptionつけ忘れ防止)
  • その他色々

テスト実行結果

以下の感じでコードをかくと...

test.py
import tf_cop

if __name__ == '__main__':
    test = tf_cop.TfCop()
    test.tf_review("./test", "./review_book")
    output = test.output()
    print(output["output_log"])
    print(output["output_summary_log"])

こんな感じの結果が出る

output
==========================================================
RESOURCE AWS_S3_BUCKET.TEST_TF_REVIEW_BUCKET
==========================================================
[WARN] desc_checker : description not use
{   'acl': 'private',
    'bucket': '${terraform.env}-tf-review-bucket'}
[ALERT] tag_checker : tags not use
{   'acl': 'private',
    'bucket': '${terraform.env}-tf-review-bucket'}

==========================================================
RESOURCE AWS_S3_BUCKET.TEST_TF_REVIEW_BUCKET2
==========================================================
[WARN] desc_checker : description not use
{   'acl': 'private',
    'bucket': '${terraform.env}-tf-review-bucket2',
    'lifecycle_rule': {...},
    'logging': {...},
    'tags': {...}}
[PASS] tag_checker : passed
[PASS] name_checker : passed
[PASS] env_checker : passed


 =======================
| RESOURCE NUM  : 2     |
| SKIP NUM      : 0     |
| PASS NUM      : 3     |
| WARN NUM      : 2     |
| ALERT NUM     : 1     |
 =======================

※実際は色付きで出ます

チェックの内容

テストだと以下のyamlの設定でS3をテストしています。

s3.yaml
aws_s3_bucket:
-
  title: desc_checker
  desc: simple existance checker
  mode: existance
  warn: True
  key: description
-
  title: tag_checker
  desc: nested value checker
  mode: nested
  key: tags
  nest:
    -
      title: name_checker
      desc: nested value checker
      mode: existance
      key: Name
    -
      title: env_checker
      desc: nested value checker
      mode: value
      warn: True
      key: Env
      value: (dev|stg|prd)

テスト対象のtfファイルは以下のものです

s3.tf
resource "aws_s3_bucket" "test_tf_review_bucket" {
  bucket = "${terraform.env}-tf-review-bucket"
  acl = "private"
}

resource "aws_s3_bucket" "test_tf_review_bucket2" {
  bucket = "${terraform.env}-tf-review-bucket2"
  acl = "private"

  logging {
    target_bucket = "${terraform.env}-tf-review-bucket-log"
    target_prefix = "${terraform.env}-tf-review-bucket/"
  }

  lifecycle_rule {
    id = "cap-s3bucket-lifecycle-rule"
    prefix = ""
    enabled = true
    abort_incomplete_multipart_upload_days = 7
  }

  tags {
    Name = "${terraform.env}-tf-review-bucket"
    Env = "dev"
  }
}

そのため tagsName = が書かれていない等のアラートが出ます。

できるまで

  • テスト用のyamlの設計が悩む
    • alertかどうかを alert: True で入れるのはいまだに微妙な気がする笑
    • 否定形も定義できるようにと言いつつまだやっていない
  • Pypi で公開するのが難しかった。。。

感想!!

  • 自分で作ったものが世に出て行くのは気持ちいい!
  • Pypi に公開するのが想像の100倍楽だった
  • 誰かにイシュー上げてもらえるぐらいになりたいな。。。
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?