あめがえるのITブログ

頑張りすぎない。ほどほどに頑張るブログ。

【Terraform】便利ツールを調べてみた(Windows)


ツール

  1. terraform-lint
  2. terraform-docs
  3. terragrunt
  4. trivy
  5. tfsec

1. terraform-lint(tflint)

Terraformコードの静的解析ツール。ベストプラクティスの違反や型の不一致、未使用の変数を検出。各クラウドプロバイダー(AWS、Azure、GCP)のプラグインを使用して詳細なチェックが可能。

インストール方法
 ・tflintの公式リリースページから、Windows用のバイナリをダウンロード
 ・ZIPを解凍し、tflint.exe をPATHが通ったディレクトリに移動
 ・コマンドプロンプトまたはPowerShellでインストール確認

> tflint --version
TFLint version 0.54.0
+ ruleset.terraform (0.10.0-bundled)

github.com
使い方
 ・tfファイルがあるディレクトリへ移動
 ・下記コマンドを実行

> tflint
10 issue(s) found:
Warning: terraform "required_version" attribute is required (terraform_required_version)
  on ecs.tf line 1:
Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.10.0/docs/rules/terraform_required_version.md


2. terraform-docs

Terraformモジュールのドキュメントを自動生成するツール。
変数、出力、リソースをMarkdown形式で生成し、コードとドキュメントの整合性を保つ。

インストール方法
 ・terraform-docsの公式リリースページから、Windows用のバイナリをダウンロード
 ・ZIPを解凍し、terraform-docs.exe をPATHが通ったディレクトリに移動
 ・インストール確認

> terraform-docs --version
terraform-docs version v0.19.0 af31cc6 windows/amd64

github.com
使い方
 ・tfファイルがあるディレクトリへ移動
 ・下記コマンドを実行

> > terraform-docs markdown table --output-file test.md --output-mode inject .  
test.md updated successfully

 ・mdファイル中身

