-
Notifications
You must be signed in to change notification settings - Fork 8
Deploying a Golang app to Elastic Beanstalk
There are two parts to running a Golang app in Elastic Beanstalk:
- Prepare the app bundle.
- 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.
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).
#!/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
#!/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
echo "GETTING GO PACKAGES..."
./0.go-get
echo "BUILDING GO APPLICATION..."
./0.go-build
make: ./build.sh
web: bin/authserviced
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
#!/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
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.
-
ENVIRONMENT =
-
Application environment variables (e.g. TTAUTH_HOME=/Volumes/authservice/authservice-home)
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
Deploying a NodeJS app to Elastic Beanstalk
Installing files from the EB S3 Bucket
How should secret files be pushed...