Setting up a CI/CD pipeline with Travis CI for a Serverless app

When working with a small team (in my case, by myself alone) its really important to outsource as much work as possible to automation. Big companies have whole teams for testing and DevOps, but sometimes we don’t have that luxury. Some of the most important tasks we want to automatize are the build, package, test and deployment of our application.

Travis CI (along other tools like CodeShip or even Jenkins) is a tool that listen to your Github repository; and every time you commit code to it, Travis CI will take your code and run on it all the tasks you need in a flow called Continuous Integration/Continuous Deployment (CI/CD) where the term “continuous” means that our app will be built and deployed with every code change we do to our repository. This guarantees that our deployed application is always in sync with our code base, and we can track exactly what changes live in the current version of our app, allowing us to revert to a previous version if we need to.

Image for post
Image for post

Also, if you work with a team of developers, it’s easy to know what and who broke the application by committing code that introduces errors. Setting up CI/CD help keeping us sane by taking away from our mind the fact we have to deploy our application.

Travis CI is an excellent option to start working in a CI/CD environment: It provides a free layer which allows you to create public projects for free. All you need is to create an account and connect it to your Github account.

Serverless applications are no different from other applications: They can be built, tested and deployed. We want Travis CI to take the code from our Github account, run the unit tests, and put into AWS.

We will again use our TacoGallery app again, which I already have hosted into Github. I already set up my account with Travis CI and linked it with my Github account.

In the Travis’ main dashboard click on “Add New Repository” (with the icon ‘+’):

Image for post
Image for post

You will see a list of all the repositories in your Github account. Scroll to your Serverless app and click on the switch icon:

Image for post
Image for post

After the icon changes to green, click in the Travis CI icon in the top of the navigation bar to come back to the dashboard. You should see a new section for your app:

Image for post
Image for post

What happened here? Travis is connected to our repository and is ready to start builds. However, we have to tell Travis how to build and deploy our project. For that, we will add a .travis.yml configuration file.

language: node_js
- node
- node_modules
- npm install -g serverless
- sls deploy

The highlights of this file are:

  • We use node_js as the language Travis CI will use to build our project.
  • Since Travis CI uses a clean workspace every time we build, we don’t want it to always download all the Node packages required for our app. That’s why we add the node_modules folder to a cache that won’t be cleared in each build.
  • The script section lists all the commands Travis CI will run. In this case, we want to install Serverless globally and use it to run the deploy command.

Add that file at the root of your project, add it and commit it to Github. As soon as you do, Travis CI will try to run the project:

Image for post
Image for post

A console will be displayed with the output of the commands we indicated in the .travis.yml file. After a while, you will notice a problem:

Image for post
Image for post

There is an error:

ServerlessError: ServerlessError: AWS provider credentials not found. You can find more info on how to set up provider credentials in our docs here:

This is because we haven’t setup our AWS credentials in Travis CI. We obviously don’t want to add them to our source code, as we are using Github and it’s publicly accessible. That’s why we will use Travis’s environment variables (for more information, check this link about Environment Variables in Travis CI).

In the project area, click on “More options” and then “Settings”. Scroll down to the section “Environment Variables”, and add two entries: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. The Serverless framework listens to environment variables for these access keys, so that’s enough to make our deployment work.

Image for post
Image for post

You can re-start the build (or do another commit to trigger a new build) and now the console will show you something like this:

Image for post
Image for post

You can navigate to AWS console and check your Lambdas, you will see they just got deployed by checking the “Last Modified” column:

Image for post
Image for post

Setting up a Travis CI is really easy. You might feel right now that something that easy doesn’t bring any value to your app. That’s the part I love about Travis CI: The way they allow you to easily set up your CI/CD process.

Now, every time you make changes to your code, and commit them to your repository, Travis CI will automatically build and deploy your Serverless application.

A man with no server

The history of my journey creating Serverless applications

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store