如何将 Terraform 与 A​​WS 提供商和本地编辑器一起使用?

How to use terraform with AWS provider and local editors?

当使用 terraform 运行 任务与 AWS as

provider "aws" {
  assume_role {
    role_arn = "arn:aws:iam::xxxx"
  }
}

terraform {
  required_version = "1.0.0"

  required_providers {
    aws = {
      version = "3.46.0"
      source  = "hashicorp/aws"
    }
  }

  backend "s3" {
    bucket         = "my-bucket"
    key            = "my-key"
    region         = "us-east-1"
  }
}

如果部署服务器上只有 运行 terraform initterraform apply,但由于 aws 密钥安全问题,无法在本地主机上执行此操作。然后每次在本地用编辑器编辑 .tf 文件都会导致这个问题:

语言服务器问题。 VS Code 和 Emacs 都无法正确获取 terraform 资源,因为它没有执行 terraform init。比如Emacs:

~/.spacemacs 配置

dotspacemacs-configuration-layers
'(lsp
  git
  (terraform :variables
      terraform-auto-format-on-save t
      terraform-backend 'lsp) 

在 .tf 文件中它总是注意到:Server tfls:3869 exited with status exit(check corresponding stderr buffer for details). Do you want to restart it? (y or n).

我发现这个缓冲区有问题:*lsp-log: tfls:1358*

[Trace - 09:04:46 AM] Sending request 'textDocument/documentLink - (5)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/user12/terraform/module1/main.tf"
  }
}

[Trace - 09:04:47 AM] Received response 'textDocument/documentLink - (5)' in 38ms.
Result: [
  {
    "tooltip": "https://github.com",
    "target": "https://github.com",
    "range": {
      "end": {
        "character": 10,
        "line": 1
      },
      "start": {
        "character": 1,
        "line": 1
      }
    }
  }
]

[Trace - 09:04:58 AM] Received response 'textDocument/documentLink - (13)' in 43ms.
Result: [
  {
    "tooltip": "https://github.com",
    "target": "https://github.com",
    "range": {
      "end": {
        "character": 10,
        "line": 1
      },
      "start": {
        "character": 1,
        "line": 1
      }
    }
  }
]


[Trace - 09:04:58 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "diagnostics": [
    {
      "message": "Resource aws_security_group does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 29,
          "line": 3
        },
        "start": {
          "character": 9,
          "line": 3
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 17
        },
        "start": {
          "character": 9,
          "line": 17
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 29
        },
        "start": {
          "character": 9,
          "line": 29
        }
      }
    },
    {
      "message": "Resource aws_security_group_rule does not exist",
      "source": "Terraform Schema",
      "severity": 1,
      "range": {
        "end": {
          "character": 34,
          "line": 42
        },
        "start": {
          "character": 9,
          "line": 42
        }
      }
    }
  ],
  "uri": "file:////Users/user12/terraform/module1/main.tf"
}

另一个缓冲区*tfls::stderr*:

...
time="2021-06-21T14:24:31+08:00" level=info msg="Log Level is Debug: false"
e[36mINFOe[0m Server started                               
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x14af3fa]

goroutine 28 [running]:
github.com/hashicorp/terraform/configs.(*Module).appendFile(0xc000e7c600, 0x0, 0xc0006b65b8, 0x1101c9d, 0xc000042410)
    /Users/user12/go/1.16.2/pkg/mod/github.com/hashicorp/terraform@v0.12.20/configs/module.go:128 +0x3a
github.com/hashicorp/terraform/configs.NewModule(0xc000847d80, 0xa, 0x10, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15)
    /Users/user12/go/1.16.2/pkg/mod/github.com/hashicorp/terraform@v0.12.20/configs/module.go:97 +0x1fa
github.com/juliosueiras/terraform-lsp/langserver.TextDocumentComplete(0x1e7d970, 0xc000bfcb10, 0xc000124000, 0x51, 0x10, 0x15, 0x1, 0x0, 0x0, 0x0, ...)
    /Users/user12/terraform/terraform-lsp/langserver/complete.go:56 +0x48e
reflect.Value.call(0x1b75760, 0x1d10f58, 0x13, 0x1cbed0c, 0x4, 0xc000bfcdb0, 0x2, 0x2, 0xc000bfcdb0, 0x199, ...)
    /Users/user12/.goenv/versions/1.16.2/src/reflect/value.go:476 +0x8e7
reflect.Value.Call(0x1b75760, 0x1d10f58, 0x13, 0xc000bfcdb0, 0x2, 0x2, 0x2, 0xc000bfcdb0, 0x1)
    /Users/user12/.goenv/versions/1.16.2/src/reflect/value.go:337 +0xb9
github.com/creachadair/jrpc2/handler.newHandler.func7(0x1e7d970, 0xc000bfcb10, 0xc0002d0540, 0x1c38e60, 0xc000bfcb10, 0x1b87280, 0x246a8a0)
    /Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/jrpc2@v0.4.2/handler/handler.go:222 +0x1f4
github.com/creachadair/jrpc2/handler.Func.Handle(0xc0000d23e0, 0x1e7d970, 0xc000bfcb10, 0xc0002d0540, 0x0, 0x0, 0x1e7d970, 0xc000bfcb10)
    /Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/jrpc2@v0.4.2/handler/handler.go:23 +0x44
github.com/creachadair/jrpc2.(*Server).invoke(0xc0001ec240, 0x1e7d8c8, 0xc0002d0580, 0x1e69b60, 0xc0000d23e0, 0xc0002d0540, 0x0, 0x0, 0x0, 0x0, ...)
    /Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/jrpc2@v0.4.2/server.go:288 +0x175
github.com/creachadair/jrpc2.(*Server).dispatch.func1(0xc000c0e814, 0xc0001ec240, 0xc000bdc420)
    /Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/jrpc2@v0.4.2/server.go:185 +0x91
created by github.com/creachadair/jrpc2.(*Server).dispatch
    /Users/user12/go/1.16.2/pkg/mod/github.com/creachadair/jrpc2@v0.4.2/server.go:183 +0x137

Process tfls stderr finished

所以我认为主要原因是 Terraform 没有进行初始化。

在这种情况下,如何在本地主机上模拟 AWS 提供商以正确使用本地编辑器?我发现 Local Provider 只适用于文件。有什么好的办法解决吗?

我不熟悉这个 tfls 程序,所以我无法评论恐慌的原因,但如果它确实与不相关 运行 terraform init正如您所怀疑的那样,一种继续进行的方法可能是在不激活后端的情况下进行初始化:

terraform init -backend=false

在此模式下,Terraform 仍应执行所有常规安装步骤(模块和提供程序),但会跳过尝试初始化 S3 后端,因此即使您没有可用的 AWS 凭证,它也应该会成功。

根据堆栈跟踪,此 tfls 程序使用的代码来自相当旧版本的 Terraform,因此即使初始化后它也可能无法完全理解 Terraform v1.0 支持的 Terraform 语言及以后。