使用terraform模块构建golang二进制文件时遇到了Go mod的问题。

huangapple go评论86阅读模式
英文:

Go mod troubles with terraform module that builds golang binaries

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我在一个边项目的最后一步,但是我无法让terraform在我尝试将代码部署为terraform模块时构建我的golang二进制文件。

我的terraform模块调用如下所示:

module "moot" {
  source = "github.com/seanturner026/moot.git"

  name                           = "moot"
  admin_user_email               = "blah@example.com"
  enable_delete_admin_user       = false
  github_token                   = "42"
  gitlab_token                   = "42"
  slack_webhook_url              = "42"
  fqdn_alias                     = ""
  hosted_zone_name               = ""
  enable_api_gateway_access_logs = false
  tags                           = { Name = "moot" }
}

由于某种原因,go modules正在干扰我用来构建golang lambda二进制文件的null_resource,当我通过模块部署时会出错。

这个模块调用构建了很多东西,但你可以构建相当简单的资源,仍然会遇到以下命令的错误:

terraform apply -target='module.moot.null_resource.lambda_build["users"]'

运行此命令会出现以下错误:

module.moot.null_resource.lambda_build["users"]: Creating...
module.moot.null_resource.lambda_build["users"]: Provisioning with 'local-exec'...
module.moot.null_resource.lambda_build["users"] (local-exec): Executing: ["/bin/sh" "-c" "export GO111MODULE=on"]
module.moot.null_resource.lambda_build["users"]: Provisioning with 'local-exec'...
module.moot.null_resource.lambda_build["users"] (local-exec): Executing: ["/bin/sh" "-c" "GOOS=linux go build -ldflags '-s -w' -o .terraform/modules/moot/bin/users .terraform/modules/moot/cmd/users/."]
module.moot.null_resource.lambda_build["users"] (local-exec): no required module provides package .terraform/modules/moot/cmd/users: go.mod file not found in current directory or any parent directory; see 'go help modules'

null_resource的代码位于此处:https://github.com/seanturner026/moot/blob/e51aee51d12472735e6bc5902e54dd603f750aff/r_lambda.tf#L1-L23

使用模块源为../../的示例确实可以成功工作。我不确定原因是什么,但可能是因为go.mod在上级目录中?无论如何,该示例可以在此处部署,并且上述的terraform apply命令可以工作:https://github.com/seanturner026/moot/blob/stackoverflow/terraform_examples/complete/main.tf

我还有一个位于此处的makefile:https://github.com/seanturner026/moot/blob/stackoverflow/Makefile。运行make build也会失败。

如何解决这个问题?我最近重命名了仓库,这可能会引起问题吗?

当我将github.com/seanturner026/moot.git指定为模块调用源时,这是目录布局:

.
├── .terraform
│   └── modules
│       └── moot
│           ├── .editorconfig
│           ├── .gitignore
│           ├── Makefile
│           ├── README.md
│           ├── archive
│           ├── assets
│           │   ├── repositories-add.png
│           │   ├── repositories.png
│           │   └── users.png
│           ├── bin
│           │   └── users
│           ├── cmd
│           │   ├── auth
│           │   │   ├── events.json
│           │   │   ├── login.go
│           │   │   ├── login_test.go
│           │   │   ├── main.go
│           │   │   ├── reset_password.go
│           │   │   └── reset_password_test.go
│           │   ├── releases
│           │   │   ├── event.json
│           │   │   ├── github.go
│           │   │   ├── gitlab.go
│           │   │   ├── main.go
│           │   │   └── main_test.go
│           │   ├── repositories
│           │   │   ├── create.go
│           │   │   ├── create_test.go
│           │   │   ├── delete.go
│           │   │   ├── delete_test.go
│           │   │   ├── events.json
│           │   │   ├── list.go
│           │   │   ├── list_test.go
│           │   │   └── main.go
│           │   └── users
│           │       ├── create.go
│           │       ├── create_test.go
│           │       ├── delete.go
│           │       ├── delete_test.go
│           │       ├── events.json
│           │       ├── list.go
│           │       ├── list_test.go
│           │       └── main.go
│           ├── data.tf
│           ├── go.mod
│           ├── go.sum
│           ├── internal
│           │   └── util
│           │       ├── generate_response_body.go
│           │       ├── generate_secret_hash.go
│           │       ├── main.go
│           │       └── post_to_slack.go
│           ├── locals.tf
│           ├── modules.tf
│           ├── outputs.tf
│           ├── r_acm.tf
│           ├── r_api_gateway.tf
│           ├── r_cloudwatch.tf
│           ├── r_cognito.tf
│           ├── r_dynamodb.tf
│           ├── r_iam.tf
│           ├── r_lambda.tf
│           ├── r_null.tf
│           ├── r_route53.tf
│           ├── r_s3.tf
│           ├── r_ssm.tf
│           ├── terraform_assets
│           │   ├── cognito.go
│           │   ├── cognito_invite_template.html
│           │   └── dynamodb_put_item_input.json
│           └── variables.tf
├── .terraform.lock.hcl
├── main.tf
├── provider.tf
├── terraform.auto.tfvars
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf
英文:

I'm on the last step of a side project, and I can't get terraform to build my golang binaries now that I'm trying to deploy my code as a terraform module.

My terraform module invocation looks like this.

module "moot" {
  source = "github.com/seanturner026/moot.git"

  name                           = "moot"
  admin_user_email               = "blah@example.com"
  enable_delete_admin_user       = false
  github_token                   = "42"
  gitlab_token                   = "42"
  slack_webhook_url              = "42"
  fqdn_alias                     = ""
  hosted_zone_name               = ""
  enable_api_gateway_access_logs = false
  tags                           = { Name = "moot" }
}

