🦁

Terraformのログレベルを調整する方法(TF_LOG)

2023/11/08に公開

記事の内容

Terraformのログレベルを調整する方法を解説します。

記事を読むと得られるもの

  • Terraformのログレベルの設定方法

対象読者

  • Terraform User

記事の長さ

1分で読めます

ログの調整

基本的には公式ドキュメント通りです。

https://developer.hashicorp.com/terraform/internals/debugging

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR

の中から、自分が出したいログレベルを選び、TF_LOGという環境変数にセットします。

$ export TF_LOG=DEBUG
$ terraform plan
2023-11-08T11:43:53.017+0900 [INFO]  Terraform version: 1.6.2 dev
2023-11-08T11:43:53.017+0900 [DEBUG] using github.com/hashicorp/go-tfe v1.36.0
2023-11-08T11:43:53.017+0900 [DEBUG] using github.com/hashicorp/hcl/v2 v2.19.1
2023-11-08T11:43:53.017+0900 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2023-11-08T11:43:53.017+0900 [DEBUG] using github.com/zclconf/go-cty v1.14.1
2023-11-08T11:43:53.017+0900 [INFO]  Go runtime version: go1.21.3
2023-11-08T11:43:53.017+0900 [INFO]  CLI args: []string{"terraform", "plan"}
...
2023-11-08T11:43:53.987+0900 [DEBUG] ProviderTransformer: "aws_cloudwatch_log_group.app" (*terraform.NodeApplyableResourceInstance) needs provider["registry.terraform.io/hashicorp/aws"]
2023-11-08T11:43:53.987+0900 [DEBUG] ReferenceTransformer: "provider[\"registry.terraform.io/hashicorp/aws\"]" references: []
2023-11-08T11:43:53.988+0900 [DEBUG] ReferenceTransformer: "aws_cloudwatch_log_group.app (expand)" references: []
2023-11-08T11:43:53.988+0900 [DEBUG] ReferenceTransformer: "aws_cloudwatch_log_group.app" references: []
2023-11-08T11:43:53.988+0900 [INFO]  backend/local: plan operation completed
...

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_log_group.app will be created
  + resource "aws_cloudwatch_log_group" "app" {
      + arn               = (known after apply)
      + id                = (known after apply)
      + name              = "services-app"
      + name_prefix       = (known after apply)
      + retention_in_days = 14
      + skip_destroy      = false
      + tags_all          = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

DEBUGを指定すると大量のログが出力されます。

$ export TF_LOG=ERROR
$ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_log_group.app will be created
  + resource "aws_cloudwatch_log_group" "app" {
      + arn               = (known after apply)
      + id                = (known after apply)
      + name              = "services-app"
      + name_prefix       = (known after apply)
      + retention_in_days = 14
      + skip_destroy      = false
      + tags_all          = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

ERRORを指定すると、最小限のログのみ出力されます。

CICDやローカル環境等、terraform plan, terraform applyを実行する場所で、最適なログ出力を設定してください。

note

勉強法やキャリア構築法など、エンジニアに役立つ記事をnoteで配信しています。

https://note.com/ring_belle/membership

Discussion