Search code examples
goterraformterraform-provider-awsgo-modules

Go mod troubles with terraform module that builds golang binaries


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               = "[email protected]"
  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

Solution

  • 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