For some reason, go modules is messing with my null_resources which I use to build my golang lambda binaries when I deploy via the module.

This module invocation builds a whole heap of things, but you can build pretty minimal resources and still hit the error with the following command.

terraform apply -target='module.moot.null_resource.lambda_build["users"]'

Running this command errors out with the following:

module.moot.null_resource.lambda_build["users"]: Creating...
module.moot.null_resource.lambda_build["users"]: Provisioning with 'local-exec'...
module.moot.null_resource.lambda_build["users"] (local-exec): Executing: ["/bin/sh" "-c" "export GO111MODULE=on"]
module.moot.null_resource.lambda_build["users"]: Provisioning with 'local-exec'...
module.moot.null_resource.lambda_build["users"] (local-exec): Executing: ["/bin/sh" "-c" "GOOS=linux go build -ldflags '-s -w' -o .terraform/modules/moot/bin/users .terraform/modules/moot/cmd/users/."]
module.moot.null_resource.lambda_build["users"] (local-exec): no required module provides package .terraform/modules/moot/cmd/users: go.mod file not found in current directory or any parent directory; see 'go help modules'

The null_resource code is located here https://github.com/seanturner026/moot/blob/e51aee51d12472735e6bc5902e54dd603f750aff/r_lambda.tf#L1-L23

The example which uses a module source of ../../ (rather than the github link) does work successfully. I'm not sure why, but perhaps it's because go.mod is in an upper directory? Either way, that example is deployable here, and the above terraform apply command works https://github.com/seanturner026/moot/blob/stackoverflow/terraform_examples/complete/main.tf

I've also got a makefile located here https://github.com/seanturner026/moot/blob/stackoverflow/Makefile. Running make build also fails

$ make build
export GO111MODULE=on
env GOOS=linux go build -ldflags="-s -w" -o bin/auth         cmd/auth/. &
env GOOS=linux go build -ldflags="-s -w" -o bin/releases     cmd/releases/. &
env GOOS=linux go build -ldflags="-s -w" -o bin/repositories cmd/repositories/. &
env GOOS=linux go build -ldflags="-s -w" -o bin/users        cmd/users/. &
package cmd/auth is not in GOROOT (/usr/local/go/src/cmd/auth)
package cmd/releases is not in GOROOT (/usr/local/go/src/cmd/releases)
package cmd/repositories is not in GOROOT (/usr/local/go/src/cmd/repositories)
package cmd/users is not in GOROOT (/usr/local/go/src/cmd/users)

How do I go about fixing this problem? I recently renamed the repo, could that be causing issues?

Here's the directory layout when I'm specifying github.com/seanturner026/moot.git as the module invocation source.

.
├── .terraform
│   └── modules
│       └── moot
│           ├── .editorconfig
│           ├── .gitignore
│           ├── Makefile
│           ├── README.md
│           ├── archive
│           ├── assets
│           │   ├── repositories-add.png
│           │   ├── repositories.png
│           │   └── users.png
│           ├── bin
│           │   └── users
│           ├── cmd
│           │   ├── auth
│           │   │   ├── events.json
│           │   │   ├── login.go
│           │   │   ├── login_test.go
│           │   │   ├── main.go
│           │   │   ├── reset_password.go
│           │   │   └── reset_password_test.go
│           │   ├── releases
│           │   │   ├── event.json
│           │   │   ├── github.go
│           │   │   ├── gitlab.go
│           │   │   ├── main.go
│           │   │   └── main_test.go
│           │   ├── repositories
│           │   │   ├── create.go
│           │   │   ├── create_test.go
│           │   │   ├── delete.go
│           │   │   ├── delete_test.go
│           │   │   ├── events.json
│           │   │   ├── list.go
│           │   │   ├── list_test.go
│           │   │   └── main.go
│           │   └── users
│           │       ├── create.go
│           │       ├── create_test.go
│           │       ├── delete.go
│           │       ├── delete_test.go
│           │       ├── events.json
│           │       ├── list.go
│           │       ├── list_test.go
│           │       └── main.go
│           ├── data.tf
│           ├── go.mod
│           ├── go.sum
│           ├── internal
│           │   └── util
│           │       ├── generate_response_body.go
│           │       ├── generate_secret_hash.go
│           │       ├── main.go
│           │       └── post_to_slack.go
│           ├── locals.tf
│           ├── modules.tf
│           ├── outputs.tf
│           ├── r_acm.tf
│           ├── r_api_gateway.tf
│           ├── r_cloudwatch.tf
│           ├── r_cognito.tf
│           ├── r_dynamodb.tf
│           ├── r_iam.tf
│           ├── r_lambda.tf
│           ├── r_null.tf
│           ├── r_route53.tf
│           ├── r_s3.tf
│           ├── r_ssm.tf
│           ├── terraform_assets
│           │   ├── cognito.go
│           │   ├── cognito_invite_template.html
│           │   └── dynamodb_put_item_input.json
│           └── variables.tf
├── .terraform.lock.hcl
├── main.tf
├── provider.tf
├── terraform.auto.tfvars
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf

答案1

得分: 1

在构建 Lambda 二进制文件的 null_resource 中,需要在命令二进制文件前面加上 ./

还需要使用另一个 null_resource 将 go.mod 复制到顶级目录中

https://github.com/seanturner026/moot/pull/6/commits/3ff8e18c5449f610eb9ca99c8e89bd31717a8bd9

英文:

needed to put a ./ in front of the cmd binaries in the null_resource that builds the lambda binaries

Also needed to copy go.mod to the top level directory with another null_resource

https://github.com/seanturner026/moot/pull/6/commits/3ff8e18c5449f610eb9ca99c8e89bd31717a8bd9

huangapple
  • 本文由 发表于 2021年5月23日 12:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/67656271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定