$GOPATH/go.mod exists but should not in AWS Elastic Beanstalk

NiBE picture NiBE · Dec 2, 2019 · Viewed 19.3k times · Source

I'm trying to deploy a golang app based on gin framework using bitbucket pipeline and AWS Elastic Beanstalk. I create my package application.zip with all the files needed to deploy the app inside an EB, when I push it, both by pipeline or by manual menu in AWS console I got these:

17:21:49 make.1 | go: finding github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:49 make.1 | go: downloading github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:49 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:21:50 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:21:50 make.1 | + GOOS=linux
  17:21:50 make.1 | + GOARCH=amd64
  17:21:50 make.1 | + go build -o bin/application '-ldflags=-s -w'
  17:21:50 make.1 | go: cannot find main module; see 'go help modules'
  17:21:50 make.1 | exited with code 1
  17:21:50 system | sending SIGTERM to all processes

I see I forgot the go.mod file, indeed adding it then I got these message and everything works great:

  17:27:59 make.1 | go: extracting github.com/appleboy/gin-jwt v2.5.0+incompatible
  17:27:59 make.1 | go: downloading gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | go: extracting gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | go: finding gopkg.in/dgrijalva/jwt-go.v3 v3.2.0
  17:27:59 make.1 | + GOOS=linux
  17:27:59 make.1 | + GOARCH=amd64
  17:27:59 make.1 | + go build -o bin/application '-ldflags=-s -w'
  17:27:59 make.1 | go: downloading golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:00 make.1 | go: extracting golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:00 make.1 | go: finding golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c
  17:28:01 make.1 | exited with code 0
  17:28:01 system | sending SIGTERM to all processes

But then while working I made some changes in my code, and I commit again, thinking that now everything is fine but I got these logs line:

  Executing: HOME=/tmp /opt/elasticbeanstalk/lib/ruby/bin/ruby /opt/elasticbeanstalk/lib/ruby/bin/foreman start --procfile /tmp/d20191202-5748-15rj5l8/eb-buildtask-0 --root /var/app/staging --env /var/elasticbeanstalk/staging/elasticbeanstalk.env
  17:32:00 make.1 | started with pid 5761
  17:32:00 make.1 | + go get github.com/gin-gonic/gin
  17:32:00 make.1 | $GOPATH/go.mod exists but should not
  17:32:00 make.1 | exited with code 1
  17:32:00 system | sending SIGTERM to all processes

And now I'm lost about $GOPATH/go.mod exists but should not I'm using last Go vesion 1.13.4, and AWS uses 1.13.2.

MORE INFO

I'll try to be more precise about the project, sorry if the logs is not enough. I have a simple application with gorm + gin, it's basically a REST API server. the structure of my project is like this:

/go/src/company.com/project

Inside I have these files and folders:

application.go
go.mod
Controller/dashboardController.go
Model/user.go
Service/utility.go
BuildFile
Procfile

Last two files are used to let the EB build the project once is uploaded in the S3. Of course locally everything works just fine, as well as the first time I upload the complete project with go.mod included, see the second bunch of logs. But when I amend a file, and I pack the application again and send it to EB, both via bitbucket piple or aws console, it doesn't matter, I got the error.

SOLUTION

After a lot of headhache I decide to log into ElasticBeanstalk via ssh, and I found that if I add to my build.sh script the line below than all the problems go away:

sudo rm /var/app/current/go.*

My build.sh file at the end is like this:

#!/usr/bin/env bash
# Stops the process if something fails
set -xe
touch /var/app/current/go.bak
sudo rm /var/app/current/go.*

# get all of the dependencies needed
go get "github.com/gin-gonic/gin"
go get "github.com/jinzhu/gorm"
go get "github.com/jinzhu/gorm/dialects/postgres"
go get "github.com/appleboy/gin-jwt"

# create the application binary that eb uses
GOOS=linux GOARCH=amd64 go build -o bin/application -ldflags="-s -w"

Answer

serkan picture serkan · May 28, 2020

When GOPATH is set go get installs the requested module to the path provided in GOPATH but if you use .mod file, it uses the working directory.

Your case, you have both GOPATH set and have the .mod file.

You can unset GOPATH like:

unset GOPATH

It should solve your problem. Or, you can delete .mod file too.