Skip to content

Instantly share code, notes, and snippets.

@DevoKun
Created November 25, 2017 03:56
Show Gist options
  • Select an option

  • Save DevoKun/9eea593d8c258c9476d9fd87d25d1a54 to your computer and use it in GitHub Desktop.

Select an option

Save DevoKun/9eea593d8c258c9476d9fd87d25d1a54 to your computer and use it in GitHub Desktop.
How to deploy GO applications to Elastic Beanstalk without or with Docker

Deploy go application to Elastic Beanstalk

non-Docker Elastic Beanstalk GO

Create the Elastic Beanstalk deployment bundle

zip eb.zip -r bin

Methods to deploy using the non-Docker Elastic Beanstalk GO

  • Elastic Beanstalk spins up an EC2 Instance running an NGinX reverseproxy and your app.

  • In the case of GO, you can either upload a .go source file or you can upload the compiled binary.

  • Elastic Beanstalk will run the compiled binary from /var/app/current/bin and expects the binary name to be application

  • By default, Elastic Beanstalk configures the nginx proxy to forward requests to your application on port 5000.

  • You can override the default port by setting the PORT system property to the port on which your main application listens.

1. Source bundle with a source file at the root called application.go

  • Elastic Beanstalk automatically builds the binary using the following command at deployment time:
export GOOS="linux"
export GOARCH="amd64"
mkdir bin
go build -installsuffix bin -o application application.go

2. Provide a source bundle with a binary file called application

  • The binary file can be located either at the root of the source bundle or in the bin/ directory of the source bundle.
  • If you place the application binary file in both locations, Elastic Beanstalk uses the file in the bin/ directory.

Docker-based Elastic Beanstalk GO

Create a statically compiled GO application that does not depend on any system libs

##
## Do not look for C libs on the system.
## Disable cgo to create a static binary.
##
export CGO_ENABLED="0"

##
## Compile for 64-bit Linux
##
export GOOS="linux"
export GOARCH="amd64"

##
## -a : Rebuild all packages
##      All imported libs will be rebuilt with CGO disabled.
##
go build -a -installsuffix bin -o application application.go

Build the Docker Image

  • Use the empty scratch image as the base.
  • Scratch is a completely empty image, which will work well with the staticly compiled go app.
  • In Docker, FROM scratch is a no-op in the Dockerfile, and will not create an extra layer in your image, so the image will be as small as possible.

Create the Dockerfile

FROM   scratch
EXPOSE 5000
ADD    application /
CMD    ["/application"]

Build a tagged image

docker build -t gopagego .

Push the docker image to the public Docker Hub Repository

docker login --username=devokun
docker images
docker tag   a39df4034764 devokun/gopagego:1
docker push  devokun/gopagego

Methods to deploy using the Docker-based Elastic Beanstalk GO

  • Elastic Beanstalk spins up an EC2 Instance running an NGinX reverseproxy and your docker container.
  • For the docker container, you can pull from a docker repository or build an image to run.

1. Source bundle with a Dockerfile and the compiled application

  • GOTCHA : Elastic Beanstalk will not allow you to use scratch as the base container, so you will need to choose another small container to start with like busybox.

FILE: Dockerfile

FROM   busybox
EXPOSE 5000
ADD    application /
CMD    ["/application"]

2. Source bundle with a config file telling Elastic Beanstalk where to get the Docker image

FILE : Dockerrun.aws.json

{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name":   "devokun/gopagego:1",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "5000"
    }
  ],
  "environment": [
    {
      "name":  "ENVIRONMENT",
      "value": "production"
    },
    {
      "name":  "PORT",
      "value": "5000"
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment