Continuous Delivery with AWS Elastic Beanstalk and Travis CI

Sommer Shurbaji
5 min readAug 8, 2017

Elastic Beanstalk is a really awesome service provided by AWS that deploys, automatically scales and provisions your applications. If you’re interested in learning more, see my previous tutorial on how to deploy a Docker container using Elastic Beanstalk! Deploying with Elastic Beanstalk is extremely easy if you use the CLI, API or management console in your AWS dashboard. But with those methods, every time you want to deploy your app, you need to manually fire up the CLI or log into your dashboard, which can be time consuming.

In this tutorial, I will show you how to add a Travis CI integration that will automatically deploy your application with Elastic Beanstalk whenever you push to your master branch on github. This integration will help you be on your way to achieving a continuous delivery model!

Feel free to skip to part 2 of the tutorial if you already have Travis CI enabled with your application.

Prerequisites:

  1. You have an application with Elastic Beanstalk configuration set up. I assume that you are able to manually deploy to Elastic Beanstalk. Feel free to clone my simple Node.js and Docker app from my previous tutorial that has the Travis CI configuration set up to follow along with. You are not required to use Node.js or Docker, they are just the technologies I used in this example.
  2. Your app should be on github since in this tutorial, we will be using the github Travis CI configuration.
  3. Set up your favorite text editor. I like to use Visual Studio Code

Part 1: Add Travis CI integration to your Application

To add a Travis CI integration to your application, navigate to the settings tab on your app’s github page:

On the settings page, navigate to Integrations and services:

Click the “Add Service” drop down, then search for and click on “Travis CI”. You will be presented with a form that asks for User, Token, Domain. You don’t need to fill anything out, just click on the “add service” button. Now your services page should contain Travis CI in the list of services.

Now that we’ve added Travis to the repo, sign into https://travis-ci.org/. Add your new repository by clicking on the “+” sign in your list of repos. Make sure you sync your github account if your repo is not visible in the list. Find your repo, and then flick the repository switch on. Now Travis has been enabled! Now every time you push code to your repo, a Travis build will be triggered

The next thing we need to do is add a .travis.yml file that tells Travis CI how to build our application. First we will add an extremely simple .travis.yml and test that our builds get triggered. Add a .travis.yml file in your repo’s top level directory. Below is the starter travis config file that I added. The Docker service is not required if you are not using Docker. If you are not using Node.js, you’re .travis.yml will need to have the correct configurations for the language that you are using.

Now commit and push your .travis.yml file. You can monitor that the build has started by navigating to the repo on the Travis CI page. Below is an example of a build in progress:

Once your build passes, we can add the Elastic Beanstalk configuration to deploy your application automatically.

Part 2: Integrate Elastic Beanstalk with your Travis CI build

Now we can add the Elastic Beanstalk configuration to our .travis.yml. Append the following configuration below to your .travis.yml

Before pushing the updated .travis.yml configuration there are a few things that need to be done:

  1. Edit the region, app, env, and bucket_name to match the configurations of your app. Bucket_name corresponds to the S3 bucked that your source code is added to every time you deploy your app.
  2. Change the “on branch” configuration to match the branch that you would like to use as the “deploy” branch. I use master in this example, so that if you push to master, there will be a deployment, and no deployment on any other branch. If you omit this section, your app will be deployed every time you push code to any branch.
  3. For the access key id and secret access key, I added environment variables to my repo’s Travis settings within the Travis UI and reference them in the .travis.yml to prevent my access keys from being pushed to source control. You can get your access keys by visiting the IAM tab in your AWS management console and you either need to create a new user or use the keys from an existing user that you have saved. Add these keys as secure environment variables to your repo’s settings in the Travis CI dashboard.

Now push your updated .travis.yml file and wait for the build to start!

Once your build finishes and succeeds, the log should show some information about your application’s deployment:

If you navigate to your Elastic Beanstalk dashboard page and view your application, it should say that the application is being updated and should complete updating after a few minutes.

Closing Thoughts:

With a few button clicks and lines of code, we have added automatic deployment to our Elastic Beanstalk environment! Overall, Travis CI makes it super easy to add Continuous Integration/Continuous Deployment to your application’s development process by providing cool integrations like Elastic Beanstalk that allow you to deploy automatically. This is just one simple example using Travis CI, but feel free to explore Elastic Beanstalk deployments with other tools like Circle CI, Jenkins, and more!

Happy Coding !! 😄

Disclaimer

I’m an employee of IBM. The views expressed in this blog are mine and don’t necessarily reflect the positions, strategies, or opinions of the company.

--

--