Elastic Beanstalk with Travis CI

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!

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

Travis CI as service now not available on github you can just directly go to

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.

  4. In this use-case user should have access to both S3 and elastic beanstalk using IAM we can set permissions

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

warnings_are_errors: false
language: node_js
node_js:
  - '10'
  - node
install:
- yarn
script:
- CI=false npm run test
deploy:
  provider: elasticbeanstalk
  access_key_id: $AWS_ACCESS_ID
  secret_access_key: $AWS_SECRET_ID
  region: us-west-2
  app: "CCCCCC"
  env: "CCCCCC-env"
  bucket_name: "XXXXXXXXXXXXXXX"
  on:
    branch: s3-elasticbeanstalk-node

No Lets manually create app on elastic beanstalk

Once application is created from AWS console we have update few things

Important things to update configuration - Select the Configuration option on the left side menu within your app in the EB console and click the little “Modify” button within the “Software” box:

Now you’ll see that there’s an empty field called “Node command”. All you have to do is enter “npm start” as the start command and hit the “Apply configuration” button on the top right corner.

Now we can update our travis.yml and put correct application name and environment name with s3 bucket name, Now lets push the code to travis

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

Last updated