Deploying a Golang app to Elastic Beanstalk - tooltwist/documentation GitHub Wiki

There are two parts to running a Golang app in Elastic Beanstalk:

  1. Prepare the app bundle.
  2. Preparing the Elastic Beanstalk environment.

At runtime config files are usually required on the EB server. We install these via the S3 bucket that comes with every EB installation.

Preparing the App Bundle

You need to create several files that will tell Elastic Beanstalk how to build and run the application. The application can then be bundled into a Zip file to be uploaded via the EB Dashboard, or it can be installed using the CLI, or it can be automatically deployed using Git hooks (for CI/CD).

0.go-get

#!/bin/bash
#
#	Get dependencies
#
unset GOROOT
GOPATH=${GOPATH}:$(cd $(dirname $0); pwd)
echo '$' export GOPATH=${GOPATH}

echo '$' go get tooltwist.com/authservice tooltwist.com/authserviced
exec     go get tooltwist.com/authservice tooltwist.com/authserviced

0.go-build

#!/bin/bash
#
#	Get dependencies
#
unset GOROOT
GOPATH=${GOPATH}:$(cd $(dirname $0); pwd)
echo '$' export GOPATH=${GOPATH}

echo '$' go build tooltwist.com/authservice tooltwist.com/authserviced
exec     go build tooltwist.com/authservice tooltwist.com/authserviced

build.sh

echo "GETTING GO PACKAGES..."
./0.go-get

echo "BUILDING GO APPLICATION..."
./0.go-build

Buildfile

make: ./build.sh

Procfile

web: bin/authserviced

.ebextensions/01_volumesFromS3.config

Update the name of your S3 bucket as required.

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: "s3"
          buckets: ["elasticbeanstalk-ap-southeast-2-206419458923"]
          roleName: 
            "Fn::GetOptionSetting": 
              Namespace: "aws:autoscaling:launchconfiguration"
              OptionName: "IamInstanceProfile"
              DefaultValue: "aws-elasticbeanstalk-ec2-role"
files:
  # Private key
  /tmp/appconfig.tgz:
    mode: "000400"
    owner: root
    group: root
    authentication: "S3Auth"
    source: https://s3-ap-southeast-2.amazonaws.com/elasticbeanstalk-ap-southeast-2-206419458923/prod-drinkcircle-authservice.appconfig.tgz
commands:
  01_install_appconfig:
    cwd: /
    command: tar xvzf /tmp/appconfig.tgz
  02_tidy:
    command: rm /opt/elasticbeanstalk/hooks/appdeploy/pre/01a_bootstrap.sh

zip-for-elastic-beanstalk.sh

#!/bin/bash
#
#	This command will zip up the contents of this project, and place the zip file on the Desktop
#
LOCATION=~/Desktop
PROJECT=authservice

# Find the next version number
cnt=1
while true ; do
	filename=${LOCATION}/eb-${PROJECT}-${cnt}.zip
	[ ! -r ${filename} ] && break;
	cnt=`expr $cnt + 1`
done
echo Creating file ${filename}

# Now zip up the file
zip -r -X ${filename} \
	.ebextensions/ \
	.elasticbeanstalk/ \
	_certs \
	0.go-build \
	0.go-get \
	Buildfile \
	Procfile \
	build.sh \
	data-dir \
	src


status=$?

# Exit with the same status as the zip command
echo Package is ${filename}
exit $status

Preparing Elastic Beanstalk

In addition to the standard steps to set up EB for a Go application, you will probably need to arrange that config files end up on the server.

Set environment variables

  • ENVIRONMENT =

  • Application environment variables (e.g. TTAUTH_HOME=/Volumes/authservice/authservice-home)

A place to maintain your config files, NOT stored in Github.

For example: /Development/ElasticBeanstalk/prod-drinkcircle/authservice:

├── 1.setenv
├── 2.prepare
├── 3.sync
├── SETENV
├── Scripts
│   ├── Dump20170623.sql
│   ├── SETENV
│   ├── db-cli
│   ├── db-init
│   ├── db-load
│   └── healthcheck
└── Volumes
    └── authservice
        └── authservice-home
            └── conf
                ├── master.json
                ├── master.json-ORIGINAL
                ├── tenant-drinkcircle.json
                └── tenant-drinkcircle.json-ORIGINAL

References

Deploying a NodeJS app to Elastic Beanstalk
Installing files from the EB S3 Bucket
How should secret files be pushed...