Deploying to Elastic Beanstalk on git push with Jenkins

Here are some notes I took on how I set up my Jenkins server and deployed my application to Elastic Beanstalk on every git push.

Create new EC2 instance with Ubuntu 14.04

First create a new EC2 instance. For my server I’m using the Ubuntu 14.04 AMI - t2.micro tier

  • Network: my VPC
  • Security group: allowing whitelisted traffic on port 22, 80, and 443. Github’s IP is whitelisted too: on ports 22, 80, 443, and 9418


Set up reverse proxy -

wget -q -O - | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
sudo apt-get install apache2
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod vhost_alias
sudo a2dissite 000-default
sudo vim /etc/apache2/sites-available/jenkins.conf
<VirtualHost *:80>
        ServerAlias jenkins
        ProxyRequests Off
        <Proxy *>
                Order deny,allow
                Allow from all
        ProxyPreserveHost on
        ProxyPass / http://localhost:8080/ nocanon
        ProxyPassReverse / http://localhost:8080/
        AllowEncodedSlashes NoDecode
sudo a2ensite jenkins
sudo service apache2 reload
sudo apache2ctl restart
sudo /etc/init.d/jenkins start

Install git and now the Jenkins server should be all set up.

sudo apt-get install git

Set up security

Manage Jenkins -> Configure Global Security

Register a new user under the same username that you created permissions for.

Back in AWS you should attach an IAM policy to the security group the app is in to access the S3 bucket and EB deployment permissions. This is my policy:

    "Version": "2012-10-17",
    "Statement": [
            "Sid": "Stmt1446219985300",
            "Action": [
            "Effect": "Allow",
            "Resource": "arn:aws:elasticbeanstalk:*"
            "Sid": "Stmt1446158122885",
            "Action": [
            "Effect": "Allow",
            "Resource": "arn:aws:s3:::*"

Set up GitHub integration

Install GitHub plugin, create SSH keys on EC2 Jenkins instance as the jenkins user. Add the keys to GitHub via the reference below. Reference: Do everything as jenkins user: sudo su -s /bin/bash jenkins

Now we can go to Credentials -> Global credentials -> Add

I left my passphrase blank. For some reason I couldn’t get a passphrase to work. Also now would be a good time to go to your GitHub project page, go to settings, and add a webhook which is your public IP/DNS followed by /github-webhook/.

Plugins to Install

Hudson Post build task plugin AWS Elastic Beanstalk Deployment Plugin Promoted Builds Plugin S3 publisher plugin Token Macro

Create a build

Create new jobs -> Freestyle project Enter the GitHub project URL Tick Promo builds when… and Promote immediately once the build is complete. Add Deploy into AWS Elastic Beanstalk as a build step

Appplication name: Elastic Beanstalk application name, not environment name Root Object: . Includes: */ Version Label Format: ${GIT_COMMIT}-${BUILD-TAG} Environment name: Under application name

I couldn’t get the ${GIT_COMMIT} label to work for no good reason. I guess it’s broken. My solution is to install the Build Name Setter plugin and use the following settings:

That way I can tell which of my EB builds applies to which Git commit.