Ron
Ron

Reputation: 1047

How do I deploy a Go app with modules to AWS EB

When I deploy the app, it runs fine on first install. But any following eb deploy procedures fail with an error that: go.mod was found, but not expected. Is there a specific configuration I have to set for deploying with Go modules? I switched to Dockerizing the app and deploying that way, which works fine. But it sounds a bit cumbersome to me as AWS Elastic Beanstalk provided specific Go environments.

Upvotes: 2

Views: 800

Answers (2)

Molek Cazaplanetas
Molek Cazaplanetas

Reputation: 41

You can work with go modules.

build.sh

#!/usr/bin/env bash
set -xe

# get all of the dependencies needed
go get

# create the application binary that EB uses
go build -o bin/application application.go

and override GOPATH to point to $HOME which defaults to /var/app/current as given in the EB configuration management dashboard.

.ebextensions/go.config

option_settings:
    aws:elasticbeanstalk:application:environment:
        GOPATH: /home/ec2-user

Upvotes: 4

NiBE
NiBE

Reputation: 927

I had the same problem, I was finally able to fix it adding this line in my build.sh script file:

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

So it is like this, in my case:

#!/usr/bin/env bash
# Stops the process if something fails
set -xe

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"

Upvotes: 1

Related Questions