<!-- BEGIN_TF_DOCS -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.77.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_ecs_cluster.my_cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource |
| [aws_ecs_service.my_service](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |


3. terragrunt

Terraformのラッパーツールで、複数のTerraformモジュールの管理を簡略化。
環境ごとの設定を再利用しやすくする。

インストール方法
 ・terragruntの公式リリースページから、Windows用のバイナリをダウンロード
 ・ファイル名がterragrunt_windows_amd64.exeのような場合、terragrunt.exeに変更
 ・ダウンロードしたファイルをPATHが通ったディレクトリに移動
 ・インストール確認

> terragrunt --version
terragrunt version v0.69.1

github.com
使い方
 ・tfファイルがあるディレクトリへ移動
 ・カレントディレクトリでterraform-modules/ディレクトリを作成
 ・カレントディレクトリでterraform-modules/example-moduleファイルを作成 ※空でOK
 ・カレントディレクトリでterragrunt.hclファイルを作成

terraform {
  source = "./terraform-modules/example-module" 
}

inputs = {
  example_variable = "example_value" # 必要に応じて変数を定義
}

 ・下記コマンドを実行

> terragrunt plan
14:09:49.814 INFO   [.] Downloading Terraform configurations from 
デスクトップ/terraform/.terragrunt-cache/8uuzlDXL0NfFIp58akebUnKef_8/gIeCE5Mc_L_QSS2s8VryXc4Ibf8
14:09:49.921 STDOUT [.] terraform: Initializing the backend...
14:09:49.924 STDOUT [.] terraform: Initializing provider plugins...
...


4. trivy

オープンソース脆弱性スキャナー。主に、コンテナやクラウドネイティブなアプリケーションのセキュリティを強化するために使用。Terraform以外にもスキャン可能。
 ・Dockerイメージ(コンテナイメージ)
 ・ファイルシステム(ローカルのファイルやディレクトリ)
 ・Kubernetes YAMLファイル(設定やマニフェスト
 ・Terraformコード(インフラストラクチャの設定ファイル)
 ・Gitリポジトリ

インストール方法
 ・trivyの公式リリースページから、Windows用のバイナリをダウンロード
 ・ダウンロードしたファイルをPATHが通ったディレクトリに移動
 ・インストール確認

> trivy --version
Version: 0.57.1

github.com 使い方
 ・コマンドプロンプトを起動し、下記を実行

> trivy config "C:\Users\xxxx\デスクトップ\terraform\ecs.tf"
2024-11-25T09:01:04+09:00       INFO    [misconfig] Misconfiguration scanning is enabled
2024-11-25T09:01:04+09:00       INFO    [misconfig] Need to update the built-in checks
2024-11-25T09:01:04+09:00       INFO    [misconfig] Downloading the built-in checks...
2024-11-25T09:01:05+09:00       ERROR   [misconfig] Falling back to embedded checks     err="failed to download built-in policies: download error: oci download error: failed to fetch the layer: GET https://ghcr.io/v2/aquasecurity/trivy-checks/blobs/sha256:16442a4593a0395452e678ef699a880eec94d9211dfc887d52574beb78b95030: TOOMANYREQUESTS: retry-after: 579.476µs, allowed: 44000/minute"
2024-11-25T09:01:06+09:00       INFO    [terraform scanner] Scanning root module        file_path="."
2024-11-25T09:01:06+09:00       INFO    Detected config files   num=2

ecs.tf (terraform)
==================
Tests: 5 (SUCCESSES: 0, FAILURES: 5)
Failures: 5 (UNKNOWN: 0, LOW: 3, MEDIUM: 1, HIGH: 0, CRITICAL: 1)


5. tfsec

静的解析ツールで、主に Terraform のコードをスキャンし、セキュリティ上の問題やミスを検出するために使用する。

インストール方法
 ・tfsecの公式リリースページから、Windows用のバイナリをダウンロード
 ・ファイル名がtfsec-windows-amd64.exeのような場合、tfsec.exeに変更
  ※tfsec-checkgenではなくtfsecをダウンロードする。
  ※assetsがすべて表示されていない場合は「Show all xx assets」をクリックする。
 ・ダウンロードしたファイルをPATHが通ったディレクトリに移動
 ・インストール確認

> tfsec --version

======================================================
tfsec is joining the Trivy family

tfsec will continue to remain available
for the time being, although our engineering
attention will be directed at Trivy going forward.

You can read more here:
https://github.com/aquasecurity/tfsec/discussions/1994
======================================================
v1.28.11

github.com 使い方
 ・tfファイルがあるディレクトリへ移動
 ・下記を実行

PS C:\Users\xxx\デスクトップ\terraform> tfsec .

======================================================
tfsec is joining the Trivy family

tfsec will continue to remain available
for the time being, although our engineering
attention will be directed at Trivy going forward.

You can read more here:
https://github.com/aquasecurity/tfsec/discussions/1994
======================================================

Result #1 CRITICAL Security group rule allows ingress from public internet.
────────────────────────────────────────────────────────────────────────────────
  C:\Users\xxx\デスクトップ\terraform\ecs.tf:88
────────────────────────────────────────────────────────────────────────────────
   83    resource "aws_security_group_rule" "ingress_http" {
   84      type        = "ingress"
   85      from_port   = 80
   86      to_port     = 80
   87      protocol    = "tcp"
   88  [   cidr_blocks = ["0.0.0.0/0"]
   89      security_group_id = aws_security_group.my_sg.id
   90    }
────────────────────────────────────────────────────────────────────────────────
          ID aws-ec2-no-public-ingress-sgr
      Impact Your port exposed to the internet
  Resolution Set a more restrictive cidr range

  More Information
  - https://aquasecurity.github.io/tfsec/v1.28.11/checks/aws/ec2/no-public-ingress-sgr/
  - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule#cidr_blocks
────────────────────────────────────────────────────────────────────────────────