尝试输出Terraform的日志

首先

我打算试一下在terraform中,可以通过指定TRACE、DEBUG、INFO、WARN、ERROR等级来输出执行时的日志。试试看哪个级别会输出什么样的日志内容。

工作方法

我打算应用一个只包含创建ec2的简单tf文件,然后查看日志。

resource "aws_instance" "example" {
ami = "ami-0f9ae750e8274075b"
instance_type = "t2.micro"
}

以下是用于日志输出的命令。

TF_LOG=debug TF_LOG_PATH=/tmp/terraform_debug.log terraform apply

TF_LOG_PATH和TF_LOG用于指定输出日志级别和输出目标文件。本次选择了debug级别。

查看日志

应用后,文件被输出到通过TF_LOG_PATH指定的位置。
我立即想去查看,但是出现了大量的日志信息。

根据行数来看,

#  cat /tmp/terraform_debug.log | wc -l
    1767

创建了1767个EC2实例,但只是这样简单的操作却产生了大量的日志记录。

当使用头来稍微查看一下内容时,

# cat /tmp/terraform_debug.log | head
2021/03/18 09:40:53 [INFO] Terraform version: 0.12.24
2021/03/18 09:40:53 [INFO] Go runtime version: go1.12.13
2021/03/18 09:40:53 [INFO] CLI args: []string{"/usr/local/Cellar/tfenv/1.0.2/versions/0.12.24/terraform", "apply"}
2021/03/18 09:40:53 [DEBUG] Attempting to open CLI config file: /Users/******/.terraformrc
2021/03/18 09:40:53 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2021/03/18 09:40:53 Loading CLI configuration from /Users/******/.terraform.d/credentials.tfrc.json
2021/03/18 09:40:53 [INFO] CLI command args: []string{"apply"}
2021/03/18 09:40:53 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
  ----

除了DEBUG级别的日志之外,还有其他日志被输出。仔细想想,因为通常指定了以上级别的日志被输出,所以当然会出现这种情况。

那么,如果思考一下,是否没有输出关于 DEBUG 以下信息的 TRACE 呢?

#  cat /tmp/terraform_debug.log | grep TRACE
2021/03/18 09:40:53 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
2021/03/18 09:40:53 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
2021-03-18T09:41:21.188+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 09:41:21 [TRACE] Waiting 3s before next try: timestamp=2021-03-18T09:41:21.187+0900
2021-03-18T09:41:24.469+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 09:41:24 [TRACE] Waiting 6s before next try: timestamp=2021-03-18T09:41:24.469+0900
2021-03-18T09:41:30.584+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 09:41:30 [TRACE] Waiting 10s before next try: timestamp=2021-03-18T09:41:30.584+0900

嗯嗯?有个[TRACE]呢。。从行头看起来,是不是先有[INFO],所以这是[INFO]处理还是在执行命令时被忽略了,因为TF_LOG=debug和debug是小写指定的?

查看日志2

这次我们将销毁创建的环境,并尝试使用大写的TF_LOG=DEBUG进行指定。

TF_LOG=DEBUG TF_LOG_PATH=/tmp/terraform_debug2.log terraform apply

请再使用grep命令确认一次是否有TRACE输出。

# cat /tmp/terraform_debug2.log | grep TRACE                                                       2021/03/18 10:05:38 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
2021/03/18 10:05:38 [WARN] Log levels other than TRACE are currently unreliable, and are supported only for backward compatibility.
  Use TF_LOG=TRACE to see Terraform's internal logs.
2021-03-18T10:06:07.952+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 10:06:07 [TRACE] Waiting 3s before next try: timestamp=2021-03-18T10:06:07.952+0900
2021-03-18T10:06:11.084+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 10:06:11 [TRACE] Waiting 6s before next try: timestamp=2021-03-18T10:06:11.084+0900
2021-03-18T10:06:17.511+0900 [INFO]  plugin.terraform-provider-aws_v3.29.0_x5: 2021/03/18 10:06:17 [TRACE] Waiting 10s before next try: timestamp=2021-03-18T10:06:17.511+0900

你看起来很懂。那么,这似乎是被归类到了[INFO]。

查看日志3

我已经尝试过实际指定TRACE级别会发生什么情况了。

TF_LOG=TRACE TF_LOG_PATH=/tmp/terraform_trace.log terraform apply

我会确认TRACE的内容。

# cat /tmp/terraform_trace.log | grep TRACE | head
2021/03/18 10:11:00 [TRACE] Meta.Backend: built configuration for "s3" backend with hash value 873148124
2021/03/18 10:11:00 [TRACE] Preserving existing state lineage "c6ae9e85-4f2d-0e02-65e1-c6a98f94be5e"
2021/03/18 10:11:00 [TRACE] Preserving existing state lineage "c6ae9e85-4f2d-0e02-65e1-c6a98f94be5e"
2021/03/18 10:11:00 [TRACE] Meta.Backend: working directory was previously initialized for "s3" backend
2021/03/18 10:11:00 [TRACE] Meta.Backend: using already-initialized, unchanged "s3" backend configuration
2021/03/18 10:11:03 [TRACE] Meta.Backend: instantiated backend of type *s3.Backend
2021/03/18 10:11:03 [TRACE] Meta.Backend: backend *s3.Backend does not support operations, so wrapping it in a local backend
2021/03/18 10:11:03 [TRACE] backend/local: requesting state manager for workspace "default"
2021/03/18 10:11:03 [TRACE] backend/local: requesting state lock for workspace "default"
2021/03/18 10:11:03 [TRACE] backend/local: reading remote state for workspace "default"

刚才输出了大量的日志。当指定[TRACE]和[TRACE]级别的日志出现在[INFO]内时,它们似乎是不同类型的。

日志文件的内容被输出

由于数量庞大,无法详细查看,但内容中也包含了实例ID和IP地址等详细信息。如果从输出的日志中提取各种信息,自动化的工作将会变得更加顺利。

{
  "version": 4,
  "terraform_version": "0.12.24",
  "serial": 2,
  "lineage": "79a10223-15ac-3d6e-a5ad-ca554873829d",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "aws_instance",
      "name": "example",
      "provider": "provider.aws",
      "instances": [
        {
          "schema_version": 1,
          "attributes": {
            "ami": "ami-0f9ae750e8274075b",
<省略>
广告
将在 10 秒后关闭
bannerAds