【问题标题】:How to use terraform with AWS provider and local editors?如何将 terraform 与 A​​WS 提供商和本地编辑器一起使用?
【发布时间】:2021-09-05 01:21:03
【问题描述】:

当使用 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 密钥安全问题而无法在 localhost 上运行。然后每次使用本地编辑器编辑 .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 没有进行初始化。

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

【问题讨论】:

    标签: amazon-web-services emacs localhost terraform editor


    【解决方案1】:

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

    terraform init -backend=false
    

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

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

    【讨论】:

    • 非常感谢您的建议。我升级了 tf 版本并运行了错误的后端初始化。虽然它没有完全解决问题,但看起来更好。
    猜你喜欢
    • 2022-07-06
    • 2021-03-27
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 2013-06-20
    • 2021-01-27
    相关资源
    最近更新 更多