Deploy Rails App to ElasitcBeanstalk on wercker

I published “wercker step” to deploy an application to Elastic Beanstalk instance using AWS CLI.

morizyun/eb-deploy - GitHub

This aritcle describes how to use the wercker step and outline of the step.


Configuration for wercker

In the wercker.yml of your application use the following step definition:

1
2
3
4
5
6
7
- morizyun/eb-deploy:
access_key: $AWS_ACCESS_KEY
secret_key: $AWS_SECRET_KEY
app_name: $AWS_APP_NAME
env_name: $AWS_ENV_NAME
s3_bucket: $AWS_S3_BUCKET_NAME
region: $AWS_REGION (default: ap-northeast-1)

Outline of the wercker step

Source code of the wercker step is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export UNIXTIME=`date +%s`
if [ ! -n "$WERCKER_EB_DEPLOY_ACCESS_KEY" ]; then
error 'Please specify access_key'
exit 1
fi
if [ ! -n "$WERCKER_EB_DEPLOY_SECRET_KEY" ]; then
error 'Please specify secret_key'
exit 1
fi
if [ ! -n "$WERCKER_EB_DEPLOY_APP_NAME" ]; then
error 'Please specify app_name'
exit 1
fi
if [ ! -n "$WERCKER_EB_DEPLOY_ENV_NAME" ]; then
error 'Please specify env_name'
exit 1
fi
if [ ! -n "$WERCKER_EB_DEPLOY_S3_BUCKET" ]; then
error 'Please specify s3 bucket'
exit 1
fi
if [ ! -n "$WERCKER_EB_DEPLOY_REGION" ]; then
info 'set default region as ap-northeast-1'
export WERCKER_EB_DEPLOY_REGION="ap-northeast-1"
fi
info 'Installing pip ...'
sudo apt-get update
sudo apt-get install -y python-pip libpython-all-dev zip
info 'Installing the AWS CLI ...';
pip install awscli;
export PATH=~/.local/bin:$PATH
aws --version
info 'export set default values for AWS CLI tool ...';
export AMAZON_ACCESS_KEY_ID=$WERCKER_EB_DEPLOY_ACCESS_KEY
export AMAZON_SECRET_ACCESS_KEY=$WERCKER_EB_DEPLOY_SECRET_KEY
export AWS_DEFAULT_REGION=$WERCKER_EB_DEPLOY_REGION
export EB_VERSION_LABEL=$WERCKER_EB_DEPLOY_APP_NAME.$UNIXTIME
export EB_DESCRIPTION=$WERCKER_EB_DEPLOY_ENV_NAME,$WERCKER_GIT_BRANCH
export S3_FILE_KEY=$WERCKER_EB_DEPLOY_APP_NAME/$WERCKER_EB_DEPLOY_APP_NAME.$UNIXTIME.zip
export AWS_CONFIG_FILE=$HOME/.aws/config
info 'create .aws/config ...';
mkdir -p $HOME/.aws
echo '[default]' > $HOME/.aws/config
echo 'output = json' >> $HOME/.aws/config
echo "region = $WERCKER_EB_DEPLOY_REGION" >> $HOME/.aws/config
echo "aws_access_key_id = $WERCKER_EB_DEPLOY_ACCESS_KEY" >> $HOME/.aws/config
echo "aws_secret_access_key = $WERCKER_EB_DEPLOY_SECRET_KEY" >> $HOME/.aws/config
info 'Compress source code ...'
mkdir $WERCKER_EB_DEPLOY_APP_NAME
git archive HEAD --output=$S3_FILE_KEY
info 'copy code to S3 ...'
aws s3 cp --acl private $S3_FILE_KEY "s3://$WERCKER_EB_DEPLOY_S3_BUCKET/$S3_FILE_KEY"
info 'create elasticbeanstalk application-version ...'
aws elasticbeanstalk create-application-version \
--region $WERCKER_EB_DEPLOY_REGION \
--application-name $WERCKER_EB_DEPLOY_APP_NAME \
--version-label $EB_VERSION_LABEL \
--description $EB_DESCRIPTION \
--source-bundle "{\"S3Bucket\":\"$WERCKER_EB_DEPLOY_S3_BUCKET\", \"S3Key\":\"$S3_FILE_KEY\"}"
info 'update elasticbeanstalk application ...'
aws elasticbeanstalk update-environment \
--environment-name $WERCKER_EB_DEPLOY_ENV_NAME \
--description $EB_DESCRIPTION,$WERCKER_GIT_COMMIT \
--version-label $EB_VERSION_LABEL

Happy Hacking!

GitHub Repository

morizyun/eb-deploy - GitHub

Special Thanks